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)]; |
|
|
|
|
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
|
|
|
|
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.