| Total Complexity | 47 |
| Total Lines | 258 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OperationResult_Collection 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.
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 OperationResult_Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class OperationResult_Collection extends OperationResult |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var OperationResult[] |
||
| 31 | */ |
||
| 32 | protected $results = array(); |
||
| 33 | |||
| 34 | public function makeError(string $message, int $code=0) : OperationResult |
||
| 35 | { |
||
| 36 | return $this->add('makeError', $message, $code); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function makeSuccess(string $message, int $code=0) : OperationResult |
||
| 40 | { |
||
| 41 | return $this->add('makeSuccess', $message, $code); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function makeWarning(string $message, int $code=0) : OperationResult |
||
| 45 | { |
||
| 46 | return $this->add('makeWarning', $message, $code); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function makeNotice(string $message, int $code=0) : OperationResult |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function add(string $method, string $message, int $code=0) : OperationResult |
||
| 55 | { |
||
| 56 | $result = new OperationResult($this->subject); |
||
| 57 | $result->$method($message, $code); |
||
| 58 | |||
| 59 | $this->results[] = $result; |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function addResult(OperationResult $result) : OperationResult_Collection |
||
| 65 | { |
||
| 66 | if($result instanceof OperationResult_Collection) |
||
| 67 | { |
||
| 68 | return $this->importCollection($result); |
||
| 69 | } |
||
| 70 | |||
| 71 | return $this->importResult($result); |
||
| 72 | } |
||
| 73 | |||
| 74 | private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection |
||
| 75 | { |
||
| 76 | $results = $collection->getResults(); |
||
| 77 | |||
| 78 | foreach($results as $result) |
||
| 79 | { |
||
| 80 | $this->addResult($result); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | |||
| 86 | private function importResult(OperationResult $result) : OperationResult_Collection |
||
| 87 | { |
||
| 88 | // We need to inherit this collection\'s subject for any |
||
| 89 | // results we add, so we simply create a new instance. |
||
| 90 | // |
||
| 91 | // Since there is no easy, well performing way to check if |
||
| 92 | // the subjects are the same, this is actually the better way. |
||
| 93 | $new = new OperationResult($this->subject); |
||
| 94 | |||
| 95 | if($result->isValid()) |
||
| 96 | { |
||
| 97 | $new->makeSuccess($result->getSuccessMessage(), $result->getCode()); |
||
| 98 | } |
||
| 99 | else |
||
| 100 | { |
||
| 101 | $new->makeError($result->getErrorMessage(), $result->getCode()); |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->results[] = $new; |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return OperationResult[] |
||
| 111 | */ |
||
| 112 | public function getResults() : array |
||
| 113 | { |
||
| 114 | return $this->results; |
||
| 115 | } |
||
| 116 | |||
| 117 | public function isValid() : bool |
||
| 118 | { |
||
| 119 | foreach($this->results as $result) |
||
| 120 | { |
||
| 121 | if(!$result->isValid()) |
||
| 122 | { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function hasCode() : bool |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getCode() : int |
||
| 144 | { |
||
| 145 | foreach($this->results as $result) |
||
| 146 | { |
||
| 147 | if($result->hasCode()) |
||
| 148 | { |
||
| 149 | return $result->getCode(); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return 0; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getMessage(string $type='') : string |
||
| 157 | { |
||
| 158 | foreach($this->results as $result) |
||
| 159 | { |
||
| 160 | $msg = $result->getMessage($type); |
||
| 161 | |||
| 162 | if(!empty($msg)) |
||
| 163 | { |
||
| 164 | return $msg; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return ''; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function containsCode(int $code) : bool |
||
| 172 | { |
||
| 173 | foreach($this->results as $result) |
||
| 174 | { |
||
| 175 | if($result->getCode() === $code) |
||
| 176 | { |
||
| 177 | return true; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function countErrors() : int |
||
| 185 | { |
||
| 186 | return $this->countByType(self::TYPE_ERROR); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function countWarnings() : int |
||
| 190 | { |
||
| 191 | return $this->countByType(self::TYPE_WARNING); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function countSuccesses() : int |
||
| 195 | { |
||
| 196 | return $this->countByType(self::TYPE_SUCCESS); |
||
| 197 | } |
||
| 198 | |||
| 199 | public function countNotices() : int |
||
| 200 | { |
||
| 201 | return $this->countByType(self::TYPE_NOTICE); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function countByType(string $type) : int |
||
| 205 | { |
||
| 206 | $amount = 0; |
||
| 207 | |||
| 208 | foreach($this->results as $result) |
||
| 209 | { |
||
| 210 | if($result->isType($type)) |
||
| 211 | { |
||
| 212 | $amount++; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | return $amount; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function countResults() : int |
||
| 220 | { |
||
| 221 | return count($this->results); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function getErrors() : array |
||
| 227 | } |
||
| 228 | |||
| 229 | public function getSuccesses() : array |
||
| 230 | { |
||
| 231 | return $this->getByType(self::TYPE_SUCCESS); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function getWarnings() : array |
||
| 235 | { |
||
| 236 | return $this->getByType(self::TYPE_WARNING); |
||
| 237 | } |
||
| 238 | |||
| 239 | public function getNotices() : array |
||
| 242 | } |
||
| 243 | |||
| 244 | public function getByType(string $type) : array |
||
| 245 | { |
||
| 246 | $results = array(); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function isType(string $type) : bool |
||
| 260 | { |
||
| 261 | foreach($this->results as $result) |
||
| 262 | { |
||
| 263 | if($result->isType($type)) |
||
| 264 | { |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | return false; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function getSummary() : string |
||
| 285 | } |
||
| 286 | } |
||
| 287 |