Total Complexity | 43 |
Total Lines | 294 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
13 | class GameOfLife |
||
14 | { |
||
15 | /** |
||
16 | * @var \DOMDocument |
||
17 | */ |
||
18 | private $xml; |
||
19 | |||
20 | /** |
||
21 | * @var Life |
||
22 | */ |
||
23 | public $life; |
||
24 | |||
25 | /** |
||
26 | * @var World |
||
27 | */ |
||
28 | private $world; |
||
29 | |||
30 | /** |
||
31 | * @var Organisms |
||
32 | */ |
||
33 | private $organisms; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $size; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $iterations; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $species; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $outputFilename = "out.xml"; |
||
54 | |||
55 | /** |
||
56 | * GameOfLife constructor. |
||
57 | * |
||
58 | * @param string $filename |
||
59 | * @param string|null $outputFilename |
||
60 | */ |
||
61 | public function __construct(string $filename, string $outputFilename = null) |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Loads the inital XML file. Always runs. |
||
81 | * |
||
82 | * @param $filename |
||
83 | * @return bool |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | private function loadXML($filename) |
||
87 | { |
||
88 | if (!file_exists($filename)) { |
||
89 | throw new \Exception('File not found.'); |
||
90 | } |
||
91 | |||
92 | $handler = fopen($filename, "r"); |
||
93 | |||
94 | if (!$handler) { |
||
95 | throw new \Exception('File open failed.'); |
||
96 | } |
||
97 | |||
98 | $xmlString = stream_get_contents($handler); |
||
99 | $this->xml = new \DOMDocument(); |
||
100 | $this->xml->loadXML($xmlString); |
||
101 | fclose($handler); |
||
102 | |||
103 | return true; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Starts the game |
||
108 | * |
||
109 | * @param bool $verbose |
||
110 | * @return bool |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | public function start($verbose = true) |
||
114 | { |
||
115 | if ($this->organisms == null || $this->species == null || $this->iterations == null) { |
||
116 | throw new \Exception('Not ready yet'); |
||
117 | } |
||
118 | |||
119 | $this->life->start($verbose); |
||
120 | |||
121 | if (!$this->life->isEnded()) { |
||
122 | throw new \Exception('You cannot generate output while life continues'); |
||
123 | } |
||
124 | |||
125 | if ($this->life->isEnded()) { |
||
126 | self::createXMLfromCells("data/" . $this->outputFilename, $this->organisms->getCells(), $this->species, $this->iterations); |
||
127 | } |
||
128 | |||
129 | return true; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Creates the life of game. |
||
134 | * |
||
135 | * @throws \Exception |
||
136 | */ |
||
137 | private function createLife() |
||
138 | { |
||
139 | if ($this->organisms !== null && $this->world !== null) { |
||
140 | $this->life = new Life($this->world, $this->organisms); |
||
141 | } else { |
||
142 | throw new \Exception('Not ready yet'); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Creates the World of game. |
||
148 | * |
||
149 | * @throws \Exception |
||
150 | */ |
||
151 | private function createWorld() |
||
152 | { |
||
153 | if ($this->size == null || $this->species == null || $this->iterations == null) { |
||
154 | throw new \Exception('Not ready yet'); |
||
155 | } |
||
156 | |||
157 | if ($this->size < 0 || $this->species < 0 || $this->iterations < 0) { |
||
158 | throw new \InvalidArgumentException("World arguments should be positive."); |
||
159 | } |
||
160 | |||
161 | $this->world = new World($this->size, $this->species, $this->iterations); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Creates organisms of game. |
||
166 | * |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | private function createOrganisms() |
||
170 | { |
||
171 | if ($this->size == null || $this->species == null) { |
||
172 | throw new \Exception('Not ready yet'); |
||
173 | } |
||
174 | |||
175 | if ($this->size < 0 || $this->species < 0) { |
||
176 | throw new \InvalidArgumentException("Organisms arguments should be positive."); |
||
177 | } |
||
178 | |||
179 | $cellsArray = self::createCellsFromXML($this->xml); |
||
180 | $this->checkCells($cellsArray, $this->species); |
||
181 | |||
182 | $this->organisms = new Organisms($cellsArray); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Helper function to generate array of cells from XML DomDocument. |
||
187 | * |
||
188 | * @param \DOMDocument $xml |
||
189 | * @return array |
||
190 | */ |
||
191 | public static function createCellsFromXML(\DOMDocument $xml) |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Creates XML file from matrix |
||
215 | * |
||
216 | * @param $filename |
||
217 | * @param array $matrix |
||
218 | * @param int $species |
||
219 | * @param int $iterations |
||
220 | */ |
||
221 | public static function createXMLfromCells($filename, array $matrix, int $species, int $iterations) |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Initializes a multidimensional array of zeros |
||
259 | * |
||
260 | * @param int $size |
||
261 | * @return array |
||
262 | */ |
||
263 | public static function createSquareMatrixWithZeors(int $size) |
||
264 | { |
||
265 | |||
266 | $matrix = array_fill(0, $size, array_fill(0, $size, 0)); |
||
267 | return $matrix; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Creates a random array with integers up to $max |
||
272 | * |
||
273 | * @param int $n |
||
274 | * @param int $m |
||
275 | * @param int $max |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | public static function createRandomMatrix(int $n, int $m, int $max) |
||
280 | { |
||
281 | $matrix = array_fill(0, $n, array_fill(0, $m, 0)); |
||
282 | |||
283 | for ($i = 0; $i < $n; $i++) { |
||
284 | for ($j = 0; $j < $m; $j++) { |
||
285 | $matrix[$i][$j] = rand(0, $max); |
||
286 | } |
||
287 | } |
||
288 | |||
289 | return $matrix; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Checks if array cells are valid. |
||
294 | * |
||
295 | * @param int[][] $cells |
||
296 | * @param int $species |
||
297 | */ |
||
298 | private function checkCells(array $cells, int $species) |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | } |