Skip to content

Commit

Permalink
#157 Code formatting and PHPstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfwd committed Nov 6, 2022
1 parent 503230c commit bce8944
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
7 changes: 5 additions & 2 deletions api/src/Markdown/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace App\Markdown;

use App\Markdown\Model\Question;
use App\Markdown\Model\Quiz;

interface GeneratorInterface
{
/**
* @param string $source
* @return array
* @return array<int, Quiz>|array<int, Question>
*/
public function generate(string $source): array;

/**
* @param string[] $filePaths
* @return array
* @return array<int, Quiz>|array<int, Question>
*/
public function process(array $filePaths): array;
}
25 changes: 15 additions & 10 deletions api/src/Markdown/Model/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

namespace App\Markdown\Model;

use DOMNode;

class Question implements ModelInterface
{
/**
* @param int $id
* @param int $quizID
* @param string $filePath
* @param string $title
* @param DOMNode[] $content
* @param DOMNode[] $possibleAnswers
* @param DOMNode[] $correctAnswer
*/
public function __construct(
private readonly int $id,
private readonly int $quizID,
private readonly string $filePath,
private readonly string $title,
private readonly array $content,
private readonly array $possibleAnswers,
private readonly array $correctAnswer
private readonly array $correctAnswer,
) {
}

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

/**
* @return array
*/
/** @return DOMNode[] **/
public function getContent(): array
{
return $this->content;
}

/**
* @return array
*/
/** @return DOMNode[] **/
public function getPossibleAnswers(): array
{
return $this->possibleAnswers;
}

/**
* @return array
*/
/** @return DOMNode[] **/
public function getCorrectAnswer(): array
{
return $this->correctAnswer;
Expand Down
7 changes: 6 additions & 1 deletion api/src/Markdown/Parser/DocumentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

class DocumentExtractor
{
/** @var DOMNode[] **/
private array $question = [];
/** @var DOMNode[] **/
private array $possibleAnswers = [];
/** @var DOMNode[] **/
private array $correctAnswer = [];

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

$this->process($domDocument);
}
Expand Down Expand Up @@ -70,16 +72,19 @@ public function process(DOMNode $domNode): void
}
}

/** @return DOMNode[] **/
public function getQuestionNodes(): array
{
return $this->question;
}

/** @return DOMNode[] **/
public function getPossibleAnswerNodes(): array
{
return $this->possibleAnswers;
}

/** @return DOMNode[] **/
public function getCorrectAnswerNodes(): array
{
return $this->correctAnswer;
Expand Down
14 changes: 11 additions & 3 deletions api/src/Markdown/QuestionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function getTitleFromFilePath(string $filePath): false|string
unset($filenameParts[0], $filenameParts[1]);
$title = implode(' ', $filenameParts);
$title = preg_replace('/\.md/i', '', $title);
if (!is_string($title)) {
return false;
}
return ucfirst($title);
}

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

return $filenameParts[$index];
return (int) $filenameParts[$index];
}

/**
* @param string $source
* @return array{int, array{id: int, name: string, file_path: string}}
* @return array<int, Question>
*/
public function generate(string $source): array
{
Expand All @@ -56,7 +59,7 @@ public function generate(string $source): array

/**
* @param string[] $filePaths
* @return array{int, array{id: int, name: string, file_path: string}}
* @return array<int, Question>
*/
public function process(array $filePaths): array
{
Expand All @@ -65,6 +68,11 @@ public function process(array $filePaths): array
$quizID = $this->getIDFromFilePath($filePath);
$questionID = $this->getIDFromFilePath($filePath, false);
$title = $this->getTitleFromFilePath($filePath);

if (!$quizID || !$questionID || !$title) {
continue;
}

$content = [];
$possibleAnswers = [];
$correctAnswers = [];
Expand Down
4 changes: 2 additions & 2 deletions api/src/Markdown/QuizGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(private readonly FetcherInterface $fetcher)

/**
* @param string $source
* @return Quiz[]
* @return array<int, Quiz>
*/
public function generate(string $source): array
{
Expand Down Expand Up @@ -44,7 +44,7 @@ public function generateNameFromFilePath(string $filePath): string

/**
* @param string[] $filePaths
* @return array{int, array{id: int, name: string, file_path: string}}
* @return array<int, Quiz>
*/
public function process(array $filePaths): array
{
Expand Down

0 comments on commit bce8944

Please sign in to comment.