Skip to content

Commit 8fd6a05

Browse files
committed
Complete tests for all VOs; minor renaming & validation
1 parent f5dfc9d commit 8fd6a05

20 files changed

+471
-151
lines changed

composer.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command/GenerateCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
3434

3535
foreach ($this->handler->handle($sniffPath) as $message) {
3636
$output->writeln($message);
37-
};
37+
}
3838

3939
return Command::SUCCESS;
4040
}

src/Generator/MarkdownGenerator.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use App\Value\Property;
88
use App\Value\Sniff;
99
use App\Value\Url;
10-
use App\Value\Urls;
10+
use App\Value\UrlList;
1111
use App\Value\Violation;
1212
use Stringy\Stringy;
1313
use function Stringy\create as s;
@@ -23,7 +23,7 @@ public function createSniffDoc(Sniff $sniff): string
2323
{$this->getDocblock($sniff)}
2424
{$this->getComparisons($sniff->getDiffs())}
2525
{$this->getPublicProperties($sniff->getProperties())}
26-
{$this->getSeeAlso($sniff->getLinks())}
26+
{$this->getSeeAlso($sniff->getUrls())}
2727
{$this->getViolations($sniff->getViolations())}
2828
MD;
2929

@@ -134,13 +134,13 @@ private function getPublicPropertyLines(array $properties): array
134134
/**
135135
* @return string
136136
*/
137-
private function getSeeAlso(Urls $links): string
137+
private function getSeeAlso(UrlList $urls): string
138138
{
139-
if ($links->getUrls() === []) {
139+
if ($urls->toArray() === []) {
140140
return '';
141141
}
142142

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

145145
return <<<MD
146146
## See Also
@@ -153,11 +153,11 @@ private function getSeeAlso(Urls $links): string
153153
/**
154154
* @return string[]
155155
*/
156-
private function getLinkLines(Urls $links): array
156+
private function getLinkLines(UrlList $urls): array
157157
{
158158
return array_map(function (Url $url) {
159159
return "- [$url]($url)";
160-
}, $links->getUrls());
160+
}, $urls->toArray());
161161
}
162162

163163
/**
@@ -205,7 +205,7 @@ public function createViolationDoc(Violation $doc): string
205205
206206
{$this->getComparisons($doc->getDiffs())}
207207
208-
{$this->getSeeAlso($doc->getLinks())}
208+
{$this->getSeeAlso($doc->getUrls())}
209209
MD;
210210
}
211211
}

src/Parser/SniffParser.php

+29-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use App\Value\Property;
99
use App\Value\Sniff;
1010
use App\Value\Url;
11-
use App\Value\Urls;
11+
use App\Value\UrlList;
1212
use GlobIterator;
1313
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
1414
use phpDocumentor\Reflection\DocBlockFactory;
@@ -58,26 +58,47 @@ public function parse(string $phpFilePath, SourceLocator $projectSourceLocator):
5858
$this->getCode($phpFilePath),
5959
$this->getDocBlock($classInfo->getDocComment()),
6060
$this->getProperties($classInfo),
61-
$this->getLinks($classInfo, $xmlUrls),
61+
$this->getUrls($classInfo, $xmlUrls),
6262
$description,
6363
$diffs,
6464
$violations
6565
);
6666
}
6767

68+
private function getSniffClassName(string $phpFilePath): string
69+
{
70+
$parts = $this->getSniffFileParts($phpFilePath);
71+
72+
return "{$parts[0]}\\Sniffs\\{$parts[1]}\\{$parts[2]}Sniff";
73+
}
74+
75+
/**
76+
* @return string[]
77+
*/
78+
private function getSniffFileParts(string $filePath): array
79+
{
80+
$part = '([^\/]*)';
81+
preg_match("/$part\/Sniffs\/$part\/{$part}Sniff\.php/", $filePath, $matches);
82+
if ($matches === []) {
83+
throw NotASniffPath::fromPath($filePath);
84+
}
85+
86+
return array_slice($matches, 1, 3);
87+
}
88+
6889
/**
6990
* @return Url[]
7091
*/
7192
private function getXmlUrls(SimpleXMLElement $xml): array
7293
{
73-
$links = [];
94+
$urls = [];
7495
foreach ($xml->link as $link) {
75-
$links[] = new Url(
96+
$urls[] = new Url(
7697
(string)s((string)$link)->trim()
7798
);
7899
}
79100

80-
return $links;
101+
return $urls;
81102
}
82103

83104
private function getDescription(SimpleXMLElement $xml): string
@@ -174,10 +195,10 @@ private function getPropertyDescription(Roave\ReflectionProperty $property): str
174195
/**
175196
* @param Url[] $xmlUrls
176197
*/
177-
private function getLinks(ReflectionClass $classInfo, array $xmlUrls): Urls
198+
private function getUrls(ReflectionClass $classInfo, array $xmlUrls): UrlList
178199
{
179200
if ($classInfo->getDocComment() === '') {
180-
return new Urls([]);
201+
return new UrlList([]);
181202
}
182203

183204
$links = DocBlockFactory::createInstance()
@@ -188,27 +209,6 @@ private function getLinks(ReflectionClass $classInfo, array $xmlUrls): Urls
188209
return new Url($url);
189210
}, $links);
190211

