Passed
Push — main ( bf003e...0239c9 )
by Jeroen
01:37
created

NikicParserClassReflector::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\FileClassReflector\NikicParser;
6
7
use Jerowork\FileClassReflector\ClassReflector;
8
use Jerowork\FileClassReflector\FileFinder\FileFinder;
9
use PhpParser\NodeTraverser;
10
use PhpParser\Parser;
11
use ReflectionClass;
12
13
final class NikicParserClassReflector implements ClassReflector
14
{
15
    /**
16
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type Jerowork\FileClassReflector\NikicParser\list 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...
17
     */
18
    private array $files = [];
19
20
    /**
21
     * @var list<ReflectionClass>
22
     */
23
    private array $classes = [];
24
25 3
    public function __construct(
26
        private FileFinder $fileFinder,
27
        private Parser $parser,
28
        private NodeTraverser $nodeTraverser,
29
    ) {
30 3
    }
31
32 1
    public function addFile(string ...$files) : self
33
    {
34 1
        $this->files = [...$this->files, ...array_values($files)];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->files, array_values($files)) of type array<integer,array> is incompatible with the declared type Jerowork\FileClassReflector\NikicParser\list of property $files.

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...
35
36 1
        return $this;
37
    }
38
39 2
    public function addDirectory(string ...$directories) : self
40
    {
41 2
        $this->files = [...$this->files, ...$this->fileFinder->getFiles(...$directories)];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->files, $thi...getFiles($directories)) of type array<integer,array|string[]> is incompatible with the declared type Jerowork\FileClassReflector\NikicParser\list of property $files.

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...
42
43 2
        return $this;
44
    }
45
46 1
    public function reflect() : self
47
    {
48 1
        $this->nodeTraverser->addVisitor(
49 1
            $visitor = new FqcnNodeVisitor(),
50 1
        );
51
52 1
        foreach ($this->files as $file) {
53 1
            $ast = $this->parser->parse((string) file_get_contents($file));
54
55 1
            if ($ast === null) {
56
                continue;
57
            }
58
59 1
            $this->nodeTraverser->traverse($ast);
60
61 1
            $fqcn = $visitor->getFqcn();
62
63 1
            if ($fqcn === null) {
64
                continue;
65
            }
66
67 1
            $this->classes[] = new ReflectionClass($fqcn);
68
        }
69
70 1
        return $this;
71
    }
72
73 2
    public function getFiles() : array
74
    {
75 2
        return $this->files;
76
    }
77
78 1
    public function getClasses() : array
79
    {
80 1
        return $this->classes;
81
    }
82
}
83