Completed
Branch master (2e2c40)
by Rob
01:16
created

AbstractCerberus::clearResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __construct.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
17
    {
18
        $this->soteria = new SoteriaService();
19
        $this->setOptions();
20
    }
21
22
23 4
    protected function processWrappers($handler)
24
    {
25 4
        $options = $this->getOption('Detection');
26
27 4
        foreach ($handler->getWrappers() as $wrapper) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
28
29 4
            $wrapper->setOptions($handler->getValue(), $options['Rules']);
30 4
            $wrapper->process();
31 4
            $this->addResult($wrapper->getScore(), $wrapper->getResult());
32
        }
33 4
    }
34
35 4
    private function getOption($name)
36
    {
37 4
        if (!$this->hasOption($name)) {
38
            return null;
39
        }
40
41 4
        return $this->options[$name];
42
    }
43
44 4
    private function hasOption($name)
45
    {
46 4
        return isset($this->options[$name]);
47
    }
48
49 4
    protected function addResult($score, $result)
50
    {
51 4
        if (is_array($result)) {
52 2
            $this->addScore($score);
53 2
            $this->result = array_merge($this->result, $result);
54
        }
55 4
        return $this;
56
    }
57
58
    /**
59
     * @param $score
60
     * @return $this
61
     */
62 2
    private function addScore($score)
63
    {
64 2
        $this->score += $score;
65 2
        return $this;
66
    }
67
68 4
    protected function clearResults()
69
    {
70 4
        $this->results = [];
71 4
        $this->result = [];
72 4
        $this->score = 0;
73 4
    }
74
75
    /**
76
     * @return array
77
     */
78
    protected function getOptions()
79
    {
80
        return $this->options;
81
    }
82
83
    /**
84
     * @param file|array $options
85
     * @return $this
86
     */
87 4
    public function setOptions($options = [])
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...
88
    {
89
90 4
        $baseOptions = new BaseOptions();
91 4
        $basic_options = $baseOptions->getOptions();
92
93 4
        if (is_array($options)) {
94 4
            $options = $this->arrayMergeRecursiveDistinct($basic_options, $options);
95
        } 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...
96
            if ($this->yaml_loaded()) {
0 ignored issues
show
Bug introduced by
The method yaml_loaded() does not seem to exist on object<devtoolboxuk\cerberus\AbstractCerberus>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
                $this->soteria->sanitise()->disinfect($options, 'url');
98
                if ($this->soteria->sanitise()->result()->isValid()) {
99
                    $options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options));
100
                } 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...
101
                    if (file_exists($options)) {
102
                        $options = $this->arrayMergeRecursiveDistinct($basic_options, yaml_parse_file($options));
103
                    }
104
                }
105
            }
106
        }
107
108 4
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options can also be of type object<devtoolboxuk\cerberus\file>. However, the property $options is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
109 4
    }
110
111 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...
112
    {
113 4
        if (empty($array2)) {
114
            return $merged;
115
        }
116
117 4
        foreach ($array2 as $key => &$value) {
118 4
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
119 4
                $merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
120
            } 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...
121 4
                $merged[$key] = $value;
122
            }
123
        }
124 4
        return $merged;
125
    }
126
}