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.0 (#798)
by
unknown
18:12
created

NestedValidationException   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 23.85%

Importance

Changes 0
Metric Value
wmc 45
lcom 1
cbo 2
dl 0
loc 267
ccs 31
cts 130
cp 0.2385
rs 8.3673
c 0
b 0
f 0

13 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
C getIterator() 0 48 10
A getMessages() 0 13 3
A getFullMessage() 0 18 4
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 self
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
    /**
126
     * @return SplObjectStorage
127
     */
128
    public function getIterator()
129
    {
130
        $childrenExceptions = new SplObjectStorage();
131
132
        $recursiveIteratorIterator = $this->getRecursiveIterator();
133
        $exceptionIterator = $recursiveIteratorIterator->getInnerIterator();
134
135
        $lastDepth = 0;
136
        $lastDepthOriginal = 0;
137
        $knownDepths = [];
138
        foreach ($recursiveIteratorIterator as $childException) {
139
            if ($childException instanceof self
140
                && $childException->getRelated()->count() === 1) {
141
                $relatedException = iterator_to_array($childException->getRelated())[0];
142
                if (! $relatedException instanceof  AttributeException) {
143
                    continue;
144
                }
145
            }
146
            $currentDepth = $lastDepth;
147
            $currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
148
149
            if (isset($knownDepths[$currentDepthOriginal])) {
150
                $currentDepth = $knownDepths[$currentDepthOriginal];
151
            } elseif ($currentDepthOriginal > $lastDepthOriginal
152
                && ($this->hasCustomTemplate() || $exceptionIterator->count() != 1)) {
153
                ++$currentDepth;
154
            }
155
156
            if (!isset($knownDepths[$currentDepthOriginal])) {
157
                $knownDepths[$currentDepthOriginal] = $currentDepth;
158
            }
159
160
            $lastDepth = $currentDepth;
161
            $lastDepthOriginal = $currentDepthOriginal;
162
163
            $childrenExceptions->attach(
164
                $childException,
165
                [
166
                    'depth' => $currentDepth,
167
                    'depth_original' => $currentDepthOriginal,
168
                    'previous_depth' => $lastDepth,
169
                    'previous_depth_original' => $lastDepthOriginal,
170
                ]
171
            );
172
        }
173
174
        return $childrenExceptions;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function getMessages()
181
    {
182
        $messages = [$this->getMessage()];
183
        foreach ($this as $exception) {
184
            $messages[] = $exception->getMessage();
185
        }
186
187
        if (count($messages) > 1) {
188
            array_shift($messages);
189
        }
190
191
        return $messages;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getFullMessage()
198
    {
199
        $marker = '-';
200
        $messages = [];
201
        $exceptions = $this->getIterator();
202
203
        if ($this->hasCustomTemplate() || count($exceptions) != 1) {
204
            $messages[] = sprintf('%s %s', $marker, $this->getMessage());
205
        }
206
207
        foreach ($exceptions as $exception) {
208
            $depth = $exceptions[$exception]['depth'];
209
            $prefix = str_repeat(' ', $depth * 2);
210
            $messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
211
        }
212
213
        return implode(PHP_EOL, $messages);
214
    }
215
216
    /**
217
     * @return SplObjectStorage
218
     */
219 3
    public function getRelated()
220
    {
221 3
        if (!$this->exceptions instanceof SplObjectStorage) {
222 3
            $this->exceptions = new SplObjectStorage();
223 3
        }
224
225 3
        return $this->exceptions;
226
    }
227
228
    /**
229
     * @param string $name
230
     * @param mixed  $value
231
     *
232
     * @return self
233
     */
234
    public function setParam($name, $value)
235
    {
236
        if ('translator' === $name) {
237
            foreach ($this->getRelated() as $exception) {
238
                $exception->setParam($name, $value);
239
            }
240
        }
241
242
        parent::setParam($name, $value);
243
244
        return $this;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250 1
    private function isRelated($name, ValidationException $exception)
251
    {
252 1
        return ($exception->getId() === $name || $exception->getName() === $name);
253
    }
254
255
    /**
256
     * @return ValidationException
257
     */
258 1
    public function getRelatedByName($name)
259
    {
260 1
        if ($this->isRelated($name, $this)) {
261
            return $this;
262
        }
263
264 1
        foreach ($this->getRecursiveIterator() as $exception) {
265 1
            if ($this->isRelated($name, $exception)) {
266 1
                return $exception;
267
            }
268 1
        }
269 1
    }
270
271
    /**
272
     * @param array $exceptions
273
     *
274
     * @return self
275
     */
276
    public function setRelated(array $exceptions)
277
    {
278
        foreach ($exceptions as $exception) {
279
            $this->addRelated($exception);
280
        }
281
282
        return $this;
283
    }
284
}
285