1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace NamespaceProtector\Parser; |
4
|
|
|
|
5
|
|
|
use PhpParser\Parser; |
6
|
|
|
use NamespaceProtector\Result\Result; |
7
|
|
|
use PhpParser\NodeTraverserInterface; |
8
|
|
|
use NamespaceProtector\Result\ResultParser; |
9
|
|
|
use NamespaceProtector\Common\PathInterface; |
10
|
|
|
use NamespaceProtector\Result\ResultCollector; |
11
|
|
|
use NamespaceProtector\Result\ResultParserInterface; |
12
|
|
|
use NamespaceProtector\Result\ResultCollectorReadable; |
13
|
|
|
use NamespaceProtector\Exception\NamespaceProtectorExceptionInterface; |
14
|
|
|
|
15
|
|
|
final class PhpFileParser implements ParserInterface |
16
|
|
|
{ |
17
|
|
|
private const ONLY_ONE_ENTRY = 1; |
18
|
|
|
|
19
|
|
|
/** @var \PhpParser\Parser */ |
20
|
|
|
private $parser; |
21
|
|
|
|
22
|
|
|
/** @var \PhpParser\NodeTraverserInterface. */ |
|
|
|
|
23
|
|
|
private $traverser; |
24
|
|
|
|
25
|
|
|
/** @var ResultCollector */ |
26
|
|
|
private $resultCollector; |
27
|
|
|
|
28
|
|
|
/** @var \Psr\SimpleCache\CacheInterface */ |
29
|
|
|
private $cache; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
\Psr\SimpleCache\CacheInterface $cache, |
33
|
|
|
NodeTraverserInterface $nodeTraverserInterface, |
34
|
|
|
Parser $parser, |
35
|
|
|
ResultCollector $resultCollector |
36
|
|
|
) { |
37
|
|
|
$this->cache = $cache; |
38
|
|
|
$this->traverser = $nodeTraverserInterface; |
|
|
|
|
39
|
|
|
$this->resultCollector = $resultCollector; |
40
|
|
|
$this->parser = $parser; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function parseFile(PathInterface $pathFile): ResultParserInterface |
44
|
|
|
{ |
45
|
|
|
$this->resultCollector->emptyResult(); |
46
|
|
|
$this->resultCollector->addResult(new Result('Process file: ' . $pathFile() . PHP_EOL)); |
47
|
|
|
|
48
|
|
|
$ast = $this->fetchAstAfterParse($pathFile); |
49
|
|
|
$this->traverser->traverse($ast); |
50
|
|
|
$this->emptyIfOneResult(); |
51
|
|
|
|
52
|
|
|
return new ResultParser($this->getListResult()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getListResult(): ResultCollectorReadable |
56
|
|
|
{ |
57
|
|
|
return new ResultCollectorReadable($this->resultCollector); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function emptyIfOneResult(): void |
61
|
|
|
{ |
62
|
|
|
if (\count($this->resultCollector->get()) === self::ONLY_ONE_ENTRY) { |
63
|
|
|
$this->resultCollector->emptyResult(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array<mixed> |
69
|
|
|
*/ |
70
|
|
|
private function fetchAstAfterParse(PathInterface $pathFile): array |
71
|
|
|
{ |
72
|
|
|
$code = $pathFile(); |
73
|
|
|
$keyEntryForCache = sha1($code) . '.' . base64_encode($pathFile()); |
74
|
|
|
|
75
|
|
|
if (!$this->cache->has($keyEntryForCache)) { |
76
|
|
|
$code = \file_get_contents($pathFile()); |
77
|
|
|
if ($code === false) { |
78
|
|
|
throw new \RuntimeException(NamespaceProtectorExceptionInterface::MSG_PLAIN_ERROR_FILE_GET_CONTENT); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$ast = $this->parser->parse($code); |
82
|
|
|
$this->cache->set($keyEntryForCache, $ast); |
83
|
|
|
|
84
|
|
|
return $ast ?? []; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->cache->get($keyEntryForCache, []); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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