Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — 1.1 (#1047)
by Henrique
03:53
created

NestedValidationException   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 286
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 22.3%

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 2
dl 0
loc 286
ccs 31
cts 139
cp 0.223
rs 8.8798
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addRelated() 0 6 1
A getExceptionForPath() 0 16 4
B findMessages() 0 29 6
A findRelated() 0 12 3
A getRecursiveIterator() 0 10 1
A isSkippable() 0 19 4
B getIterator() 0 44 6
A getMessages() 0 13 3
A getFullMessage() 0 21 3
A getRelated() 0 8 2
A setParam() 0 12 3
A isRelated() 0 4 2
A getRelatedByName() 0 12 4
A setRelated() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like NestedValidationException often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use NestedValidationException, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Respect\Validation\Exceptions;
13
14
use IteratorAggregate;
15
use RecursiveIteratorIterator;
16
use SplObjectStorage;
17
18
class NestedValidationException extends ValidationException implements IteratorAggregate
19
{
20
    /**
21
     * @var SplObjectStorage
22
     */
23
    private $exceptions = [];
24
25
    /**
26
     * @param ValidationException $exception
27
     *
28
     * @return self
29
     */
30 3
    public function addRelated(ValidationException $exception)
31
    {
32 3
        $this->getRelated()->attach($exception);
33
34 3
        return $this;
35
    }
36
37
    /**
38
     * @param string $path
39
     * @param ValidationException $exception
40
     *
41
     * @return ValidationException
42
     */
43
    private function getExceptionForPath($path, ValidationException $exception)
44
    {
45
        if ($path === $exception->guessId()) {
46
            return $exception;
47
        }
48
49
        if (!$exception instanceof self) {
50
            return $exception;
51
        }
52
53
        foreach ($exception as $subException) {
54
            return $subException;
55
        }
56
57
        return $exception;
58
    }
59
60
    /**
61
     * @param array $paths
62
     *
63
     * @return array
64
     */
65
    public function findMessages(array $paths)
66
    {
67
        $messages = [];
68
69
        foreach ($paths as $key => $value) {
70
            $numericKey = is_numeric($key);
71
            $path = $numericKey ? $value : $key;
72
73
            if (!($exception = $this->getRelatedByName($path))) {
74
                $exception = $this->findRelated($path);
75
            }
76
77
            $path = str_replace('.', '_', $path);
78
79
            if (!$exception) {
80
                $messages[$path] = '';
81
                continue;
82
            }
83
84
            $exception = $this->getExceptionForPath($path, $exception);
85
            if (!$numericKey) {
86
                $exception->setTemplate($value);
87
            }
88
89
            $messages[$path] = $exception->getMainMessage();
90
        }
91
92
        return $messages;
93
    }
94
95
    /**
96
     * @return Exception
97
     */
98 1
    public function findRelated($path)
99
    {
100 1
        $target = $this;
101 1
        $pieces = explode('.', $path);
102
103 1
        while (!empty($pieces) && $target) {
104 1
            $piece = array_shift($pieces);
105 1
            $target = $target->getRelatedByName($piece);
106 1
        }
107
108 1
        return $target;
109
    }
110
111
    /**
112
     * @return RecursiveIteratorIterator
113
     */
114 1
    private function getRecursiveIterator()
115
    {
116 1
        $exceptionIterator = new RecursiveExceptionIterator($this);
117 1
        $recursiveIteratorIterator = new RecursiveIteratorIterator(
118 1
            $exceptionIterator,
119
            RecursiveIteratorIterator::SELF_FIRST
120 1
        );
121
122 1
        return $recursiveIteratorIterator;
123
    }
124
125
    private function isSkippable(ValidationException $exception)
126
    {
127
        if (!$exception instanceof self) {
128
            return false;
129
        }
130
131
        if (1 !== $exception->getRelated()->count()) {
132
            return false;
133
        }
134
135
        if (!$exception->hasCustomTemplate()) {
136
            return true;
137
        }
138
139
        $exception->getRelated()->rewind();
140
        $childException = $exception->getRelated()->current();
141
142
        return $childException->getMessage() === $exception->getMessage();
143
    }
144
145
    /**
146
     * @return SplObjectStorage
147
     */
148
    public function getIterator()
149
    {
150
        $childrenExceptions = new SplObjectStorage();
151
152
        $recursiveIteratorIterator = $this->getRecursiveIterator();
153
        $exceptionIterator = $recursiveIteratorIterator->getInnerIterator();
0 ignored issues
show
Unused Code introduced by
$exceptionIterator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
155
        $lastDepth = 0;
156
        $lastDepthOriginal = 0;
157
        $knownDepths = [];
158
        foreach ($recursiveIteratorIterator as $childException) {
159
            if ($this->isSkippable($childException)) {
160
                continue;
161
            }
162
163
            $currentDepth = $lastDepth;
164
            $currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
165
166
            if (isset($knownDepths[$currentDepthOriginal])) {
167
                $currentDepth = $knownDepths[$currentDepthOriginal];
168
            } elseif ($currentDepthOriginal > $lastDepthOriginal) {
169
                ++$currentDepth;
170
            }
171
172
            if (!isset($knownDepths[$currentDepthOriginal])) {
173
                $knownDepths[$currentDepthOriginal] = $currentDepth;
174
            }
175
176
            $lastDepth = $currentDepth;
177
            $lastDepthOriginal = $currentDepthOriginal;
178
179
            $childrenExceptions->attach(
180
                $childException,
181
                [
182
                    'depth' => $currentDepth,
183
                    'depth_original' => $currentDepthOriginal,
184
                    'previous_depth' => $lastDepth,
185
                    'previous_depth_original' => $lastDepthOriginal,
186
                ]
187
            );
188
        }
189
190
        return $childrenExceptions;
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function getMessages()
197
    {
198
        $messages = [$this->getMessage()];
199
        foreach ($this as $exception) {
200
            $messages[] = $exception->getMessage();
201
        }
202
203
        if (count($messages) > 1) {
204
            array_shift($messages);
205
        }
206
207
        return $messages;
208
    }
209
210
    /**
211
     * @return string
212
     */
213
    public function getFullMessage()
214
    {
215
        $messages = [];
216
        $leveler = 1;
217
218
        if (!$this->isSkippable($this)) {
219
            $leveler = 0;
220
            $messages[] = sprintf('- %s', $this->getMessage());
221
        }
222
223
        $exceptions = $this->getIterator();
224
        foreach ($exceptions as $exception) {
225
            $messages[] = sprintf(
226
                '%s- %s',
227
                str_repeat(' ', ($exceptions[$exception]['depth'] - $leveler) * 2),
228
                $exception->getMessage()
229
            );
230
        }
231
232
        return implode(PHP_EOL, $messages);
233
    }
234
235
    /**
236
     * @return SplObjectStorage
237
     */
238 3
    public function getRelated()
239
    {
240 3
        if (!$this->exceptions instanceof SplObjectStorage) {
241 3
            $this->exceptions = new SplObjectStorage();
242 3
        }
243
244 3
        return $this->exceptions;
245
    }
246
247
    /**
248
     * @param string $name
249
     * @param mixed  $value
250
     *
251
     * @return self
252
     */
253
    public function setParam($name, $value)
254
    {
255
        if ('translator' === $name) {
256
            foreach ($this->getRelated() as $exception) {
257
                $exception->setParam($name, $value);
258
            }
259
        }
260
261
        parent::setParam($name, $value);
262
263
        return $this;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269 1
    private function isRelated($name, ValidationException $exception)
270
    {
271 1
        return ($exception->getId() === $name || $exception->getName() === $name);
272
    }
273
274
    /**
275
     * @return ValidationException
276
     */
277 1
    public function getRelatedByName($name)
278
    {
279 1
        if ($this->isRelated($name, $this)) {
280
            return $this;
281
        }
282
283 1
        foreach ($this->getRecursiveIterator() as $exception) {
284 1
            if ($this->isRelated($name, $exception)) {
285 1
                return $exception;
286
            }
287 1
        }
288 1
    }
289
290
    /**
291
     * @param array $exceptions
292
     *
293
     * @return self
294
     */
295
    public function setRelated(array $exceptions)
296
    {
297
        foreach ($exceptions as $exception) {
298
            $this->addRelated($exception);
299
        }
300
301
        return $this;
302
    }
303
}
304