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) |
|
| 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) |
|
| 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 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $exceptions |
||
| 240 | * |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | public function setRelated(array $exceptions) |
||
| 251 | } |
||
| 252 |