Skip to content

Commit 5405484

Browse files
committed
misc
1 parent 671918b commit 5405484

File tree

6 files changed

+27
-72
lines changed

6 files changed

+27
-72
lines changed

bin/dump-nodes.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use PhpParser\PrettyPrinter\Standard;
6-
use Rector\PhpParserNodesDocs\Finder\PhpFilesFinder;
75
use Rector\PhpParserNodesDocs\NodeCodeSampleProvider;
86
use Rector\PhpParserNodesDocs\NodeInfosFactory;
97
use Rector\PhpParserNodesDocs\Printer\MarkdownNodeInfosPrinter;
@@ -12,17 +10,15 @@
1210
require_once __DIR__ . '/../vendor/autoload.php';
1311

1412
$markdownNodeInfosPrinter = new MarkdownNodeInfosPrinter();
15-
$nodeInfosFactory = new NodeInfosFactory(
16-
new NodeCodeSampleProvider(new Standard(), new PhpFilesFinder()),
17-
new NodeInfoSorter()
18-
);
13+
14+
$nodeInfosFactory = new NodeInfosFactory(new NodeCodeSampleProvider(), new NodeInfoSorter());
1915

2016
$nodeInfos = $nodeInfosFactory->create();
2117
$printedContent = $markdownNodeInfosPrinter->print($nodeInfos);
2218

2319
file_put_contents(getcwd() . '/README.md', $printedContent);
2420

25-
echo sprintf('<info>Documentation for %d nodes was generated to README.md</info>' . PHP_EOL, count($nodeInfos));
21+
echo sprintf('Documentation for %d nodes was generated to README.md' . PHP_EOL . PHP_EOL, count($nodeInfos));
2622

2723
// success
2824
exit(0);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"require": {
1010
"php": "^8.3",
1111
"nikic/php-parser": "^5.6",
12-
"webmozart/assert": "^1.11"
12+
"webmozart/assert": "^1.11",
13+
"symfony/finder": "^7.3"
1314
},
1415
"require-dev": {
1516
"symplify/easy-coding-standard": "^12.6",

phpstan.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,4 @@ parameters:
55
- bin
66
- src
77
- tests
8-
98
treatPhpDocTypesAsCertain: false
10-
11-
ignoreErrors:
12-
-
13-
path: src/Finder/PhpFilesFinder.php
14-
identifier: varTag.nativeType

src/Finder/PhpFilesFinder.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,25 @@
44

55
namespace Rector\PhpParserNodesDocs\Finder;
66

7+
use Symfony\Component\Finder\Finder;
8+
use Symfony\Component\Finder\SplFileInfo;
79
use Webmozart\Assert\Assert;
810

911
final class PhpFilesFinder
1012
{
1113
/**
1214
* @param string[] $paths
13-
* @return string[]
15+
* @return SplFileInfo[]
1416
*/
15-
public function findPhpFiles(array $paths): array
17+
public static function find(array $paths): array
1618
{
17-
Assert::allFileExists($paths);
19+
Assert::allString($paths);
1820

19-
// fallback to config paths
20-
$filePaths = [];
21+
$finder = Finder::create()
22+
->name('*.php')
23+
->sortByName()
24+
->in($paths);
2125

22-
foreach ($paths as $path) {
23-
if (is_file($path)) {
24-
$filePaths[] = $path;
25-
} else {
26-
$currentFilePaths = $this->findFilesUsingGlob($path);
27-
$filePaths = array_merge($filePaths, $currentFilePaths);
28-
}
29-
}
30-
31-
sort($filePaths);
32-
33-
Assert::allString($filePaths);
34-
Assert::allFileExists($filePaths);
35-
36-
return $filePaths;
37-
}
38-
39-
/**
40-
* @return string[]
41-
*/
42-
private function findFilesUsingGlob(string $directory): array
43-
{
44-
// Search for php files in the current directory
45-
/** @var list<string> $phpFiles */
46-
$phpFiles = glob($directory . '/*.php');
47-
48-
// recursively search in subdirectories
49-
50-
/** @var string[] $subdirectories */
51-
$subdirectories = glob($directory . '/*', GLOB_ONLYDIR);
52-
53-
foreach ($subdirectories as $subdirectory) {
54-
// Merge the results from subdirectories
55-
$phpFiles = array_merge($phpFiles, $this->findFilesUsingGlob($subdirectory));
56-
}
57-
58-
return $phpFiles;
26+
return iterator_to_array($finder->getIterator());
5927
}
6028
}

src/NodeCodeSampleProvider.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,35 @@
1212

1313
final class NodeCodeSampleProvider
1414
{
15+
private Standard $standardPrinter;
16+
1517
public function __construct(
16-
private readonly Standard $standard,
17-
private readonly PhpFilesFinder $phpFilesFinder,
1818
) {
19+
$this->standardPrinter = new Standard();
1920
}
2021

2122
/**
2223
* @return array<class-string<Node>, array<int, NodeCodeSample>>
2324
*/
2425
public function provide(): array
2526
{
26-
$phpFilePaths = $this->phpFilesFinder->findPhpFiles([__DIR__ . '/../snippet']);
27+
$phpFileInfos = PhpFilesFinder::find([__DIR__ . '/../snippet']);
2728

2829
$nodeCodeSamplesByNodeClass = [];
2930

30-
foreach ($phpFilePaths as $phpFilePath) {
31+
foreach ($phpFileInfos as $phpFileInfo) {
3132
/** @var Node $node */
32-
$node = include $phpFilePath;
33-
34-
/** @var string $fileContents */
35-
$fileContents = file_get_contents($phpFilePath);
33+
$node = include $phpFileInfo->getRealPath();
3634

37-
$errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFilePath, Node::class);
35+
$errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFileInfo, Node::class);
3836
Assert::isInstanceOf($node, Node::class, $errorMessage);
3937

38+
/** @var string $fileContents */
39+
$fileContents = $phpFileInfo->getContents();
40+
4041
$nodeClass = $node::class;
4142

42-
$printedContent = $this->standard->prettyPrint([$node]);
43+
$printedContent = $this->standardPrinter->prettyPrint([$node]);
4344

4445
$nodeCodeSamplesByNodeClass[$nodeClass][] = new NodeCodeSample($fileContents, $printedContent);
4546
}

tests/NodeInfosFactoryTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Rector\PhpParserNodesDocs\Tests;
66

7-
use PhpParser\PrettyPrinter\Standard;
87
use PHPUnit\Framework\TestCase;
9-
use Rector\PhpParserNodesDocs\Finder\PhpFilesFinder;
108
use Rector\PhpParserNodesDocs\NodeCodeSampleProvider;
119
use Rector\PhpParserNodesDocs\NodeInfosFactory;
1210
use Rector\PhpParserNodesDocs\Sorter\NodeInfoSorter;
@@ -15,10 +13,7 @@ final class NodeInfosFactoryTest extends TestCase
1513
{
1614
public function test(): void
1715
{
18-
$nodeInfosFactory = new NodeInfosFactory(
19-
new NodeCodeSampleProvider(new Standard(), new PhpFilesFinder()),
20-
new NodeInfoSorter()
21-
);
16+
$nodeInfosFactory = new NodeInfosFactory(new NodeCodeSampleProvider(), new NodeInfoSorter());
2217

2318
$nodeInfos = $nodeInfosFactory->create();
2419

0 commit comments

Comments
 (0)