Completed
Push — master ( f1592e...b3630f )
by Daniel
03:25 queued 01:30
created

Capabilities   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 8.96 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 6
loc 67
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B create() 6 28 2
A getSupportedComparators() 0 4 1
A canSetParent() 0 4 1
A canQueryCount() 0 4 1
A canQueryJoin() 0 4 1
A canQuerySelect() 0 4 1

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\ObjectAgent;
6
7
final class Capabilities
8
{
9
    private $supportedComparators;
10
    private $setParent;
11
    private $queryCount;
12
    private $queryJoin;
13
    private $querySelect;
14
15
    private function __construct()
16
    {
17
        // this class cannot be instantiated with "new".
18
    }
19
20
    public static function create(array $capabilities): Capabilities
21
    {
22
        $defaults = [
23
            'supported_comparators' => [],
24
            'can_set_parent' => false,
25
            'can_query_count' => false,
26
            'can_query_join' => false,
27
            'can_query_select' => false,
28
        ];
29
30 View Code Duplication
        if ($diff = array_diff(array_keys($capabilities), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
            throw new \InvalidArgumentException(sprintf(
32
                'Unknown capabilities: "%s". Valid capabilities: "%s"',
33
                implode('", "', $diff), implode('", "', array_keys($defaults))
34
            ));
35
        }
36
37
        $capabilities = array_merge($defaults, $capabilities);
38
39
        $instance = new self();
40
        $instance->supportedComparators = (array) $capabilities['supported_comparators'];
41
        $instance->setParent = (bool) $capabilities['can_set_parent'];
42
        $instance->queryJoin = (bool) $capabilities['can_query_join'];
43
        $instance->queryCount = (bool) $capabilities['can_query_count'];
44
        $instance->querySelect = (bool) $capabilities['can_query_select'];
45
46
        return $instance;
47
    }
48
49
    public function getSupportedComparators(): array
50
    {
51
        return $this->supportedComparators;
52
    }
53
54
    public function canSetParent(): bool
55
    {
56
        return $this->setParent;
57
    }
58
59
    public function canQueryCount(): bool
60
    {
61
        return $this->queryCount;
62
    }
63
64
    public function canQueryJoin(): bool
65
    {
66
        return $this->queryJoin;
67
    }
68
69
    public function canQuerySelect(): bool
70
    {
71
        return $this->querySelect;
72
    }
73
}
74