Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jekyll refactor #19

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="./tests/bootstrap.php" backupGlobals="true" colors="true" forceCoversAnnotation="true">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">

<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
183 changes: 183 additions & 0 deletions src/Generator/Formatter/MarkdownFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
declare(strict_types=1);

namespace App\Generator\Formatter;

use App\Value\Diff;
use App\Value\Property;
use App\Value\Sniff;
use App\Value\Url;
use App\Value\UrlList;
use App\Value\Violation;
use Stringy\Stringy;
use function Stringy\create as s;

final class MarkdownFormatter
{
public function formatDescription(Sniff $sniff): string
{
$description = $sniff->getDescription();
if ($description === '') {
return '';
}

return <<<MD
{$description}
MD;
}

public function formatDocblock(Sniff $sniff): string
{
$docblock = $sniff->getDocblock();
if ($docblock === '') {
return '';
}

return <<<MD
## Docblock

{$docblock}
MD;
}

/**
* @param Diff[] $diffs
*/
public function formatComparisons(array $diffs): string
{
if ($diffs === []) {
return '';
}

$diffBlocks = implode("\n\n", $this->getDiffBlocks($diffs));

return <<<MD
## Comparisons

{$diffBlocks}
MD;
}

/**
* @param Diff[] $diffs
* @return string[]
*/
private function getDiffBlocks(array $diffs): array
{
return array_map(function (Diff $diff): string {
return <<<MD
```diff
{$this->prependLinesWith('-', $diff->getBefore())}
{$this->prependLinesWith('+', $diff->getAfter())}
```
MD;
}, $diffs);
}

private function prependLinesWith(string $prefix, string $lines): string
{
$prependedLines = array_map(function (Stringy $line) use ($prefix) {
return (string)$line->prepend($prefix);
}, s($lines)->lines());

return implode("\n", $prependedLines);
}

/**
* @param Property[] $properties
*/
public function formatPublicProperties(array $properties): string
{
if ($properties === []) {
return '';
}

$propertyLines = implode("\n", $this->getPublicPropertyLines($properties));

return <<<MD
## Public Properties

{$propertyLines}
MD;
}

/**
* @param Property[] $properties
* @return string[]
*/
private function getPublicPropertyLines(array $properties): array
{
return array_map(function (Property $property) {
return "- `\${$property->getName()}` : {$property->getType()} {$property->getDescription()}";
}, $properties);
}

/**
* @return string
*/
public function formatSeeAlso(UrlList $urls): string
{
if ($urls->toArray() === []) {
return '';
}

$linkLines = implode("\n", $this->getLinkLines($urls));

return <<<MD
## See Also

{$linkLines}
MD;
}

/**
* @return string[]
*/
private function getLinkLines(UrlList $urls): array
{
return array_map(function (Url $url) {
return "- [$url]($url)";
}, $urls->toArray());
}

/**
* @param Violation[] $violations
* @return string
*/
public function formatViolations(array $violations): string
{
if ($violations === []) {
return '';
}

$violations = implode("\n", $this->getViolationBlocks($violations));

return <<<MD
## Troubleshooting

{$violations}
MD;
}

/**
* @param Violation[] $violations
* @return string[]
*/
private function getViolationBlocks(array $violations): array
{
return array_map(function (Violation $violation): string {
return <<<MD
```
<details>
<summary>{$violation->getCode()}</summary>
{$violation->getDescription()}

{$this->formatComparisons($violation->getDiffs())}

{$this->formatSeeAlso($violation->getUrls())}
</details>
```
MD;
}, $violations);
}
}
2 changes: 1 addition & 1 deletion src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

interface Generator
{
public function createViolationDoc(Violation $doc): string;
public function createViolationDoc(Violation $violation): string;

public function createSniffDoc(Sniff $sniff): string;
}
46 changes: 35 additions & 11 deletions src/Generator/JekyllPageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,52 @@

namespace App\Generator;

use App\Generator\Formatter\MarkdownFormatter;
use App\Value\Sniff;
use App\Value\Violation;

class JekyllPageGenerator extends MarkdownGenerator implements Generator
final class JekyllPageGenerator implements Generator
{
private MarkdownFormatter $formatter;
private const LINE_ENDING_REGEX = '\r\n|\n';

public function __construct(MarkdownFormatter $formatter)
{
$this->formatter = $formatter;
}

public function createSniffDoc(Sniff $sniff): string
{
$sniffDoc = $this->getFrontMatter($sniff) . "\n";
$sniffDoc .= parent::createSniffDoc($sniff);
$sniffDoc = <<<MD
{$this->getFrontMatter($sniff)}
# {$sniff->getCode()}

{$this->formatter->formatDescription($sniff)}
{$this->formatter->formatDocblock($sniff)}
{$this->formatter->formatComparisons($sniff->getDiffs())}
{$this->formatter->formatPublicProperties($sniff->getProperties())}
{$this->formatter->formatSeeAlso($sniff->getUrls())}
{$this->formatter->formatViolations($sniff->getViolations())}
MD;

return $sniffDoc;
$sniffDoc = preg_replace('`'.self::LINE_ENDING_REGEX.'{3,}`', "\n\n", $sniffDoc);
return preg_replace('`'.self::LINE_ENDING_REGEX.'{2,}$`', "\n", $sniffDoc);
}

public function createViolationDoc(Violation $violation): string
{
return <<<MD
{$violation->getDescription()}

{$this->formatter->formatComparisons($violation->getDiffs())}

{$this->formatter->formatSeeAlso($violation->getUrls())}
MD;
}

private function getFrontMatter(Sniff $sniff): string
{
$sniffName = $sniff->getSniffName();
if ($sniffName === '') {
return <<<'MD'
---
---

MD;
}

return <<<MD
---
Expand Down
Loading