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

MergedProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 8
loc 58
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addProvider() 0 4 1
A getDefinition() 8 8 2
A createDefinition() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}