Skip to content

Commit bce8944

Browse files
committed
#157 Code formatting and PHPstan fixes
1 parent 503230c commit bce8944

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

api/src/Markdown/GeneratorInterface.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace App\Markdown;
44

5+
use App\Markdown\Model\Question;
6+
use App\Markdown\Model\Quiz;
7+
58
interface GeneratorInterface
69
{
710
/**
811
* @param string $source
9-
* @return array
12+
* @return array<int, Quiz>|array<int, Question>
1013
*/
1114
public function generate(string $source): array;
1215

1316
/**
1417
* @param string[] $filePaths
15-
* @return array
18+
* @return array<int, Quiz>|array<int, Question>
1619
*/
1720
public function process(array $filePaths): array;
1821
}

api/src/Markdown/Model/Question.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
namespace App\Markdown\Model;
44

5+
use DOMNode;
6+
57
class Question implements ModelInterface
68
{
9+
/**
10+
* @param int $id
11+
* @param int $quizID
12+
* @param string $filePath
13+
* @param string $title
14+
* @param DOMNode[] $content
15+
* @param DOMNode[] $possibleAnswers
16+
* @param DOMNode[] $correctAnswer
17+
*/
718
public function __construct(
819
private readonly int $id,
920
private readonly int $quizID,
1021
private readonly string $filePath,
1122
private readonly string $title,
1223
private readonly array $content,
1324
private readonly array $possibleAnswers,
14-
private readonly array $correctAnswer
25+
private readonly array $correctAnswer,
1526
) {
1627
}
1728

@@ -31,25 +42,19 @@ public function getFilePath(): string
3142
return $this->filePath;
3243
}
3344

34-
/**
35-
* @return array
36-
*/
45+
/** @return DOMNode[] **/
3746
public function getContent(): array
3847
{
3948
return $this->content;
4049
}
4150

42-
/**
43-
* @return array
44-
*/
51+
/** @return DOMNode[] **/
4552
public function getPossibleAnswers(): array
4653
{
4754
return $this->possibleAnswers;
4855
}
4956

50-
/**
51-
* @return array
52-
*/
57+
/** @return DOMNode[] **/
5358
public function getCorrectAnswer(): array
5459
{
5560
return $this->correctAnswer;

api/src/Markdown/Parser/DocumentExtractor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
class DocumentExtractor
99
{
10+
/** @var DOMNode[] **/
1011
private array $question = [];
12+
/** @var DOMNode[] **/
1113
private array $possibleAnswers = [];
14+
/** @var DOMNode[] **/
1215
private array $correctAnswer = [];
1316

1417
private bool $foundPossibleAnswers = false;
@@ -26,7 +29,6 @@ public function extract(): void
2629
$domDocument = new DOMDocument();
2730
libxml_use_internal_errors(true);
2831
$domDocument->loadHTML($this->document);
29-
libxml_get_errors();
3032

3133
$this->process($domDocument);
3234
}
@@ -70,16 +72,19 @@ public function process(DOMNode $domNode): void
7072
}
7173
}
7274

75+
/** @return DOMNode[] **/
7376
public function getQuestionNodes(): array
7477
{
7578
return $this->question;
7679
}
7780

81+
/** @return DOMNode[] **/
7882
public function getPossibleAnswerNodes(): array
7983
{
8084
return $this->possibleAnswers;
8185
}
8286

87+
/** @return DOMNode[] **/
8388
public function getCorrectAnswerNodes(): array
8489
{
8590
return $this->correctAnswer;

api/src/Markdown/QuestionGenerator.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function getTitleFromFilePath(string $filePath): false|string
2525
unset($filenameParts[0], $filenameParts[1]);
2626
$title = implode(' ', $filenameParts);
2727
$title = preg_replace('/\.md/i', '', $title);
28+
if (!is_string($title)) {
29+
return false;
30+
}
2831
return ucfirst($title);
2932
}
3033

@@ -41,12 +44,12 @@ public function getIDFromFilePath(string $filePath, bool $isQuiz = true): false|
4144
return false;
4245
}
4346

44-
return $filenameParts[$index];
47+
return (int) $filenameParts[$index];
4548
}
4649

4750
/**
4851
* @param string $source
49-
* @return array{int, array{id: int, name: string, file_path: string}}
52+
* @return array<int, Question>
5053
*/
5154
public function generate(string $source): array
5255
{
@@ -56,7 +59,7 @@ public function generate(string $source): array
5659

5760
/**
5861
* @param string[] $filePaths
59-
* @return array{int, array{id: int, name: string, file_path: string}}
62+
* @return array<int, Question>
6063
*/
6164
public function process(array $filePaths): array
6265
{
@@ -65,6 +68,11 @@ public function process(array $filePaths): array
6568
$quizID = $this->getIDFromFilePath($filePath);
6669
$questionID = $this->getIDFromFilePath($filePath, false);
6770
$title = $this->getTitleFromFilePath($filePath);
71+
72+
if (!$quizID || !$questionID || !$title) {
73+
continue;
74+
}
75+
6876
$content = [];
6977
$possibleAnswers = [];
7078
$correctAnswers = [];

api/src/Markdown/QuizGenerator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct(private readonly FetcherInterface $fetcher)
1212

1313
/**
1414
* @param string $source
15-
* @return Quiz[]
15+
* @return array<int, Quiz>
1616
*/
1717
public function generate(string $source): array
1818
{
@@ -44,7 +44,7 @@ public function generateNameFromFilePath(string $filePath): string
4444

4545
/**
4646
* @param string[] $filePaths
47-
* @return array{int, array{id: int, name: string, file_path: string}}
47+
* @return array<int, Quiz>
4848
*/
4949
public function process(array $filePaths): array
5050
{

0 commit comments

Comments
 (0)