191-
return new Urls(array_merge($urls, $xmlUrls));
192-
}
193-
194-
private function getSniffClassName(string $phpFilePath): string
195-
{
196-
$parts = $this->getSniffFileParts($phpFilePath);
197-
198-
return "{$parts[0]}\\Sniffs\\{$parts[1]}\\{$parts[2]}Sniff";
199-
}
200-
201-
/**
202-
* @return string[]
203-
*/
204-
private function getSniffFileParts(string $filePath): array
205-
{
206-
$part = '([^\/]*)';
207-
preg_match("/$part\/Sniffs\/$part\/{$part}Sniff\.php/", $filePath, $matches);
208-
if ($matches === []) {
209-
throw NotASniffPath::fromPath($filePath);
210-
}
211-
212-
return array_slice($matches, 1, 3);
212+
return new UrlList(array_merge($urls, $xmlUrls));
213213
}
214214
}

src/Parser/ViolationParser.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use App\Parser\Exception\NotAViolationPath;
77
use App\Value\Diff;
88
use App\Value\Url;
9-
use App\Value\Urls;
9+
use App\Value\UrlList;
1010
use App\Value\Violation;
1111
use SimpleXMLElement;
1212
use function Stringy\create as s;
@@ -21,7 +21,7 @@ public function parse(string $xmlFilePath): Violation
2121
$this->getErrorCode($xmlFilePath),
2222
$this->getDescription($xml),
2323
$this->getDiffs($xml),
24-
$this->getLinks($xml)
24+
$this->getUrls($xml)
2525
);
2626
}
2727

@@ -57,15 +57,15 @@ private function getDiffs(SimpleXMLElement $xml): array
5757
return $comparisons;
5858
}
5959

60-
private function getLinks(SimpleXMLElement $xml): Urls
60+
private function getUrls(SimpleXMLElement $xml): UrlList
6161
{
62-
$links = [];
62+
$urls = [];
6363
foreach ($xml->link as $link) {
64-
$links[] = new Url(
64+
$urls[] = new Url(
6565
(string)s((string)$link)->trim()
6666
);
6767
}
6868

69-
return new Urls($links);
69+
return new UrlList($urls);
7070
}
7171
}

src/Value/Sniff.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace App\Value;
55

6+
use Assert\Assert;
7+
68
class Sniff
79
{
810
private string $code;
@@ -11,7 +13,7 @@ class Sniff
1113
* @var Property[]
1214
*/
1315
private array $properties;
14-
private Urls $links;
16+
private UrlList $urls;
1517
private string $description;
1618
/**
1719
* @var Diff[]
@@ -31,16 +33,28 @@ public function __construct(
3133
string $code,
3234
string $docblock,
3335
array $properties,
34-
Urls $links,
36+
UrlList $urls,
3537
string $description,
3638
array $diffs,
3739
array $violations
3840
)
3941
{
42+
Assert::that($code)
43+
->notBlank();
44+
45+
Assert::thatAll($properties)
46+
->isInstanceOf(Property::class);
47+
48+
Assert::thatAll($diffs)
49+
->isInstanceOf(Diff::class);
50+
51+
Assert::thatAll($violations)
52+
->isInstanceOf(Violation::class);
53+
4054
$this->code = $code;
4155
$this->docblock = $docblock;
4256
$this->properties = array_values($properties);
43-
$this->links = $links;
57+
$this->urls = $urls;
4458
$this->description = $description;
4559
$this->diffs = $diffs;
4660
$this->violations = $violations;
@@ -64,9 +78,9 @@ public function getProperties(): array
6478
return $this->properties;
6579
}
6680

67-
public function getLinks(): Urls
81+
public function getUrls(): UrlList
6882
{
69-
return $this->links;
83+
return $this->urls;
7084
}
7185

7286
public function getDescription(): string

src/Value/Url.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
namespace App\Value;
55

6+
use Assert\Assert;
7+
68
class Url
79
{
810
private string $url;
911

1012
public function __construct(string $url)
1113
{
14+
Assert::that($url)
15+
->url();
16+
1217
$this->url = $url;
1318
}
1419

src/Value/Urls.php renamed to src/Value/UrlList.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Collection of unique URLs.
88
*/
9-
class Urls
9+
class UrlList
1010
{
1111
/**
1212
* @var Url[]
@@ -32,7 +32,7 @@ public function __construct(array $urls)
3232
/**
3333
* @return Url[]
3434
*/
35-
public function getUrls(): array
35+
public function toArray(): array
3636
{
3737
return $this->urls;
3838
}

src/Value/Violation.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace App\Value;
55

6+
use Assert\Assert;
7+
68
class Violation
79
{
810
private string $code;
@@ -11,17 +13,23 @@ class Violation
1113
* @var Diff[]
1214
*/
1315
private array $diffs;
14-
private Urls $links;
16+
private UrlList $urls;
1517

1618
/**
1719
* @param Diff[] $diffs
1820
*/
19-
public function __construct(string $code, string $description, array $diffs, Urls $links)
21+
public function __construct(string $code, string $description, array $diffs, UrlList $urls)
2022
{
23+
Assert::that($code)
24+
->notBlank();
25+
26+
Assert::thatAll($diffs)
27+
->isInstanceOf(Diff::class);
28+
2129
$this->code = $code;
2230
$this->description = $description;
2331
$this->diffs = $diffs;
24-
$this->links = $links;
32+
$this->urls = $urls;
2533
}
2634

2735
public function getCode(): string
@@ -42,8 +50,8 @@ public function getDiffs(): array
4250
return $this->diffs;
4351
}
4452

45-
public function getLinks(): Urls
53+
public function getUrls(): UrlList
4654
{
47-
return $this->links;
55+
return $this->urls;
4856
}
4957
}

0 commit comments

Comments
 (0)