Total Complexity | 45 |
Total Lines | 291 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 | /** |
||
35 | * @param string $message |
||
36 | * @param int $code |
||
37 | * @return $this |
||
38 | */ |
||
39 | public function makeError(string $message, int $code=0) : OperationResult |
||
40 | { |
||
41 | return $this->add('makeError', $message, $code); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $message |
||
46 | * @param int $code |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function makeSuccess(string $message, int $code=0) : OperationResult |
||
50 | { |
||
51 | return $this->add('makeSuccess', $message, $code); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param string $message |
||
56 | * @param int $code |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function makeWarning(string $message, int $code=0) : OperationResult |
||
60 | { |
||
61 | return $this->add('makeWarning', $message, $code); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $message |
||
66 | * @param int $code |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function makeNotice(string $message, int $code=0) : OperationResult |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $method |
||
76 | * @param string $message |
||
77 | * @param int $code |
||
78 | * @return $this |
||
79 | */ |
||
80 | protected function add(string $method, string $message, int $code=0) : OperationResult |
||
81 | { |
||
82 | $result = new OperationResult($this->subject); |
||
83 | $result->$method($message, $code); |
||
84 | |||
85 | $this->results[] = $result; |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Adds a result to the collection. |
||
92 | * |
||
93 | * @param OperationResult $result |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function addResult(OperationResult $result) : OperationResult_Collection |
||
97 | { |
||
98 | if($result instanceof OperationResult_Collection) |
||
99 | { |
||
100 | return $this->importCollection($result); |
||
101 | } |
||
102 | |||
103 | $this->results[] = $result; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Merges the target collection's results with this collection. |
||
110 | * |
||
111 | * @param OperationResult_Collection $collection |
||
112 | * @return $this |
||
113 | */ |
||
114 | private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection |
||
115 | { |
||
116 | $results = $collection->getResults(); |
||
117 | |||
118 | foreach($results as $result) |
||
119 | { |
||
120 | $this->addResult($result); |
||
121 | } |
||
122 | |||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return OperationResult[] |
||
128 | */ |
||
129 | public function getResults() : array |
||
130 | { |
||
131 | return $this->results; |
||
132 | } |
||
133 | |||
134 | public function isValid() : bool |
||
135 | { |
||
136 | foreach($this->results as $result) |
||
137 | { |
||
138 | if(!$result->isValid()) |
||
139 | { |
||
140 | return false; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return true; |
||
145 | } |
||
146 | |||
147 | public function hasCode() : bool |
||
158 | } |
||
159 | |||
160 | public function getCode() : int |
||
161 | { |
||
162 | foreach($this->results as $result) |
||
163 | { |
||
164 | if($result->hasCode()) |
||
165 | { |
||
166 | return $result->getCode(); |
||
167 | } |
||
168 | } |
||
169 | |||
170 | return 0; |
||
171 | } |
||
172 | |||
173 | public function getMessage(string $type='') : string |
||
174 | { |
||
175 | foreach($this->results as $result) |
||
176 | { |
||
177 | $msg = $result->getMessage($type); |
||
178 | |||
179 | if(!empty($msg)) |
||
180 | { |
||
181 | return $msg; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return ''; |
||
186 | } |
||
187 | |||
188 | public function containsCode(int $code) : bool |
||
189 | { |
||
190 | foreach($this->results as $result) |
||
191 | { |
||
192 | if($result->getCode() === $code) |
||
193 | { |
||
194 | return true; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | return false; |
||
199 | } |
||
200 | |||
201 | public function countErrors() : int |
||
204 | } |
||
205 | |||
206 | public function countWarnings() : int |
||
207 | { |
||
208 | return $this->countByType(self::TYPE_WARNING); |
||
209 | } |
||
210 | |||
211 | public function countSuccesses() : int |
||
212 | { |
||
214 | } |
||
215 | |||
216 | public function countNotices() : int |
||
217 | { |
||
218 | return $this->countByType(self::TYPE_NOTICE); |
||
219 | } |
||
220 | |||
221 | public function countByType(string $type) : int |
||
234 | } |
||
235 | |||
236 | public function countResults() : int |
||
237 | { |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return OperationResult[] |
||
243 | */ |
||
244 | public function getErrors() : array |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return OperationResult[] |
||
251 | */ |
||
252 | public function getSuccesses() : array |
||
253 | { |
||
254 | return $this->getByType(self::TYPE_SUCCESS); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return OperationResult[] |
||
259 | */ |
||
260 | public function getWarnings() : array |
||
261 | { |
||
262 | return $this->getByType(self::TYPE_WARNING); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return OperationResult[] |
||
267 | */ |
||
268 | public function getNotices() : array |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param string $type |
||
275 | * @return OperationResult[] |
||
276 | */ |
||
277 | public function getByType(string $type) : array |
||
290 | } |
||
291 | |||
292 | public function isType(string $type) : bool |
||
293 | { |
||
294 | foreach($this->results as $result) |
||
295 | { |
||
296 | if($result->isType($type)) |
||
297 | { |
||
298 | return true; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | return false; |
||
303 | } |
||
304 | |||
305 | public function getSummary() : string |
||
318 | } |
||
319 | } |
||
320 |