Passed
Push — master ( 63f785...e28c78 )
by Michael
02:34
created

MergedProvider::addProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition;
5
use Mikemirten\Component\JsonApi\Mapper\Definition\Exception\DefinitionNotFoundException;
6
7
/**
8
 * A definition provider aggregates a number of definition providers
9
 * Merges definitions from registered providers
10
 *
11
 * @package Mikemirten\Component\JsonApi\Mapper\Definition
12
 */
13
class MergedProvider implements DefinitionProviderInterface
14
{
15
    /**
16
     * @var DefinitionProviderInterface[]
17
     */
18
    protected $providers = [];
19
20
    /**
21
     * @var Definition[]
22
     */
23
    protected $definitions = [];
24
25
    /**
26
     * Add definition provider
27
     *
28
     * @param DefinitionProviderInterface $provider
29
     */
30 1
    public function addProvider(DefinitionProviderInterface $provider)
31
    {
32 1
        $this->providers[] = $provider;
33 1
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2 View Code Duplication
    public function getDefinition(string $class): Definition
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40 2
        if (! isset($this->definitions[$class])) {
41 2
            $this->definitions[$class] = $this->createDefinition($class);
42
        }
43
44 2
        return $this->definitions[$class];
45
    }
46
47
    /**
48
     * Create definition
49
     *
50
     * @param  string $class
51
     * @return Definition
52
     */
53 2
    public function createDefinition(string $class): Definition
54
    {
55 2
        $definition = new Definition($class);
56
57 2
        foreach ($this->providers as $provider)
58
        {
59
            try {
60 1
                $definitionPartial = $provider->getDefinition($class);
61
            } catch (DefinitionNotFoundException $exception) {
62
                continue;
63
            }
64
65 1
            $definition->merge($definitionPartial);
0 ignored issues
show
Documentation introduced by
$definitionPartial 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...
66
        }
67
68 2
        return $definition;
69
    }
70
}