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 Board 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 Board, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | final class Board |
||
| 6 | { |
||
| 7 | private $width; |
||
| 8 | private $height; |
||
| 9 | private $fieldTypes = []; |
||
| 10 | private $fields = []; |
||
| 11 | private $actors = []; |
||
| 12 | private $exits = []; |
||
| 13 | private $functions = []; |
||
| 14 | private $variables = []; |
||
| 15 | private $actions = []; |
||
| 16 | |||
| 17 | private static $moveMap = [ |
||
| 18 | 'left' => [0, -1], |
||
| 19 | 'right' => [0, 1], |
||
| 20 | 'up' => [-1, 0], |
||
| 21 | 'down' => [1, 0], |
||
| 22 | ]; |
||
| 23 | |||
| 24 | public function __construct(int $width, int $height) |
||
| 29 | |||
| 30 | /* --- METHODS --- */ |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param ActionInterface[] $actions |
||
| 34 | */ |
||
| 35 | public function addActions(array $actions) |
||
| 41 | |||
| 42 | public function addAction(ActionInterface $action) |
||
| 46 | |||
| 47 | public function getAction(string $alias): ActionInterface |
||
| 55 | |||
| 56 | public function addFieldTypes(array $types) |
||
| 62 | |||
| 63 | public function hasAction($alias): bool |
||
| 67 | |||
| 68 | public function addFieldType($alias, $symbol) |
||
| 72 | |||
| 73 | public function loadFromString($string) |
||
| 86 | |||
| 87 | public function addActor($alias, $x, $y, $direction) |
||
| 97 | |||
| 98 | public function addExit($alias, $x, $y) |
||
| 105 | |||
| 106 | public function addFunctions(array $functions) |
||
| 112 | |||
| 113 | public function addFunction($name, array $program) |
||
| 117 | |||
| 118 | public function isActorAtExit($actor, $exit): bool |
||
| 125 | |||
| 126 | public function runActor($alias) |
||
| 130 | |||
| 131 | public function runActorProgram($alias, array $program) |
||
| 137 | |||
| 138 | private function runActorOperation($alias, array $operation) |
||
| 142 | |||
| 143 | public function getActorNextMove($alias): array |
||
| 150 | |||
| 151 | public function setVariable($name, $value) |
||
| 155 | |||
| 156 | public function getVariable($name) |
||
| 160 | |||
| 161 | public function getFunction($name) |
||
| 165 | |||
| 166 | public function moveActor($alias) |
||
| 189 | |||
| 190 | public function debug(...$args) |
||
| 193 | |||
| 194 | public function setField($x, $y, $field) |
||
| 198 | |||
| 199 | public function getField($x, $y) |
||
| 203 | |||
| 204 | public function getActorDirection($alias) |
||
| 208 | |||
| 209 | public function setActorDirection($alias, $direction) |
||
| 213 | |||
| 214 | public function getActorPick($alias) |
||
| 218 | |||
| 219 | public function setActorPick($alias, $pick) |
||
| 223 | |||
| 224 | public function getActorPosition($alias): array |
||
| 228 | |||
| 229 | public function renderBoard(): string |
||
| 257 | |||
| 258 | /* --- GETTERS --- */ |
||
| 259 | |||
| 260 | public function getWidth(): int |
||
| 264 | |||
| 265 | public function getHeight(): int |
||
| 269 | } |
||
| 270 |
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.