Passed
Push — master ( fa352d...0eb7bd )
by BruceScrutinizer
02:09
created

PhpFileParser::createResultCollector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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.  */
0 ignored issues
show
Bug introduced by
The type PhpParser\NodeTraverserInterface. was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nodeTraverserInterface of type PhpParser\NodeTraverserInterface is incompatible with the declared type PhpParser\NodeTraverserInterface. of property $traverser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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