| Total Complexity | 45 |
| Total Lines | 237 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 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 | $this->results[] = $result; |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection |
||
| 77 | { |
||
| 78 | $results = $collection->getResults(); |
||
| 79 | |||
| 80 | foreach($results as $result) |
||
| 81 | { |
||
| 82 | $this->addResult($result); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $this; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return OperationResult[] |
||
| 90 | */ |
||
| 91 | public function getResults() : array |
||
| 92 | { |
||
| 93 | return $this->results; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function isValid() : bool |
||
| 97 | { |
||
| 98 | foreach($this->results as $result) |
||
| 99 | { |
||
| 100 | if(!$result->isValid()) |
||
| 101 | { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function hasCode() : bool |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getCode() : int |
||
| 123 | { |
||
| 124 | foreach($this->results as $result) |
||
| 125 | { |
||
| 126 | if($result->hasCode()) |
||
| 127 | { |
||
| 128 | return $result->getCode(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return 0; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getMessage(string $type='') : string |
||
| 136 | { |
||
| 137 | foreach($this->results as $result) |
||
| 138 | { |
||
| 139 | $msg = $result->getMessage($type); |
||
| 140 | |||
| 141 | if(!empty($msg)) |
||
| 142 | { |
||
| 143 | return $msg; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return ''; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function containsCode(int $code) : bool |
||
| 151 | { |
||
| 152 | foreach($this->results as $result) |
||
| 153 | { |
||
| 154 | if($result->getCode() === $code) |
||
| 155 | { |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function countErrors() : int |
||
| 164 | { |
||
| 165 | return $this->countByType(self::TYPE_ERROR); |
||
| 166 | } |
||
| 167 | |||
| 168 | public function countWarnings() : int |
||
| 169 | { |
||
| 170 | return $this->countByType(self::TYPE_WARNING); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function countSuccesses() : int |
||
| 174 | { |
||
| 175 | return $this->countByType(self::TYPE_SUCCESS); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function countNotices() : int |
||
| 179 | { |
||
| 180 | return $this->countByType(self::TYPE_NOTICE); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function countByType(string $type) : int |
||
| 184 | { |
||
| 185 | $amount = 0; |
||
| 186 | |||
| 187 | foreach($this->results as $result) |
||
| 188 | { |
||
| 189 | if($result->isType($type)) |
||
| 190 | { |
||
| 191 | $amount++; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | return $amount; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function countResults() : int |
||
| 199 | { |
||
| 200 | return count($this->results); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function getErrors() : array |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getSuccesses() : array |
||
| 209 | { |
||
| 210 | return $this->getByType(self::TYPE_SUCCESS); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getWarnings() : array |
||
| 214 | { |
||
| 215 | return $this->getByType(self::TYPE_WARNING); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getNotices() : array |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getByType(string $type) : array |
||
| 224 | { |
||
| 225 | $results = array(); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function isType(string $type) : bool |
||
| 239 | { |
||
| 240 | foreach($this->results as $result) |
||
| 241 | { |
||
| 242 | if($result->isType($type)) |
||
| 243 | { |
||
| 244 | return true; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | return false; |
||
| 249 | } |
||
| 250 | |||
| 251 | public function getSummary() : string |
||
| 264 | } |
||
| 265 | } |
||
| 266 |