Instance   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequired() 0 5 1
A set() 0 4 1
B check() 6 22 8
A get() 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
namespace CheckedInstance;
3
4
/**
5
 * Class Instance
6
 * @package CheckedInstance
7
 */
8
class Instance implements InstanceInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $required = [];
14
15
    /**
16
     * @var array
17
     */
18
    protected $config = [];
19
20
    /**
21
     * @return array
22
     */
23
    public static function getRequired() : array
24
    {
25
        $i = new static();
26
        return $i->required;
27
    }
28
29
    /**
30
     * @param $key
31
     * @param $value
32
     */
33
    public function set($key, $value)
34
    {
35
        $this->config[$key] = $value;
36
    }
37
38
    /**
39
     * @return bool
40
     * @throws InstanceCorruptException
41
     */
42
    public function check() : bool
43
    {
44
        if ($this->required == array_values($this->required)) {
45
            foreach ($this->required as $key) {
46 View Code Duplication
                if (!isset($this->config[$key])) {
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...
47
                    throw new InstanceCorruptException(get_class($this).':'.$key.' not set!');
48
                }
49
            }
50
        } else {
51
            foreach ($this->required as $instance => $key) {
52 View Code Duplication
                if (!isset($this->config[$key])) {
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...
53
                    throw new InstanceCorruptException(get_class($this).':'.$key.' not set!');
54
                }
55
                if (!is_numeric($instance)) {
56
                    if (!($this->config[$key] instanceof $instance)) {
57
                        throw new InstanceCorruptException(get_class($this).':'.$key.' not instance of '.$instance.'!');
58
                    }
59
                }
60
            }
61
        }
62
        return true;
63
    }
64
65
    /**
66
     * @param $key
67
     * @return mixed
68
     */
69
    protected function get($key)
70
    {
71
        return $this->config[$key] ?? null;
72
    }
73
}
74