Complex classes like Pairwise 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 Pairwise, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Pairwise implements \ArrayAccess, \Iterator |
||
| 17 | { |
||
| 18 | use CondorcetVersion; |
||
| 19 | |||
| 20 | // Implement ArrayAccess |
||
| 21 | public function offsetSet($offset, $value) : void {} |
||
| 22 | |||
| 23 | public function offsetExists($offset) : bool { |
||
| 24 | return isset($this->_Pairwise[$offset]); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function offsetUnset($offset) : void {} |
||
| 28 | |||
| 29 | 38 | public function offsetGet($offset) : ?array { |
|
| 30 | 38 | return $this->_Pairwise[$offset] ?? null; |
|
| 31 | } |
||
| 32 | |||
| 33 | |||
| 34 | // Implement Iterator |
||
| 35 | private $valid = true; |
||
| 36 | |||
| 37 | 92 | public function rewind() : void { |
|
| 38 | 92 | reset($this->_Pairwise); |
|
| 39 | 92 | $this->valid = true; |
|
| 40 | 92 | } |
|
| 41 | |||
| 42 | 92 | public function current() : array { |
|
| 43 | 92 | return $this->_Pairwise[$this->key()]; |
|
| 44 | } |
||
| 45 | |||
| 46 | 92 | public function key() : ?int { |
|
| 47 | 92 | return key($this->_Pairwise); |
|
| 48 | } |
||
| 49 | |||
| 50 | 92 | public function next() : void { |
|
| 51 | 92 | if (next($this->_Pairwise) === false) : |
|
| 52 | 70 | $this->valid = false; |
|
| 53 | endif; |
||
| 54 | 92 | } |
|
| 55 | |||
| 56 | 92 | public function valid() : bool { |
|
| 57 | 92 | return $this->valid; |
|
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | // Pairwise |
||
| 62 | |||
| 63 | protected $_Election; |
||
| 64 | protected $_Pairwise_Model; |
||
| 65 | protected $_Pairwise; |
||
| 66 | |||
| 67 | 95 | public function __construct (Election $link) |
|
| 68 | { |
||
| 69 | 95 | $this->setElection($link); |
|
| 70 | 95 | $this->formatNewpairwise(); |
|
| 71 | 95 | $this->doPairwise(); |
|
| 72 | 95 | } |
|
| 73 | |||
| 74 | 1 | public function __clone () |
|
| 78 | |||
| 79 | 95 | public function setElection (Election $election) : void |
|
| 83 | |||
| 84 | 6 | public function addNewVote (int $key) : void |
|
| 85 | { |
||
| 86 | 6 | new Timer_Chrono ( $this->_Election->getTimerManager(), 'Add Vote To Pairwise' ); |
|
| 87 | |||
| 88 | 6 | $this->computeOneVote($this->_Pairwise,$this->_Election->getVotesManager()[$key]); |
|
| 89 | 6 | } |
|
| 90 | |||
| 91 | 4 | public function removeVotes (int $key) : void |
|
| 92 | { |
||
| 93 | 4 | new Timer_Chrono ( $this->_Election->getTimerManager(), 'Remove Vote To Pairwise' ); |
|
| 94 | |||
| 107 | |||
| 108 | 2 | public function getExplicitPairwise () : array |
|
| 128 | |||
| 129 | 95 | protected function formatNewpairwise () : void |
|
| 149 | |||
| 150 | 95 | protected function doPairwise () : void |
|
| 161 | |||
| 162 | 95 | protected function computeOneVote (array &$pairwise, Vote $oneVote) : void |
|
| 219 | |||
| 220 | } |
||
| 221 |