Complex classes like Vote 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 Vote, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Vote implements \Iterator |
||
20 | { |
||
21 | use Linkable, CondorcetVersion; |
||
22 | |||
23 | // Implement Iterator |
||
24 | |||
25 | private $position = 1; |
||
26 | |||
27 | 107 | public function rewind() : void { |
|
30 | |||
31 | 99 | public function current() { |
|
34 | |||
35 | 99 | public function key() : int { |
|
38 | |||
39 | 99 | public function next() : void { |
|
42 | |||
43 | 99 | public function valid() : bool { |
|
46 | |||
47 | // Vote |
||
48 | |||
49 | private $_ranking = []; |
||
50 | |||
51 | private $_weight = 1; |
||
52 | |||
53 | private $_tags = []; |
||
54 | |||
55 | private $_hashCode; |
||
56 | |||
57 | /// |
||
58 | |||
59 | 107 | public function __construct ($ranking, $tags = null, $ownTimestamp = false) |
|
60 | { |
||
61 | 107 | $tagsFromString = null; |
|
62 | // Vote Weight |
||
63 | 107 | if (is_string($ranking)) : |
|
64 | 90 | $is_voteWeight = mb_strpos($ranking, '^'); |
|
65 | 90 | if ($is_voteWeight !== false) : |
|
66 | 1 | $weight = intval( trim( substr($ranking, $is_voteWeight + 1) ) ); |
|
67 | 1 | $ranking = substr($ranking, 0,$is_voteWeight); |
|
68 | |||
69 | // Errors |
||
70 | 1 | if ( !is_numeric($weight) ) : |
|
71 | throw new CondorcetException(13, null); |
||
72 | endif; |
||
73 | endif; |
||
74 | |||
75 | 90 | $is_voteTags = mb_strpos($ranking, '||'); |
|
76 | 90 | if ($is_voteTags !== false) : |
|
77 | 2 | $tagsFromString = explode(',', trim( substr($ranking, 0, $is_voteTags) )); |
|
78 | 2 | $ranking = substr($ranking, $is_voteTags + 2); |
|
79 | endif; |
||
80 | endif; |
||
81 | |||
82 | 107 | $this->setRanking($ranking, $ownTimestamp); |
|
83 | 107 | $this->addTags($tags); |
|
84 | 107 | $this->addTags($tagsFromString); |
|
85 | |||
86 | 107 | if (isset($weight)) : |
|
87 | 1 | $this->setWeight($weight); |
|
88 | endif; |
||
89 | 107 | } |
|
90 | |||
91 | 2 | public function __sleep () : array |
|
92 | { |
||
93 | 2 | $this->position = 1; |
|
94 | |||
95 | 2 | return array_keys(get_object_vars($this)); |
|
96 | } |
||
97 | |||
98 | 2 | public function __clone () |
|
99 | { |
||
100 | 2 | $this->destroyAllLink(); |
|
101 | 2 | $this->setHashCode(); |
|
102 | 2 | } |
|
103 | |||
104 | 107 | public function __toString () : string { |
|
112 | |||
113 | public function getHashCode () : string { |
||
116 | |||
117 | /// |
||
118 | |||
119 | // GETTERS |
||
120 | |||
121 | 107 | public function getRanking () : ?array |
|
129 | |||
130 | 2 | public function getHistory () : array |
|
134 | |||
135 | |||
136 | 107 | public function getTags () : array |
|
140 | |||
141 | 11 | public function getTagsAsString () : string |
|
145 | |||
146 | 1 | public function getCreateTimestamp () : float |
|
147 | { |
||
148 | 1 | return $this->_ranking[0]['timestamp']; |
|
149 | } |
||
150 | |||
151 | 91 | public function getTimestamp () : float |
|
155 | |||
156 | public function countRankingCandidates () : int |
||
160 | |||
161 | 87 | public function getAllCandidates () : array |
|
173 | |||
174 | 86 | public function getContextualRanking (Election $election, bool $string = false) : array |
|
222 | |||
223 | 107 | public function getSimpleRanking (?Election $context = null) : string |
|
224 | { |
||
225 | 107 | $ranking = ($context) ? $this->getContextualRanking($context) : $this->getRanking(); |
|
226 | |||
227 | 107 | $simpleRanking = self::getRankingAsString($ranking); |
|
228 | |||
229 | 107 | if ($this->_weight > 1 && ( ($context && $context->isVoteWeightIsAllowed()) || $context === null ) ) : |
|
230 | 3 | $simpleRanking .= " ^".$this->getWeight(); |
|
231 | endif; |
||
232 | |||
233 | 107 | return $simpleRanking; |
|
234 | } |
||
235 | |||
236 | 107 | public static function getRankingAsString (array $ranking) : string |
|
247 | |||
248 | |||
249 | // SETTERS |
||
250 | |||
251 | 107 | public function setRanking ($rankingCandidate, $ownTimestamp = false) : bool |
|
284 | |||
285 | 107 | private function formatRanking (&$ranking) : int |
|
339 | |||
340 | // From a string like 'A>B=C=H>G=T>Q' |
||
341 | 91 | public static function convertVoteInput (string $formula) : array |
|
356 | |||
357 | |||
358 | 1 | public function removeCandidates (array $candidatesList) : bool |
|
359 | { |
||
360 | 1 | $ranking = $this->getRanking(); |
|
361 | |||
362 | 1 | if ($ranking === null) : |
|
363 | return false; |
||
364 | endif; |
||
365 | |||
366 | 1 | $rankingCandidate = $this->getAllCandidates(); |
|
367 | |||
368 | 1 | $canRemove = false; |
|
369 | 1 | foreach ($candidatesList as $oneCandidate) : |
|
370 | 1 | if (in_array($oneCandidate, $rankingCandidate, false)) : |
|
371 | 1 | $canRemove = true; |
|
372 | 1 | break; |
|
373 | endif; |
||
374 | endforeach; |
||
375 | |||
376 | 1 | if (!$canRemove) : |
|
377 | return false; |
||
378 | endif; |
||
379 | |||
380 | 1 | foreach ($ranking as $rankingKey => &$rank) : |
|
381 | 1 | foreach ($rank as $oneRankKey => $oneRankValue) : |
|
382 | 1 | if (in_array($oneRankValue, $candidatesList, false)) : |
|
383 | 1 | unset($rank[$oneRankKey]); |
|
384 | endif; |
||
385 | endforeach; |
||
386 | |||
387 | 1 | if (empty($rank)) : |
|
388 | 1 | unset($ranking[$rankingKey]); |
|
389 | endif; |
||
390 | endforeach; |
||
391 | |||
392 | 1 | $this->setRanking($ranking); |
|
393 | |||
394 | 1 | return true; |
|
395 | } |
||
396 | |||
397 | |||
398 | 107 | public function addTags ($tags) : bool |
|
427 | |||
428 | 2 | public function removeTags ($tags) : array |
|
429 | { |
||
430 | 2 | if (is_object($tags) || is_bool($tags)) : |
|
431 | throw new CondorcetException(17); |
||
432 | endif; |
||
433 | |||
434 | 2 | $tags = self::tagsConvert($tags); |
|
435 | |||
436 | 2 | if (empty($tags)) : |
|
437 | 1 | return []; |
|
438 | endif; |
||
439 | |||
440 | |||
441 | 2 | $rm = []; |
|
442 | 2 | foreach ($tags as $key => $tag) : |
|
443 | 2 | $tagK = array_search($tag, $this->_tags, true); |
|
444 | |||
445 | 2 | if ($tagK === false) : |
|
446 | 1 | unset($tags[$key]); |
|
447 | else : |
||
448 | 2 | $rm[] = $this->_tags[$tagK]; |
|
449 | 2 | unset($this->_tags[$tagK]); |
|
450 | endif; |
||
451 | endforeach; |
||
452 | |||
453 | 2 | $this->setHashCode(); |
|
454 | 2 | return $rm; |
|
455 | } |
||
456 | |||
457 | 2 | public function removeAllTags () : bool |
|
458 | { |
||
459 | 2 | $this->removeTags($this->getTags()); |
|
460 | 2 | return true; |
|
461 | } |
||
462 | |||
463 | 107 | public static function tagsConvert ($tags) : ?array |
|
464 | { |
||
465 | 107 | if (empty($tags)) : |
|
466 | 107 | return null; |
|
467 | endif; |
||
468 | |||
469 | // Make Array |
||
470 | 11 | if (!is_array($tags)) : |
|
471 | 11 | $tags = explode(',', $tags); |
|
472 | endif; |
||
473 | |||
474 | // Trim tags |
||
475 | 11 | foreach ($tags as $key => &$oneTag) : |
|
476 | 11 | if (empty($oneTag) || is_object($oneTag) || is_bool($oneTag)) {unset($tags[$key]); |
|
477 | 1 | continue; |
|
478 | } |
||
479 | |||
480 | 11 | $oneTag = (!ctype_digit($oneTag)) ? trim($oneTag) : intval($oneTag); |
|
481 | endforeach; |
||
482 | |||
483 | 11 | return $tags; |
|
484 | } |
||
485 | |||
486 | 71 | public function getWeight () : int |
|
490 | |||
491 | 71 | public function setWeight (int $newWeight) : int |
|
509 | |||
510 | /////////// INTERNAL /////////// |
||
511 | |||
512 | 107 | private function archiveRanking ($ranking, int $counter, $ownTimestamp) : void |
|
520 | |||
521 | 107 | private function setHashCode () : string |
|
525 | } |
||
526 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.