Completed
Push — master ( f76706...507944 )
by Rob
02:03
created

AbstractCerberus::isYamlLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace devtoolboxuk\cerberus;
4
5
use devtoolboxuk\soteria\SoteriaService;
6
7
abstract class AbstractCerberus
8
{
9
10
    protected $options = [];
11
    protected $results = [];
12
    protected $result = [];
13
    protected $score = 0;
14
    protected $soteria;
15
16
    public function __construct()
17
    {
18
        $this->soteria = new SoteriaService();
19
        $this->setOptions();
20
    }
21
22
    /**
23
     * @param $handler
24
     */
25 4
    protected function processWrappers($handler)
26
    {
27 4
        $options = $this->getOption('Detection');
28
29 4
        foreach ($handler->getWrappers() as $wrapper) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
30
31 4
            $wrapper->setOptions($handler->getValue(), $options['Rules']);
32 4
            $wrapper->process();
33 4
            $this->addResult($wrapper->getScore(), $wrapper->getResult());
34
        }
35 4
    }
36
37
    /**
38
     * @param $name
39
     * @return mixed|null
40
     */
41 4
    private function getOption($name)
42
    {
43 4
        if (!$this->hasOption($name)) {
44
            return null;
45
        }
46
47 4
        return $this->options[$name];
48
    }
49
50
    /**
51
     * @param $name
52
     * @return bool
53
     */
54 4
    private function hasOption($name)
55
    {
56 4
        return isset($this->options[$name]);
57
    }
58
59
    /**
60
     * @param $score
61
     * @param $result
62
     * @return $this
63
     */
64 4
    protected function addResult($score, $result)
65
    {
66 4
        if (is_array($result)) {
67 2
            $this->addScore($score);
68 2
            $this->result = array_merge($this->result, $result);
69
        }
70 4
        return $this;
71
    }
72
73
    /**
74
     * @param $score
75
     * @return $this
76
     */
77 2
    private function addScore($score)
78
    {
79 2
        $this->score += $score;
80 2
        return $this;
81
    }
82
83
84 4
    protected function clearResults()
85
    {
86 4
        $this->results = [];
87 4
        $this->result = [];
88 4
        $this->score = 0;
89 4
    }
90
91
    /**
92
     * @return array
93
     */
94
    protected function getOptions()
95
    {
96
        return $this->options;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    private function isYamlLoaded()
103
    {
104
        return extension_loaded('yaml');
105
    }
106
107
    /**
108
     * @param null $options
109
     */
110 4
    public function setOptions($options = null)
0 ignored issues
show
Coding Style Naming introduced by
The variable $basic_options is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
111
    {
112
113 4
        $baseOptions = new BaseOptions();
114 4
        $basic_options = $baseOptions->getOptions();
115
116 4
        if (is_array($options)) {
117 4
            $options = $this->arrayMergeRecursiveDistinct($basic_options, $options);
118
        } else {
0 ignored issues
show
Coding Style introduced by
The method setOptions uses an else expression. Else is never necessary and you can simplify the code to work without else.
Loading history...
119
            if ($this->isYamlLoaded()) {
120
                $this->soteria->sanitise()->disinfect($options, 'url');
121
                if ($this->soteria->sanitise()->result()->isValid()) {
122
                    $options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options));
123
                } else {
0 ignored issues
show
Coding Style introduced by
The method setOptions uses an else expression. Else is never necessary and you can simplify the code to work without else.
Loading history...
124
                    if (file_exists($options)) {
125
                        $options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options));
126
                    }
127
                }
128
            }
129
        }
130
131 4
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can be null. However, the property $options is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
132 4
    }
133
134
    /**
135
     * @param array $merged
136
     * @param array $array2
137
     * @return array
138
     */
139 4
    private function arrayMergeRecursiveDistinct($merged = [], $array2 = [])
0 ignored issues
show
Complexity introduced by
This operation has 10 execution paths which exceeds the configured maximum of 10.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
140
    {
141 4
        if (empty($array2)) {
142
            return $merged;
143
        }
144
145 4
        foreach ($array2 as $key => &$value) {
146 4
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
147 4
                $merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
148
            } else {
0 ignored issues
show
Coding Style introduced by
The method arrayMergeRecursiveDistinct uses an else expression. Else is never necessary and you can simplify the code to work without else.
Loading history...
149 4
                $merged[$key] = $value;
150
            }
151
        }
152 4
        return $merged;
153
    }
154
}