Description   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 12
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 12 12 2
A has() 0 4 1
A all() 0 4 1
A set() 0 12 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
3
declare(strict_types=1);
4
5
namespace Psi\Component\Description;
6
7
/**
8
 * Descriptive metadata for objects.
9
 */
10
class Description implements DescriptionInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $descriptors = [];
16
17
    /**
18
     * @var array
19
     */
20
    private $priorities = [];
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 View Code Duplication
    public function get($descriptorKey): DescriptorInterface
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...
26
    {
27
        if (!isset($this->descriptors[$descriptorKey])) {
28
            throw new \InvalidArgumentException(sprintf(
29
                'Descriptor "%s" is not supported',
30
                $descriptorKey,
31
                implode('", "', array_keys($this->descriptors))
32
            ));
33
        }
34
35
        return $this->descriptors[$descriptorKey];
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function has($descriptor): bool
42
    {
43
        return isset($this->descriptors[$descriptor]);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function all(): array
50
    {
51
        return $this->descriptors;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function set(string $key, DescriptorInterface $descriptor, int $priority = 0)
58
    {
59
        // do not overwrite descriptor if a lower priority is given.
60
        if (isset($this->priorities[$key])) {
61
            if ($priority < $this->priorities[$key]) {
62
                return;
63
            }
64
        }
65
66
        $this->descriptors[$key] = $descriptor;
67
        $this->priorities[$key] = $priority;
68
    }
69
}
70