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 | 3 | public function addRelated(ValidationException $exception) |
|
| 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) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return RecursiveIteratorIterator |
||
| 113 | */ |
||
| 114 | 1 | private function getRecursiveIterator() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @return SplObjectStorage |
||
| 127 | */ |
||
| 128 | public function getIterator() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function getMessages() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getFullMessage() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @see https://github.com/Respect/Validation/issues/664 |
||
| 217 | * @return Array |
||
| 218 | */ |
||
| 219 | public function getMessagesIndexedByName() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return SplObjectStorage |
||
| 237 | */ |
||
| 238 | 3 | public function getRelated() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $name |
||
| 249 | * @param mixed $value |
||
| 250 | * |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | public function setParam($name, $value) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 1 | private function isRelated($name, ValidationException $exception) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return ValidationException |
||
| 276 | */ |
||
| 277 | 1 | public function getRelatedByName($name) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $exceptions |
||
| 292 | * |
||
| 293 | * @return self |
||
| 294 | */ |
||
| 295 | public function setRelated(array $exceptions) |
||
| 303 | } |
||
| 304 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.