AnnotationDefinitionProvider::getDefinition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
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 1
    public function registerProcessor(AnnotationProcessorInterface $processor)
34
    {
35 1
        $this->processors[] = $processor;
36 1
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 4
    public function getDefinition(string $class): Definition
42
    {
43 4
        if (! isset($this->definitionCache[$class])) {
44 4
            $reflection = new \ReflectionClass($class);
45
46 4
            $this->definitionCache[$class] = $this->createDefinition($reflection);
47
        }
48
49 4
        return $this->definitionCache[$class];
50
    }
51
52
    /**
53
     * Create definition for given class
54
     *
55
     * @param  \ReflectionClass $reflection
56
     * @return Definition
57
     */
58 4
    protected function createDefinition(\ReflectionClass $reflection): Definition
59
    {
60 4
        $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 4
        foreach ($this->processors as $processor)
63
        {
64 1
            $processor->process($reflection, $definition);
65
        }
66
67 4
        $parent = $reflection->getParentClass();
68
69 4
        if ($parent !== false) {
70 1
            $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
        }
72
73 4
        foreach ($reflection->getTraits() as $trait)
74
        {
75 1
            $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
        }
77
78 4
        return $definition;
79
    }
80
}