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
Push — master ( d71621...4eb6d8 )
by Henrique
06:52
created

NestedValidationException::getExceptionForPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 2
crap 20
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
58
    /**
59
     * @param array $paths
60
     *
61
     * @return self
62
     */
63
    public function findMessages(array $paths)
64
    {
65
        $messages = [];
66
67
        foreach ($paths as $key => $value) {
68
            $numericKey = is_numeric($key);
69
            $path = $numericKey ? $value : $key;
70
71
            if (!($exception = $this->getRelatedByName($path))) {
72
                $exception = $this->findRelated($path);
73
            }
74
75
            $path = str_replace('.', '_', $path);
76
77
            if (!$exception) {
78
                $messages[$path] = '';
79
                continue;
80
            }
81
82
            $exception = $this->getExceptionForPath($path, $exception);
83
            if (!$numericKey) {
84
                $exception->setTemplate($value);
85
            }
86
87
            $messages[$path] = $exception->getMainMessage();
88
        }
89
90
        return $messages;
91
    }
92
93
    /**
94
     * @return Exception
95
     */
96 1
    public function findRelated($path)
97
    {
98 1
        $target = $this;
99 1
        $pieces = explode('.', $path);
100
101 1
        while (!empty($pieces) && $target) {
102 1
            $piece = array_shift($pieces);
103 1
            $target = $target->getRelatedByName($piece);
104 1
        }
105
106 1
        return $target;
107
    }
108
109
    /**
110
     * @return RecursiveIteratorIterator
111
     */
112 1
    private function getRecursiveIterator()
113
    {
114 1
        $exceptionIterator = new RecursiveExceptionIterator($this);
115 1
        $recursiveIteratorIterator = new RecursiveIteratorIterator(
116 1
            $exceptionIterator,
117
            RecursiveIteratorIterator::SELF_FIRST
118 1
        );
119
120 1
        return $recursiveIteratorIterator;
121
    }
122
123
    /**
124
     * @return SplObjectStorage
125
     */
126
    public function getIterator()
127
    {
128
        $childrenExceptions = new SplObjectStorage();
129
130
        $recursiveIteratorIterator = $this->getRecursiveIterator();
131
        $exceptionIterator = $recursiveIteratorIterator->getInnerIterator();
132
133
        $lastDepth = 0;
134
        $lastDepthOriginal = 0;
135
        $knownDepths = [];
136
        foreach ($recursiveIteratorIterator as $childException) {
137
            if ($childException instanceof self
138
                && $childException->getRelated()->count() > 0
139
                && $childException->getRelated()->count() < 2) {
140
                continue;
141
            }
142
143
            $currentDepth = $lastDepth;
144
            $currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
145
146
            if (isset($knownDepths[$currentDepthOriginal])) {
147
                $currentDepth = $knownDepths[$currentDepthOriginal];
148
            } elseif ($currentDepthOriginal > $lastDepthOriginal
149
                && ($this->hasCustomTemplate() || $exceptionIterator->count() != 1)) {
150
                ++$currentDepth;
151
            }
152
153
            if (!isset($knownDepths[$currentDepthOriginal])) {
154
                $knownDepths[$currentDepthOriginal] = $currentDepth;
155
            }
156
157
            $lastDepth = $currentDepth;
158
            $lastDepthOriginal = $currentDepthOriginal;
159
160
            $childrenExceptions->attach(
161
                $childException,
162
                [
163
                    'depth' => $currentDepth,
164
                    'depth_original' => $currentDepthOriginal,
165
                    'previous_depth' => $lastDepth,
166
                    'previous_depth_original' => $lastDepthOriginal,
167
                ]
168
            );
169
        }
170
171
        return $childrenExceptions;
172
    }
173
174
    /**
175
     * @return array
176
     */
177
    public function getMessages()
178
    {
179
        $messages = [$this->getMessage()];
180
        foreach ($this as $exception) {
181
            $messages[] = $exception->getMessage();
182
        }
183
184
        if (count($messages) > 1) {
185
            array_shift($messages);
186
        }
187
188
        return $messages;
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public function getFullMessage()
195
    {
196
        $marker = '-';
197
        $messages = [];
198
        $exceptions = $this->getIterator();
199
200
        if ($this->hasCustomTemplate() || count($exceptions) != 1) {
201
            $messages[] = sprintf('%s %s', $marker, $this->getMessage());
202
        }
203
204
        foreach ($exceptions as $exception) {
205
            $depth = $exceptions[$exception]['depth'];
206
            $prefix = str_repeat(' ', $depth * 2);
207
            $messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
208
        }
209
210
        return implode(PHP_EOL, $messages);
211
    }
212
213
    /**
214
     * @return SplObjectStorage
215
     */
216 3
    public function getRelated()
217
    {
218 3
        if (!$this->exceptions instanceof SplObjectStorage) {
219 3
            $this->exceptions = new SplObjectStorage();
220 3
        }
221
222 3
        return $this->exceptions;
223
    }
224
225
    /**
226
     * @param string $name
227
     * @param mixed  $value
228
     *
229
     * @return self
230
     */
231
    public function setParam($name, $value)
232
    {
233
        if ('translator' === $name) {
234
            foreach ($this->getRelated() as $exception) {
235
                $exception->setParam($name, $value);
236
            }
237
        }
238
239
        parent::setParam($name, $value);
240
241
        return $this;
242
    }
243
244
    /**
245
     * @return bool
246
     */
247 1
    private function isRelated($name, ValidationException $exception)
248
    {
249 1
        return ($exception->getId() === $name || $exception->getName() === $name);
250
    }
251
252
    /**
253
     * @return ValidationException
254
     */
255 1
    public function getRelatedByName($name)
256
    {
257 1
        if ($this->isRelated($name, $this)) {
258
            return $this;
259
        }
260
261 1
        foreach ($this->getRecursiveIterator() as $exception) {
262 1
            if ($this->isRelated($name, $exception)) {
263 1
                return $exception;
264
            }
265 1
        }
266 1
    }
267
268
    /**
269
     * @param array $exceptions
270
     *
271
     * @return self
272
     */
273
    public function setRelated(array $exceptions)
274
    {
275
        foreach ($exceptions as $exception) {
276
            $this->addRelated($exception);
277
        }
278
279
        return $this;
280
    }
281
}
282