Completed
Push — master ( 71f966...a7254e )
by BruceScrutinizer
08:15
created

PhpFileParser::emptyLogIfNoErrorEntry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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.  */
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...
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;
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...
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