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 — pr/798 ( d37a95 )
by Henrique
03:13
created

NestedValidationException::addRelated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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