Complex classes like Dijkstra 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 Dijkstra, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Dijkstra { |
||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $points = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Dijkstra constructor. |
||
| 28 | * |
||
| 29 | * @param Closure|null $function Anonymous function to create relations. |
||
| 30 | */ |
||
| 31 | public function __construct($function = null) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Method executes sent function and add result to $points parameter. |
||
| 40 | * |
||
| 41 | * @param Closure $function Anonymous function to create relations. |
||
| 42 | * @return Dijkstra $this Reference to the same object. |
||
| 43 | * @throws Exception Method throws exception when $function argument isn't a Closure object. |
||
| 44 | */ |
||
| 45 | public function add($function) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Method search unvisited points in neighborhood of sent point. |
||
| 72 | * |
||
| 73 | * @param integer[] $visited Array of visited points' ids. |
||
| 74 | * @param integer $point Point id. |
||
| 75 | * @return boolean True when exists unvisited point in neighborhood or false otherwise. |
||
| 76 | */ |
||
| 77 | private function existsUnvisitedInNeighborhood($visited, $point) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param integer[] $unvisited Array of unvisited points' ids. |
||
| 90 | * @param integer[] $visited Array of visited points' ids. |
||
| 91 | * @param integer|null $startPoint Start point id. |
||
| 92 | * @return integer|null Method returns point identifier where it exists in $unvisited array or null otherwise. |
||
| 93 | */ |
||
| 94 | private function findPointWithUncheckedRelation($unvisited, $visited, $startPoint = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Method generates path for given Point object or point id. |
||
| 115 | * |
||
| 116 | * @param Point|integer $point Point object or point identification number. |
||
| 117 | * @return Path[] Array of Path objects. |
||
| 118 | * @throws Exception Throws exception when sent point not isset in object's $point array. |
||
| 119 | */ |
||
| 120 | public function generate($point) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Method generates paths for all defined points. |
||
| 165 | * |
||
| 166 | * @return Path[][] Two-dimensional array of Path objects. |
||
| 167 | * @throws Exception Method throws exception when generate() method throws exception. |
||
| 168 | */ |
||
| 169 | public function generateAll() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Method try to find minimal relation to another point from given point. |
||
| 182 | * |
||
| 183 | * @param integer[] $unvisited Array of unvisited points' ids. |
||
| 184 | * @param Path[] $paths Array of generated Path objects. |
||
| 185 | * @param integer $point Point integer identifier. |
||
| 186 | * @return Dijkstra\Relation|null Method returns Relation object or null when minimal relation wasn't found. |
||
| 187 | */ |
||
| 188 | private function getMinimalRelation($unvisited, $paths, $point) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Method gets from Point object identifier or checks given value. If this value not exists in $points object property then throws exception. |
||
| 210 | * |
||
| 211 | * @param mixed $point Value to check. |
||
| 212 | * @return integer Point identifier. |
||
| 213 | * @throws Exception Method throws exception when given value not exists in $points array. |
||
| 214 | */ |
||
| 215 | private function getPointID($point) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param array $reIndex New points' indexes. |
||
| 230 | */ |
||
| 231 | public function reIndex($reIndex) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Method updates existing Path object or create new if it's possible. |
||
| 258 | * |
||
| 259 | * @param Path[] &$paths Array of generated Path[] objects. Given by reference. |
||
| 260 | * @param integer $point Integer point identifier. |
||
| 261 | */ |
||
| 262 | public function updatePaths(&$paths, $point) |
||
| 276 | } |
||
| 277 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.