We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 |
||
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 | 127 | public function addRelated(ValidationException $exception) |
|
36 | |||
37 | /** |
||
38 | * @param string $path |
||
39 | * @param ValidationException $exception |
||
40 | * |
||
41 | * @return ValidationException |
||
42 | */ |
||
43 | 8 | private function getExceptionForPath($path, ValidationException $exception) |
|
44 | { |
||
45 | 8 | if ($path === $exception->guessId()) { |
|
46 | 5 | return $exception; |
|
47 | } |
||
48 | |||
49 | 6 | if (!$exception instanceof self) { |
|
50 | 2 | return $exception; |
|
51 | } |
||
52 | |||
53 | 4 | foreach ($exception as $subException) { |
|
54 | 4 | return $subException; |
|
55 | } |
||
56 | |||
57 | 1 | return $exception; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param array $paths |
||
62 | * |
||
63 | * @return string[] |
||
64 | */ |
||
65 | 8 | public function findMessages(array $paths) |
|
66 | { |
||
67 | 8 | $messages = []; |
|
68 | |||
69 | 8 | foreach ($paths as $key => $value) { |
|
70 | 8 | $numericKey = is_numeric($key); |
|
71 | 8 | $path = $numericKey ? $value : $key; |
|
72 | |||
73 | 8 | if (!($exception = $this->getRelatedByName($path))) { |
|
74 | 4 | $exception = $this->findRelated($path); |
|
75 | } |
||
76 | |||
77 | 8 | $path = str_replace('.', '_', $path); |
|
78 | |||
79 | 8 | if (!$exception) { |
|
80 | 1 | $messages[$path] = ''; |
|
81 | 1 | continue; |
|
82 | } |
||
83 | |||
84 | 8 | $exception = $this->getExceptionForPath($path, $exception); |
|
85 | 8 | if (!$numericKey) { |
|
86 | 4 | $exception->setTemplate($value); |
|
87 | } |
||
88 | |||
89 | 8 | $messages[$path] = $exception->getMainMessage(); |
|
90 | } |
||
91 | |||
92 | 8 | return $messages; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return Exception |
||
97 | */ |
||
98 | 5 | public function findRelated($path) |
|
110 | |||
111 | /** |
||
112 | * @return RecursiveIteratorIterator |
||
113 | */ |
||
114 | 122 | private function getRecursiveIterator() |
|
124 | |||
125 | /** |
||
126 | * Returns weather an exception should be omitted or not. |
||
127 | * |
||
128 | * @param ExceptionInterface $exception |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 116 | private function isOmissible(ExceptionInterface $exception) |
|
133 | { |
||
134 | 116 | if (!$exception instanceof self) { |
|
135 | 114 | return false; |
|
136 | } |
||
137 | |||
138 | 17 | $relatedExceptions = $exception->getRelated(); |
|
139 | 17 | $relatedExceptions->rewind(); |
|
140 | 17 | $childException = $relatedExceptions->current(); |
|
141 | |||
142 | 17 | return $relatedExceptions->count() === 1 && !$childException instanceof NonOmissibleExceptionInterface; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return SplObjectStorage |
||
147 | */ |
||
148 | 117 | public function getIterator() |
|
149 | { |
||
150 | 117 | $childrenExceptions = new SplObjectStorage(); |
|
151 | |||
152 | 117 | $recursiveIteratorIterator = $this->getRecursiveIterator(); |
|
153 | 117 | $exceptionIterator = $recursiveIteratorIterator->getInnerIterator(); |
|
154 | |||
155 | 117 | $lastDepth = 0; |
|
156 | 117 | $lastDepthOriginal = 0; |
|
157 | 117 | $knownDepths = []; |
|
158 | 117 | foreach ($recursiveIteratorIterator as $childException) { |
|
159 | 116 | if ($this->isOmissible($childException)) { |
|
160 | 15 | continue; |
|
161 | } |
||
162 | |||
163 | 116 | $currentDepth = $lastDepth; |
|
164 | 116 | $currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1; |
|
165 | |||
166 | 116 | if (isset($knownDepths[$currentDepthOriginal])) { |
|
167 | 13 | $currentDepth = $knownDepths[$currentDepthOriginal]; |
|
168 | 116 | } elseif ($currentDepthOriginal > $lastDepthOriginal |
|
169 | 116 | && ($this->hasCustomTemplate() || $exceptionIterator->count() != 1)) { |
|
170 | 13 | ++$currentDepth; |
|
171 | } |
||
172 | |||
173 | 116 | if (!isset($knownDepths[$currentDepthOriginal])) { |
|
174 | 116 | $knownDepths[$currentDepthOriginal] = $currentDepth; |
|
175 | } |
||
176 | |||
177 | 116 | $lastDepth = $currentDepth; |
|
178 | 116 | $lastDepthOriginal = $currentDepthOriginal; |
|
179 | |||
180 | 116 | $childrenExceptions->attach( |
|
181 | 116 | $childException, |
|
182 | [ |
||
183 | 116 | 'depth' => $currentDepth, |
|
184 | 116 | 'depth_original' => $currentDepthOriginal, |
|
185 | 116 | 'previous_depth' => $lastDepth, |
|
186 | 116 | 'previous_depth_original' => $lastDepthOriginal, |
|
187 | ] |
||
188 | ); |
||
189 | } |
||
190 | |||
191 | 117 | return $childrenExceptions; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return array |
||
196 | */ |
||
197 | 4 | public function getMessages() |
|
198 | { |
||
199 | 4 | $messages = [$this->getMessage()]; |
|
200 | 4 | foreach ($this as $exception) { |
|
201 | 4 | $messages[] = $exception->getMessage(); |
|
202 | } |
||
203 | |||
204 | 4 | if (count($messages) > 1) { |
|
205 | 4 | array_shift($messages); |
|
206 | } |
||
207 | |||
208 | 4 | return $messages; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | 110 | public function getFullMessage() |
|
215 | { |
||
216 | 110 | $marker = '-'; |
|
217 | 110 | $messages = []; |
|
218 | 110 | $exceptions = $this->getIterator(); |
|
219 | |||
220 | 110 | if ($this->hasCustomTemplate() || count($exceptions) != 1) { |
|
221 | 11 | $messages[] = sprintf('%s %s', $marker, $this->getMessage()); |
|
222 | } |
||
223 | |||
224 | 110 | foreach ($exceptions as $exception) { |
|
225 | 109 | $depth = $exceptions[$exception]['depth']; |
|
226 | 109 | $prefix = str_repeat(' ', $depth * 2); |
|
227 | 109 | $messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage()); |
|
228 | } |
||
229 | |||
230 | 110 | return implode(PHP_EOL, $messages); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return SplObjectStorage |
||
235 | */ |
||
236 | 128 | public function getRelated() |
|
244 | |||
245 | /** |
||
246 | * @param string $name |
||
247 | * @param mixed $value |
||
248 | * |
||
249 | * @return self |
||
250 | */ |
||
251 | 1 | public function setParam($name, $value) |
|
252 | { |
||
253 | 1 | if ('translator' === $name) { |
|
254 | 1 | foreach ($this->getRelated() as $exception) { |
|
255 | 1 | $exception->setParam($name, $value); |
|
256 | } |
||
257 | } |
||
258 | |||
259 | 1 | parent::setParam($name, $value); |
|
260 | |||
261 | 1 | return $this; |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | 9 | private function isRelated($name, ValidationException $exception) |
|
271 | |||
272 | /** |
||
273 | * @return ValidationException |
||
274 | */ |
||
275 | 9 | public function getRelatedByName($name) |
|
276 | { |
||
277 | 9 | if ($this->isRelated($name, $this)) { |
|
278 | 2 | return $this; |
|
287 | |||
288 | /** |
||
289 | * @param array $exceptions |
||
290 | * |
||
291 | * @return self |
||
292 | */ |
||
293 | 124 | public function setRelated(array $exceptions) |
|
301 | } |
||
302 |