Test Failed
Push — master ( 385400...b007ca )
by Michael
02:12
created

AnnotationDefinitionProvider::getDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition;
5
use Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor\AnnotationProcessorInterface;
6
7
/**
8
 * Annotation Definition Provider based on the Doctrine-Annotation library
9
 *
10
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
11
 */
12
class AnnotationDefinitionProvider implements DefinitionProviderInterface
13
{
14
    /**
15
     * Registered annotation processors
16
     *
17
     * @var AnnotationProcessorInterface[]
18
     */
19
    private $processors = [];
20
21
    /**
22
     * Cache of created definitions
23
     *
24
     * @var array
25
     */
26
    private $definitionCache = [];
27
28
    /**
29
     * Register annotation processor
30
     *
31
     * @param AnnotationProcessorInterface $processor
32
     */
33
    public function registerProcessor(AnnotationProcessorInterface $processor)
34
    {
35
        $this->processors[] = $processor;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getDefinition(string $class): Definition
42
    {
43
        if (! isset($this->definitionCache[$class])) {
44
            $reflection = new \ReflectionClass($class);
45
46
            $this->definitionCache[$class] = $this->createDefinition($reflection);
47
        }
48 18
49
        return $this->definitionCache[$class];
50 18
    }
51 1
52 1
    /**
53 1
     * Create definition for given class
54
     *
55 1
     * @param  \ReflectionClass $reflection
56
     * @return Definition
57 18
     */
58
    protected function createDefinition(\ReflectionClass $reflection): Definition
59
    {
60
        $definition = new Definition($reflection->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
61
62
        foreach ($this->processors as $processor)
63
        {
64
            $processor->process($reflection, $definition);
65
        }
66
67
        $parent = $reflection->getParentClass();
68
69
        if ($parent !== false) {
70
            $definition->merge($this->createDefinition($parent));
0 ignored issues
show
Documentation introduced by
$this->createDefinition($parent) is of type object<Mikemirten\Compon...\Definition\Definition>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71 18
        }
72
73 18
        foreach ($reflection->getTraits() as $trait)
74
        {
75 18
            $definition->merge($this->createDefinition($trait));
0 ignored issues
show
Documentation introduced by
$this->createDefinition($trait) is of type object<Mikemirten\Compon...\Definition\Definition>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76 18
        }
77
78
        return $definition;
79
    }
80
}