DecoratorAbstract::decorate()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 4
1
<?php
2
3
namespace Er1z\FakeMock\Decorator;
4
5
use Er1z\FakeMock\Decorator\AssertDecorator\AssertDecoratorInterface;
6
use Er1z\FakeMock\Metadata\FieldMetadata;
7
use Symfony\Component\Validator\Constraint;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Constraint 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...
8
9
abstract class DecoratorAbstract implements DecoratorInterface
10
{
11
    /**
12
     * @var DecoratorInterface[]
13
     */
14
    protected $decorators = [];
15
16 36
    public function decorate(
17
        &$value, FieldMetadata $field, ?string $group = null
18
    ): bool {
19 36
        $asserts = $field->annotations->findAllBy(Constraint::class);
20
21 36
        foreach ($asserts as $a) {
22 12
            $refl = new \ReflectionClass($a);
23 12
            $basename = $refl->getShortName();
24
25 12
            $result = true;
26 12
            if ($decorator = $this->getDecorator($basename)) {
27 9
                $result = $decorator->decorate($value, $field, $a, $group);
28
            }
29
30 12
            if (!$result) {
31 9
                break;
32
            }
33
        }
34
35 36
        return true;
36
    }
37
38
    abstract protected function getDecoratorFqcn($simpleClassName);
39
40 12
    protected function getDecorator($assertClass): ?AssertDecoratorInterface
41
    {
42 12
        if (empty($this->decorators[$assertClass])) {
43 12
            $decoratorFqcn = $this->getDecoratorFqcn($assertClass);
44 12
            $this->decorators[$assertClass] = class_exists($decoratorFqcn) ? new $decoratorFqcn() : null;
45
        }
46
47 12
        return $this->decorators[$assertClass];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->decorators[$assertClass] returns the type Er1z\FakeMock\Decorator\DecoratorInterface which is incompatible with the type-hinted return Er1z\FakeMock\Decorator\...DecoratorInterface|null.
Loading history...
48
    }
49
}
50