Passed
Pull Request — master (#40)
by Alexander
09:55 queued 07:23
created

AbstractClassifier::__construct()   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 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 2
b 1
f 0
nc 1
nop 2
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 Yiisoft\Classifier;
6
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * Base implementation for {@see ClassifierInterface} with common filters.
11
 */
12
abstract class AbstractClassifier implements ClassifierInterface
13
{
14
    /**
15
     * @var string[]
16
     */
17
    protected array $interfaces = [];
18
    /**
19
     * @var string[]
20
     */
21
    protected array $attributes = [];
22
    /**
23
     * @psalm-var class-string
24
     */
25
    protected ?string $parentClass = null;
26
    /**
27
     * @var string[]
28
     */
29
    protected array $directories;
30
31 28
    public function __construct(string $directory, string ...$directories)
32
    {
33 28
        $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...
34
    }
35
36
    /**
37
     * @psalm-param class-string ...$interfaces
38
     */
39 22
    public function withInterface(string ...$interfaces): self
40
    {
41 22
        $new = clone $this;
42 22
        array_push($new->interfaces, ...array_values($interfaces));
43
44 22
        return $new;
45
    }
46
47
    /**
48
     * @psalm-param class-string $parentClass
49
     */
50 2
    public function withParentClass(string $parentClass): self
51
    {
52 2
        $new = clone $this;
53 2
        $new->parentClass = $parentClass;
54 2
        return $new;
55
    }
56
57
    /**
58
     * @psalm-param class-string ...$attributes
59
     */
60 8
    public function withAttribute(string ...$attributes): self
61
    {
62 8
        $new = clone $this;
63 8
        array_push($new->attributes, ...array_values($attributes));
64
65 8
        return $new;
66
    }
67
68
    /**
69
     * @psalm-return iterable<class-string>
70
     */
71 28
    public function find(): iterable
72
    {
73 28
        if (empty($this->interfaces) && empty($this->attributes) && $this->parentClass === null) {
74 6
            return [];
75
        }
76
77 22
        yield from $this->getAvailableClasses();
78
    }
79
80 22
    protected function getFiles(): Finder
81
    {
82 22
        return (new Finder())
83 22
            ->in($this->directories)
84 22
            ->name('*.php')
85 22
            ->sortByName()
86 22
            ->files();
87
    }
88
89
    /**
90
     * @return iterable<class-string>
91
     */
92
    abstract protected function getAvailableClasses(): iterable;
93
}
94