Instance::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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