Skip to content

Commit eb899e3

Browse files
committed
Create SniffFinder for discovering sniff and violation files
1 parent 7b240c9 commit eb899e3

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\SniffFinder;
5+
6+
use App\Parser\SniffParser;
7+
use App\Parser\ViolationParser;
8+
use App\Value\Standard;
9+
use GlobIterator;
10+
use Traversable;
11+
12+
class FilesystemSniffFinder implements SniffFinder
13+
{
14+
public function getSniffs(Standard $standard): Traversable
15+
{
16+
$parser = new SniffParser();
17+
$glob = new GlobIterator($standard->getCodeLocation() . 'Sniffs/*/*Sniff.php');
18+
foreach ($glob as $fileInfo) {
19+
yield $parser->parse($fileInfo->getPathname());
20+
}
21+
}
22+
23+
public function getViolations(Standard $standard): Traversable
24+
{
25+
$parser = new ViolationParser();
26+
$glob = new GlobIterator($standard->getCodeLocation() . 'Docs/*/*Standard/*.xml');
27+
foreach ($glob as $fileInfo) {
28+
yield $parser->parse($fileInfo->getPathname());
29+
}
30+
}
31+
}

src/SniffFinder/SniffFinder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\SniffFinder;
5+
6+
use App\Value\Sniff;
7+
use App\Value\Standard;
8+
use App\Value\Violation;
9+
use Traversable;
10+
11+
interface SniffFinder
12+
{
13+
/**
14+
* @return Traversable<Sniff>
15+
*/
16+
public function getSniffs(Standard $standard): Traversable;
17+
18+
/**
19+
* @return Traversable<Violation>
20+
*/
21+
public function getViolations(Standard $standard): Traversable;
22+
}

src/Value/Standard.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Value;
5+
6+
7+
use Assert\Assert;
8+
9+
class Standard
10+
{
11+
private string $codeLocation;
12+
13+
public function __construct(string $codeLocation)
14+
{
15+
Assert::that($codeLocation)
16+
->endsWith('/');
17+
18+
$this->codeLocation = $codeLocation;
19+
}
20+
21+
public function getCodeLocation(): string
22+
{
23+
return $this->codeLocation;
24+
}
25+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\SniffFinder;
5+
6+
use App\SniffFinder\FilesystemSniffFinder;
7+
use App\Value\Sniff;
8+
use App\Value\Standard;
9+
use App\Value\Urls;
10+
use App\Value\Violation;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\Filesystem\Filesystem;
13+
14+
/** @covers \App\SniffFinder\FilesystemSniffFinder */
15+
class FilesystemSniffFinderTest extends TestCase
16+
{
17+
private const PHP_SNIFF_PATH = 'var/tests/src/Standard/Sniffs/Category/MySniff.php';
18+
private const XML_SNIFF_PATH = 'var/tests/src/Standard/Docs/Category/MyStandard.xml';
19+
private const XML_VIOLATION_PATH = 'var/tests/src/Standard/Docs/Category/MyStandard/ErrorCode.xml';
20+
21+
private FilesystemSniffFinder $finder;
22+
23+
protected function setUp(): void
24+
{
25+
$this->finder = new FilesystemSniffFinder();
26+
}
27+
28+
/** @test */
29+
public function getSniffs()
30+
{
31+
$this->writeSniffPhp();
32+
$this->writeSniffXml();
33+
34+
self::assertEquals(
35+
[
36+
new Sniff(
37+
'Standard.Category.My',
38+
'',
39+
[],
40+
new Urls([]),
41+
'Description',
42+
[],
43+
[]
44+
)
45+
],
46+
iterator_to_array($this->finder->getSniffs(new Standard(
47+
'var/tests/src/Standard/'
48+
)))
49+
);
50+
}
51+
52+
/** @test */
53+
public function getViolations()
54+
{
55+
$this->writeViolationXml();
56+
57+
self::assertEquals(
58+
[
59+
new Violation(
60+
'Standard.Category.My.ErrorCode',
61+
'Description',
62+
[],
63+
new Urls([])
64+
)
65+
],
66+
iterator_to_array($this->finder->getViolations(new Standard(
67+
'var/tests/src/Standard/'
68+
)))
69+
);
70+
}
71+
72+
private function writeSniffPhp(): void
73+
{
74+
$content = '<?php
75+
class SniffName {}
76+
';
77+
(new Filesystem())->dumpFile(self::PHP_SNIFF_PATH, $content);
78+
}
79+
80+
private function writeSniffXml(): void
81+
{
82+
$content = <<<XML
83+
<documentation title="Title">
84+
<standard>Description</standard>
85+
</documentation>
86+
XML;
87+
(new Filesystem())->dumpFile(self::XML_SNIFF_PATH, $content);
88+
}
89+
90+
private function writeViolationXml()
91+
{
92+
$content = <<<XML
93+
<documentation title="Title">
94+
<standard>Description</standard>
95+
</documentation>
96+
XML;
97+
(new Filesystem())->dumpFile(self::XML_VIOLATION_PATH, $content);
98+
}
99+
}

0 commit comments

Comments
 (0)