1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NamespaceProtector\Parser; |
6
|
|
|
|
7
|
|
|
use PhpParser\Parser; |
8
|
|
|
use PhpParser\NodeTraverserInterface; |
9
|
|
|
use NamespaceProtector\Result\ResultParser; |
10
|
|
|
use NamespaceProtector\Common\PathInterface; |
11
|
|
|
use NamespaceProtector\Result\ResultCollected; |
12
|
|
|
use NamespaceProtector\Result\ResultInterface; |
13
|
|
|
use NamespaceProtector\Result\ResultProcessedFile; |
14
|
|
|
use NamespaceProtector\Result\ResultParserInterface; |
15
|
|
|
use NamespaceProtector\Result\ResultCollectedReadable; |
16
|
|
|
use NamespaceProtector\Result\ResultProcessorInterface; |
17
|
|
|
use NamespaceProtector\Exception\NamespaceProtectorExceptionInterface; |
18
|
|
|
use NamespaceProtector\Parser\Node\NamespaceProtectorVisitorInterface; |
19
|
|
|
|
20
|
|
|
final class PhpFileParser implements ParserInterface |
21
|
|
|
{ |
22
|
|
|
/** @var \PhpParser\Parser */ |
23
|
|
|
private $parser; |
24
|
|
|
|
25
|
|
|
/** @var \PhpParser\NodeTraverserInterface. */ |
|
|
|
|
26
|
|
|
private $traverser; |
27
|
|
|
|
28
|
|
|
/** @var ResultCollected */ |
29
|
|
|
private $resultCollector; |
30
|
|
|
|
31
|
|
|
/** @var \Psr\SimpleCache\CacheInterface */ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** @var NamespaceProtectorVisitorInterface */ |
35
|
|
|
private $namespaceProtectorVisitor; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
\Psr\SimpleCache\CacheInterface $cache, |
39
|
|
|
NodeTraverserInterface $nodeTraverserInterface, |
40
|
|
|
NamespaceProtectorVisitorInterface $visitor, |
41
|
|
|
Parser $parser |
42
|
|
|
) { |
43
|
|
|
$this->cache = $cache; |
44
|
|
|
$this->traverser = $nodeTraverserInterface; |
|
|
|
|
45
|
|
|
$this->parser = $parser; |
46
|
|
|
$this->namespaceProtectorVisitor = $visitor; |
47
|
|
|
$this->resultCollector = $this->createResultCollector(); |
48
|
|
|
$nodeTraverserInterface->addVisitor($visitor); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return ResultCollected |
53
|
|
|
*/ |
54
|
|
|
private function createResultCollector(): ResultCollected |
55
|
|
|
{ |
56
|
|
|
return new ResultCollected(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function parseFile(PathInterface $pathFile): void |
60
|
|
|
{ |
61
|
|
|
$this->resultCollector = $this->createResultCollector(); |
62
|
|
|
|
63
|
|
|
$ast = $this->fetchAstAfterParse($pathFile); |
64
|
|
|
$this->traverser->traverse($ast); |
65
|
|
|
|
66
|
|
|
$visitorCollectorResult = $this->namespaceProtectorVisitor->getStoreProcessNodeResult(); |
67
|
|
|
$processFileResult = new ResultProcessedFile($pathFile()); |
68
|
|
|
|
69
|
|
|
/** @var ResultInterface $singleResult */ |
70
|
|
|
foreach ($visitorCollectorResult as $singleResult) { |
71
|
|
|
$processFileResult->add($singleResult); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->resultCollector->addResult($processFileResult); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getListResult(): ResultParserInterface |
78
|
|
|
{ |
79
|
|
|
/** @var ResultCollectedReadable<ResultProcessorInterface> */ |
80
|
|
|
$collected = new ResultCollectedReadable($this->resultCollector); |
81
|
|
|
return new ResultParser($collected); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array<mixed> |
86
|
|
|
*/ |
87
|
|
|
private function fetchAstAfterParse(PathInterface $pathFile): array |
88
|
|
|
{ |
89
|
|
|
$code = $pathFile(); |
90
|
|
|
$keyEntryForCache = sha1($code) . '.' . base64_encode($pathFile()); |
91
|
|
|
|
92
|
|
|
if (!$this->cache->has($keyEntryForCache)) { |
93
|
|
|
$code = \file_get_contents($pathFile()); |
94
|
|
|
if ($code === false) { |
95
|
|
|
throw new \RuntimeException(NamespaceProtectorExceptionInterface::MSG_PLAIN_ERROR_FILE_GET_CONTENT); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$ast = $this->parser->parse($code); |
99
|
|
|
$this->cache->set($keyEntryForCache, $ast); |
100
|
|
|
|
101
|
|
|
return $ast ?? []; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->cache->get($keyEntryForCache, []); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths