Passed
Pull Request — master (#38)
by Rustam
02:20
created

AbstractClassifier::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Classifier;
6
7
use Symfony\Component\Finder\Finder;
8
9
abstract class AbstractClassifier implements ClassifierInterface
10
{
11
    /**
12
     * @var string[]
13
     */
14
    protected array $interfaces = [];
15
    /**
16
     * @var string[]
17
     */
18
    protected array $attributes = [];
19
    /**
20
     * @psalm-var class-string
21
     */
22
    protected ?string $parentClass = null;
23
    /**
24
     * @var string[]
25
     */
26
    protected array $directories;
27
28 26
    public function __construct(string $directory, string ...$directories)
29
    {
30 26
        $this->directories = [$directory, ...array_values($directories)];
0 ignored issues
show
Documentation Bug introduced by
array($directory, array_values($directories)) is of type array<integer,array|string>, but the property $directories was declared to be of type string[]. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
31
    }
32
33
    /**
34
     * @psalm-param class-string ...$interfaces
35
     */
36 20
    public function withInterface(string ...$interfaces): self
37
    {
38 20
        $new = clone $this;
39 20
        array_push($new->interfaces, ...array_values($interfaces));
40
41 20
        return $new;
42
    }
43
44
    /**
45
     * @psalm-param class-string $parentClass
46
     */
47 2
    public function withParentClass(string $parentClass): self
48
    {
49 2
        $new = clone $this;
50 2
        $new->parentClass = $parentClass;
51 2
        return $new;
52
    }
53
54
    /**
55
     * @psalm-param class-string ...$attributes
56
     */
57 8
    public function withAttribute(string ...$attributes): self
58
    {
59 8
        $new = clone $this;
60 8
        array_push($new->attributes, ...array_values($attributes));
61
62 8
        return $new;
63
    }
64
65
    /**
66
     * @psalm-return iterable<class-string>
67
     */
68 26
    public function find(): iterable
69
    {
70 26
        if (empty($this->interfaces) && empty($this->attributes) && $this->parentClass === null) {
71 6
            return [];
72
        }
73
74 20
        yield from $this->getAvailableClasses();
75
    }
76
77 20
    protected function getFiles(): Finder
78
    {
79 20
        return (new Finder())
80 20
            ->in($this->directories)
81 20
            ->name('*.php')
82 20
            ->sortByName()
83 20
            ->files();
84
    }
85
86
    /**
87
     * @return iterable<class-string>
88
     */
89
    abstract protected function getAvailableClasses(): iterable;
90
}
91