Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.67% |
11 / 12 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CheckBaseClassFaker | |
91.67% |
11 / 12 |
|
66.67% |
2 / 3 |
8.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| supports | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
5.03 | |||
| fakeFor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\Faker\Fakers; |
| 4 | |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Faker\Interfaces\ApieClassFaker; |
| 7 | use Faker\Generator; |
| 8 | use ReflectionAttribute; |
| 9 | use ReflectionClass; |
| 10 | |
| 11 | /** |
| 12 | * @template T of object |
| 13 | * @implements ApieClassFaker<T> |
| 14 | */ |
| 15 | class CheckBaseClassFaker implements ApieClassFaker |
| 16 | { |
| 17 | private string $method; |
| 18 | |
| 19 | /** |
| 20 | * @var array<class-string<T>, array<int, ReflectionAttribute<FakeMethod>>> $cached |
| 21 | */ |
| 22 | private array $cached = []; |
| 23 | |
| 24 | /** |
| 25 | * @param ReflectionClass<T> $baseClass |
| 26 | */ |
| 27 | public function __construct(private ReflectionClass $baseClass) |
| 28 | { |
| 29 | $this->method = $baseClass->isInterface() ? 'implementsInterface' : 'isSubclassOf'; |
| 30 | } |
| 31 | |
| 32 | public function supports(ReflectionClass $class): bool |
| 33 | { |
| 34 | if (isset($this->cached[$class->name])) { |
| 35 | return (bool) $this->cached[$class->name]; |
| 36 | } |
| 37 | if ($class->{$this->method}($this->baseClass)) { |
| 38 | $parent = $class; |
| 39 | do { |
| 40 | $parent = $parent->getParentClass(); |
| 41 | if ($parent && $parent->getAttributes(FakeMethod::class)) { |
| 42 | $this->cached[$class->name] = $parent->getAttributes(FakeMethod::class); |
| 43 | return true; |
| 44 | } |
| 45 | } while ($parent); |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | public function fakeFor(Generator $generator, ReflectionClass $class): object |
| 51 | { |
| 52 | return UseFakeMethodFaker::runFake($generator, $class, $this->cached[$class->name] ?? []); |
| 53 | } |
| 54 | } |