| Total Complexity | 41 |
| Total Lines | 216 |
| Duplicated Lines | 2.78 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like GameOfLife 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 GameOfLife, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class GameOfLife |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var \DOMDocument |
||
| 10 | */ |
||
| 11 | private $xml; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Life |
||
| 15 | */ |
||
| 16 | private $life; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var World |
||
| 20 | */ |
||
| 21 | private $world; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Organisms |
||
| 25 | */ |
||
| 26 | private $organisms; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private $size; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | private $iterations; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | private $species; |
||
| 42 | |||
| 43 | private $outputFilename = "out.xml"; |
||
| 44 | |||
| 45 | public function __construct(string $filename, string $outputFilename = null) |
||
| 61 | } |
||
| 62 | |||
| 63 | private function loadXML($filename) |
||
| 64 | { |
||
| 65 | if (!file_exists($filename)) { |
||
| 66 | throw new \Exception('File not found.'); |
||
| 67 | } |
||
| 68 | |||
| 69 | $handler = fopen($filename, "r"); |
||
| 70 | |||
| 71 | if (!$handler) { |
||
| 72 | throw new \Exception('File open failed.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | $xmlString = stream_get_contents($handler); |
||
| 76 | $this->xml = new \DOMDocument(); |
||
| 77 | $this->xml->loadXML($xmlString); |
||
| 78 | fclose($handler); |
||
| 79 | |||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function start($verbose = true) |
||
| 96 | } |
||
| 97 | |||
| 98 | public function generateOutput() |
||
| 99 | { |
||
| 100 | if ($this->organisms == null || $this->world == null || $this->life !== null) { |
||
| 101 | throw new \Exception('Not ready yet'); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!$this->life->isEnded()) { |
||
| 105 | throw new \Exception('You cannot generate output while life continues'); |
||
| 106 | } |
||
| 107 | |||
| 108 | self::createXMLfromCells( |
||
| 109 | "out.xml", |
||
| 110 | $this->organisms->getCells(), |
||
| 111 | $this->world->getSpecies(), |
||
| 112 | $this->world->getIterations() |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | private function createLife() |
||
| 117 | { |
||
| 118 | if ($this->organisms !== null && $this->world !== null) { |
||
| 119 | $this->life = new Life($this->world, $this->organisms); |
||
| 120 | } else { |
||
| 121 | throw new \Exception('Not ready yet'); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | private function createWorld() |
||
| 136 | } |
||
| 137 | |||
| 138 | private function createOrganisms() |
||
| 139 | { |
||
| 140 | View Code Duplication | if ($this->size == null || $this->species == null) { |
|
| 141 | throw new \Exception('Not ready yet'); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($this->size < 0 || $this->species < 0) { |
||
| 145 | throw new \InvalidArgumentException("Organisms arguments should be positive."); |
||
| 146 | } |
||
| 147 | $cellsArray = self::createSquareMatrixWithZeors($this->size); |
||
| 148 | $organisms = $this->xml->getElementsByTagName('organism'); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var \DOMElement $organism |
||
| 152 | */ |
||
| 153 | foreach ($organisms as $organism) { |
||
| 154 | $j = $organism->getElementsByTagName('x_pos')->item(0)->nodeValue; |
||
| 155 | $i = $organism->getElementsByTagName('y_pos')->item(0)->nodeValue; |
||
| 156 | $value = $organism->getElementsByTagName('species')->item(0)->nodeValue; |
||
| 157 | |||
| 158 | $cellsArray[$i][$j] = $value; |
||
| 159 | } |
||
| 160 | |||
| 161 | $this->checkCells($cellsArray, $this->species); |
||
| 162 | |||
| 163 | $this->organisms = new Organisms($cellsArray); |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function createXMLfromCells($filename, array $matrix, int $species, int $iterations) |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $size |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public static function createSquareMatrixWithZeors(int $size) |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param int $n |
||
| 215 | * @param int $m |
||
| 216 | * @param int $max |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public static function createRandomMatrix(int $n, int $m, int $max) |
||
| 221 | { |
||
| 222 | $matrix = array_fill(0, $n, array_fill(0, $m, 0)); |
||
| 251 | } |