-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeCodeSampleProvider.php
More file actions
52 lines (37 loc) · 1.41 KB
/
NodeCodeSampleProvider.php
File metadata and controls
52 lines (37 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Rector\PhpParserNodesDocs;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use Rector\PhpParserNodesDocs\Finder\PhpFilesFinder;
use Rector\PhpParserNodesDocs\ValueObject\NodeCodeSample;
use Webmozart\Assert\Assert;
final class NodeCodeSampleProvider
{
private Standard $standardPrinter;
public function __construct(
) {
$this->standardPrinter = new Standard();
}
/**
* @return array<class-string<Node>, array<int, NodeCodeSample>>
*/
public function provide(): array
{
$phpFileInfos = PhpFilesFinder::find([__DIR__ . '/../snippet']);
$nodeCodeSamplesByNodeClass = [];
foreach ($phpFileInfos as $phpFileInfo) {
/** @var Node $node */
$node = include $phpFileInfo->getRealPath();
$errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFileInfo, Node::class);
Assert::isInstanceOf($node, Node::class, $errorMessage);
/** @var string $fileContents */
$fileContents = $phpFileInfo->getContents();
$nodeClass = $node::class;
$printedContent = $this->standardPrinter->prettyPrint([$node]);
$nodeCodeSamplesByNodeClass[$nodeClass][] = new NodeCodeSample($fileContents, $printedContent);
}
ksort($nodeCodeSamplesByNodeClass);
return $nodeCodeSamplesByNodeClass;
}
}