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\ResultProcessedFileEmpty; |
17
|
|
|
use NamespaceProtector\Result\ResultProcessorInterface; |
18
|
|
|
use NamespaceProtector\Exception\NamespaceProtectorExceptionInterface; |
19
|
|
|
use NamespaceProtector\Parser\Node\NamespaceProtectorVisitorInterface; |
20
|
|
|
use NamespaceProtector\Result\ResultProcessedFileInterface; |
21
|
|
|
|
22
|
|
|
final class PhpFileParser implements ParserInterface |
23
|
|
|
{ |
24
|
|
|
/** @var \PhpParser\Parser */ |
25
|
|
|
private $parser; |
26
|
|
|
|
27
|
|
|
/** @var \PhpParser\NodeTraverserInterface. */ |
|
|
|
|
28
|
|
|
private $traverser; |
29
|
|
|
|
30
|
|
|
/** @var ResultProcessedFileInterface */ |
31
|
|
|
private $processFileResult; |
32
|
|
|
|
33
|
|
|
/** @var \Psr\SimpleCache\CacheInterface */ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
/** @var NamespaceProtectorVisitorInterface */ |
37
|
|
|
private $namespaceProtectorVisitor; |
38
|
|
|
|
39
|
|
|
/** @var PathInterface */ |
40
|
|
|
private $pathFileToParse; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
\Psr\SimpleCache\CacheInterface $cache, |
44
|
|
|
NodeTraverserInterface $nodeTraverserInterface, |
45
|
|
|
NamespaceProtectorVisitorInterface $visitor, |
46
|
|
|
Parser $parser |
47
|
|
|
) { |
48
|
|
|
$this->cache = $cache; |
49
|
|
|
$this->traverser = $nodeTraverserInterface; |
|
|
|
|
50
|
|
|
$this->parser = $parser; |
51
|
|
|
$this->namespaceProtectorVisitor = $visitor; |
52
|
|
|
$this->processFileResult = new ResultProcessedFileEmpty(); |
53
|
|
|
$nodeTraverserInterface->addVisitor($visitor); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function parseFile(PathInterface $pathFile): void |
57
|
|
|
{ |
58
|
|
|
$this->pathFileToParse = $pathFile; |
59
|
|
|
|
60
|
|
|
$ast = $this->fetchAstAfterParse($pathFile); |
61
|
|
|
$this->namespaceProtectorVisitor->clearStoredProcessedResult(); |
62
|
|
|
|
63
|
|
|
$this->traverser->traverse($ast); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getListResult(): ResultParserInterface |
67
|
|
|
{ |
68
|
|
|
if (\count($this->namespaceProtectorVisitor->getStoreProcessedResult()) === 0) { |
69
|
|
|
return new ResultParser(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->processFileResult = new ResultProcessedFile($this->pathFileToParse->get()); |
73
|
|
|
|
74
|
|
|
/** @var ResultInterface $singleConflict */ |
75
|
|
|
foreach ($this->namespaceProtectorVisitor->getStoreProcessedResult() as $singleConflict) { |
76
|
|
|
$this->processFileResult->addConflic($singleConflict); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @var ResultCollectedReadable<ResultProcessorInterface> */ |
80
|
|
|
$collected = new ResultCollectedReadable(new ResultCollected([$this->processFileResult])); |
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