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 — master (#668)
by Henrique
06:38
created

NestedValidationException::getRelatedByName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

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