| Total Complexity | 49 |
| Total Lines | 430 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractGraph 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 AbstractGraph, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class AbstractGraph implements GraphInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The vertex supplier |
||
| 28 | * |
||
| 29 | * @var SupplierInterface |
||
| 30 | */ |
||
| 31 | private $vertexSupplier; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The edge supplier |
||
| 35 | * |
||
| 36 | * @var SupplierInterface |
||
| 37 | */ |
||
| 38 | private $edgeSupplier; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The graph type |
||
| 42 | * |
||
| 43 | * @var GraphTypeInterface |
||
| 44 | */ |
||
| 45 | private $type; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The graph specifics |
||
| 49 | * |
||
| 50 | * @var SpecificsInterface |
||
| 51 | */ |
||
| 52 | private $specifics; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The edge specifics |
||
| 56 | * |
||
| 57 | * @var EdgeSpecificsInterface |
||
| 58 | */ |
||
| 59 | private $edgeSpecifics; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Construct a new graph |
||
| 63 | * |
||
| 64 | * @param SupplierInterface $vertexSupplier - the vertex supplier |
||
| 65 | * @param SupplierInterface $edgeSupplier - the edge supplier |
||
| 66 | * @param GraphTypeInterface $type - the graph type |
||
| 67 | */ |
||
| 68 | 33 | protected function __construct( |
|
| 69 | ?SupplierInterface $vertexSupplier = null, |
||
| 70 | ?SupplierInterface $edgeSupplier = null, |
||
| 71 | ?GraphTypeInterface $type = null |
||
| 72 | ) { |
||
| 73 | 33 | $this->vertexSupplier = $vertexSupplier; |
|
| 74 | 33 | $this->edgeSupplier = $edgeSupplier; |
|
| 75 | 33 | $this->type = $type; |
|
| 76 | 33 | $this->specifics = $this->createSpecifics($type->isDirected()); |
|
|
|
|||
| 77 | 33 | $this->edgeSpecifics = $this->createEdgeSpecifics($type->isWeighted()); |
|
| 78 | 33 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get the graph specifics |
||
| 82 | * |
||
| 83 | * @param bool $isDirected - is directed graph? |
||
| 84 | * |
||
| 85 | * @return SpecificsInterface |
||
| 86 | */ |
||
| 87 | 33 | public function createSpecifics(bool $isDirected): SpecificsInterface |
|
| 88 | { |
||
| 89 | 33 | return $isDirected ? new DirectedSpecifics($this) : new UndirectedSpecifics($this); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the edge specifics |
||
| 94 | * |
||
| 95 | * @param bool $isWeighted - is weighted? |
||
| 96 | * |
||
| 97 | * @return EdgeSpecificsInterface |
||
| 98 | */ |
||
| 99 | 33 | public function createEdgeSpecifics(bool $isWeighted): EdgeSpecificsInterface |
|
| 100 | { |
||
| 101 | 33 | return $isWeighted ? new WeightedEdgeSpecifics($this) : new UniformEdgeSpecifics($this); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get all edges connecting the source vertext to the target vertex |
||
| 106 | * |
||
| 107 | * @return EdgeSet |
||
| 108 | */ |
||
| 109 | 1 | public function getAllEdges(VertexInterface $sourceVertex, VertexInterface $targetVertex): EdgeSet |
|
| 110 | { |
||
| 111 | 1 | return $this->specifics->getAllEdges($sourceVertex, $targetVertex); |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get an edge connecting the source vertext to the target vertex |
||
| 116 | * |
||
| 117 | * @return null|EdgeInterface |
||
| 118 | */ |
||
| 119 | 22 | public function getEdge(VertexInterface $sourceVertex, VertexInterface $targetVertex): ?EdgeInterface |
|
| 120 | { |
||
| 121 | 22 | return $this->specifics->getEdge($sourceVertex, $targetVertex); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the vertex supplier that the graph uses whenever it needs to create new vertices |
||
| 126 | * |
||
| 127 | * @return null|SupplierInterface |
||
| 128 | */ |
||
| 129 | 2 | public function getVertexSupplier(): ?SupplierInterface |
|
| 130 | { |
||
| 131 | 2 | return $this->vertexSupplier; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the edge supplier that the graph uses whenever it needs to create new edges |
||
| 136 | * |
||
| 137 | * @return SupplierInterface |
||
| 138 | */ |
||
| 139 | 8 | public function getEdgeSupplier(): SupplierInterface |
|
| 140 | { |
||
| 141 | 8 | return $this->edgeSupplier; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the graph type |
||
| 146 | * |
||
| 147 | * @return GraphTypeInterface |
||
| 148 | */ |
||
| 149 | 25 | public function getType(): GraphTypeInterface |
|
| 150 | { |
||
| 151 | 25 | return $this->type; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Create a new edge in the graph. Return the newly created edge if added to the graph. |
||
| 156 | * |
||
| 157 | * @return EdgeInterface |
||
| 158 | * |
||
| 159 | * @throws InvalidArgumentException |
||
| 160 | */ |
||
| 161 | 22 | public function addEdge( |
|
| 162 | VertexInterface $sourceVertex, |
||
| 163 | VertexInterface $targetVertex, |
||
| 164 | ?EdgeInterface $edge = null |
||
| 165 | ): ?EdgeInterface { |
||
| 166 | 22 | $this->assertVertexExists($sourceVertex); |
|
| 167 | 22 | $this->assertVertexExists($targetVertex); |
|
| 168 | |||
| 169 | 22 | if (!$this->getType()->isAllowingMultipleEdges() && $this->containsEdge($sourceVertex, $targetVertex)) { |
|
| 170 | 2 | return null; |
|
| 171 | } |
||
| 172 | |||
| 173 | 22 | if (!$this->getType()->isAllowingSelfLoops() && $sourceVertex->equals($targetVertex)) { |
|
| 174 | throw new InvalidArgumentException("loops are not allowed"); |
||
| 175 | } |
||
| 176 | |||
| 177 | 22 | $targetEdge = $edge ?? $this->edgeSupplier->get(); |
|
| 178 | |||
| 179 | 22 | if ($this->edgeSpecifics->add($targetEdge, $sourceVertex, $targetVertex)) { |
|
| 180 | 22 | $this->specifics->addEdgeToTouchingVertices($targetEdge); |
|
| 181 | 22 | return $targetEdge; |
|
| 182 | } |
||
| 183 | |||
| 184 | return null; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Create and return a new vertex in the graph. |
||
| 189 | * |
||
| 190 | * @return null|VertexInterface |
||
| 191 | */ |
||
| 192 | 29 | public function addVertex(VertexInterface $vertex): ?VertexInterface |
|
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check if the graph contains the given edge, specified either by two vertices or by the edge itself |
||
| 204 | * |
||
| 205 | * @param VertexInterface $sourceVertex - the source vertex |
||
| 206 | * @param VertexInterface $targetVertex - the target vertex |
||
| 207 | * @param EdgeInterface $edge - the edge |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 21 | public function containsEdge( |
|
| 212 | ?VertexInterface $sourceVertex = null, |
||
| 213 | ?VertexInterface $targetVertex = null, |
||
| 214 | ?EdgeInterface $edge = null |
||
| 215 | ): bool { |
||
| 216 | 21 | if (!is_null($sourceVertex) && !is_null($targetVertex)) { |
|
| 217 | 21 | return !is_null($this->getEdge($sourceVertex, $targetVertex)); |
|
| 218 | } |
||
| 219 | 2 | return !is_null($edge) ? $this->edgeSpecifics->containsEdge($edge) : false; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if the graph contains the given vertex |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 29 | public function containsVertex(VertexInterface $vertex): bool |
|
| 228 | { |
||
| 229 | 29 | return $this->specifics->getVertexSet()->contains($vertex); |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get a set of all edges touching the specified vertex |
||
| 234 | * |
||
| 235 | * @param VertexInterface - the vertex |
||
| 236 | * |
||
| 237 | * @return EdgeSet |
||
| 238 | */ |
||
| 239 | 7 | public function edgesOf(VertexInterface $vertex): EdgeSet |
|
| 240 | { |
||
| 241 | 7 | $this->assertVertexExists($vertex); |
|
| 242 | 7 | return $this->specifics->edgesOf($vertex); |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get a set of all edges incoming into the specified vertex |
||
| 247 | * |
||
| 248 | * @param VertexInterface $vertex - the vertex |
||
| 249 | * |
||
| 250 | * @return EdgeSet |
||
| 251 | */ |
||
| 252 | 3 | public function incomingEdgesOf(VertexInterface $vertex): EdgeSet |
|
| 253 | { |
||
| 254 | 3 | $this->assertVertexExists($vertex); |
|
| 255 | 3 | return $this->specifics->incomingEdgesOf($vertex); |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get a set of all edges outgoing from the specified vertex |
||
| 260 | * |
||
| 261 | * @param VertexInterface $vertex - the vertex |
||
| 262 | * |
||
| 263 | * @return EdgeSet |
||
| 264 | */ |
||
| 265 | 3 | public function outgoingEdgesOf(VertexInterface $vertex): EdgeSet |
|
| 266 | { |
||
| 267 | 3 | $this->assertVertexExists($vertex); |
|
| 268 | 3 | return $this->specifics->outgoingEdgesOf($vertex); |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Remove all edges specified by two vertices or the the list of edges themselves. |
||
| 273 | * Return true, if graph was changed |
||
| 274 | * |
||
| 275 | * @param VertexInterface $vertex - the source vertex |
||
| 276 | * @param VertexInterface $vertex - the target vertex |
||
| 277 | * @param EdgeSet $edges - the edges |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 4 | public function removeAllEdges( |
|
| 282 | ?VertexInterface $sourceVertex = null, |
||
| 283 | ?VertexInterface $targetVertex = null, |
||
| 284 | ?EdgeSet $edges = null |
||
| 285 | ): bool { |
||
| 286 | 4 | $changed = false; |
|
| 287 | 4 | if (!is_null($sourceVertex) && !is_null($targetVertex)) { |
|
| 288 | 1 | $edge = $this->getEdge($sourceVertex, $targetVertex); |
|
| 289 | 1 | if (!is_null($edge)) { |
|
| 290 | 1 | $this->specifics->removeEdgeFromTouchingVertices($edge); |
|
| 291 | 1 | $this->edgeSpecifics->remove($edge); |
|
| 292 | 1 | return true; |
|
| 293 | } |
||
| 294 | } else { |
||
| 295 | 3 | foreach ($edges as $edge) { |
|
| 296 | 1 | if ($this->containsEdge(null, null, $edge)) { |
|
| 297 | 1 | $this->specifics->removeEdgeFromTouchingVertices($edge); |
|
| 298 | 1 | $this->edgeSpecifics->remove($edge); |
|
| 299 | 1 | $changed = true; |
|
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | 3 | return $changed; |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Remove all specified vertices contained in the graph. |
||
| 308 | * Return true, if graph was changed |
||
| 309 | * |
||
| 310 | * @param VertexSet $vertices - the vertices |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 1 | public function removeAllVertices(VertexSet $vertices): bool |
|
| 315 | { |
||
| 316 | 1 | $changed = false; |
|
| 317 | 1 | foreach ($vertices as $vertex) { |
|
| 318 | 1 | $changed = $this->removeVertex($vertex); |
|
| 319 | } |
||
| 320 | 1 | return $changed; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Remove the specifeid edge from the graph. |
||
| 325 | * Return the edge, if it was removed |
||
| 326 | * |
||
| 327 | * @param VertexInterface $sourceVertex - the source vertex |
||
| 328 | * @param VertexInterface $targetVertex - the target vertex |
||
| 329 | * @param EdgeInterface $edge - the edge |
||
| 330 | * |
||
| 331 | * @return null|EdgeInterface |
||
| 332 | */ |
||
| 333 | 2 | public function removeEdge( |
|
| 334 | ?VertexInterface $sourceVertex = null, |
||
| 335 | ?VertexInterface $targetVertex = null, |
||
| 336 | ?EdgeInterface $edge = null |
||
| 337 | ): ?EdgeInterface { |
||
| 338 | 2 | if (!is_null($sourceVertex) && !is_null($targetVertex)) { |
|
| 339 | 2 | $edge = $this->getEdge($sourceVertex, $targetVertex); |
|
| 340 | 2 | if (!is_null($edge)) { |
|
| 341 | 2 | $this->specifics->removeEdgeFromTouchingVertices($edge); |
|
| 342 | 2 | $this->edgeSpecifics->remove($edge); |
|
| 343 | 2 | return $edge; |
|
| 344 | } |
||
| 345 | 1 | } elseif (!is_null($edge)) { |
|
| 346 | 1 | $this->specifics->removeEdgeFromTouchingVertices($edge); |
|
| 347 | 1 | $this->edgeSpecifics->remove($edge); |
|
| 348 | 1 | return $edge; |
|
| 349 | } |
||
| 350 | return null; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Remove the specifeid vertex from the graph. |
||
| 355 | * Return the true, if it was removed |
||
| 356 | * |
||
| 357 | * @param VertexInterface $vertex - the vertex |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | 3 | public function removeVertex(VertexInterface $vertex): bool |
|
| 362 | { |
||
| 363 | 3 | if ($this->containsVertex($vertex)) { |
|
| 364 | 3 | $edges = $this->edgesOf($vertex); |
|
| 365 | 3 | $this->removeAllEdges(null, null, $edges); |
|
| 366 | 3 | $this->specifics->removeVertex($vertex); |
|
| 367 | 3 | return true; |
|
| 368 | } |
||
| 369 | 1 | return false; |
|
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the set of edges contained in the graph |
||
| 374 | * |
||
| 375 | * @return EdgeSet |
||
| 376 | */ |
||
| 377 | 11 | public function edgeSet(): EdgeSet |
|
| 378 | { |
||
| 379 | 11 | return $this->edgeSpecifics->getEdgeSet(); |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the set of vertices contained in the graph |
||
| 384 | * |
||
| 385 | * @return VertexSet |
||
| 386 | */ |
||
| 387 | 14 | public function vertexSet(): VertexSet |
|
| 388 | { |
||
| 389 | 14 | return $this->specifics->getVertexSet(); |
|
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the edge source vertex |
||
| 394 | * |
||
| 395 | * @param EdgeInterface $edge - the edge |
||
| 396 | * |
||
| 397 | * @return VertexInterface |
||
| 398 | */ |
||
| 399 | 22 | public function getEdgeSource(EdgeInterface $edge): VertexInterface |
|
| 400 | { |
||
| 401 | 22 | return $this->edgeSpecifics->getEdgeSource($edge); |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get the edge target vertex |
||
| 406 | * |
||
| 407 | * @param EdgeInterface $edge - the edge |
||
| 408 | * |
||
| 409 | * @return VertexInterface |
||
| 410 | */ |
||
| 411 | 22 | public function getEdgeTarget(EdgeInterface $edge): VertexInterface |
|
| 412 | { |
||
| 413 | 22 | return $this->edgeSpecifics->getEdgeTarget($edge); |
|
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the edge weight |
||
| 418 | * |
||
| 419 | * @param EdgeInterface $edge - the edge |
||
| 420 | * |
||
| 421 | * @return null|float |
||
| 422 | */ |
||
| 423 | 1 | public function getEdgeWeight(EdgeInterface $edge): ?float |
|
| 424 | { |
||
| 425 | 1 | return $this->edgeSpecifics->getEdgeWeight($edge); |
|
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Set the edge weight |
||
| 430 | * |
||
| 431 | * @param EdgeInterface $edge - the edge |
||
| 432 | * @param float $weight - the edge weight |
||
| 433 | */ |
||
| 434 | 2 | public function setEdgeWeight(EdgeInterface $edge, ?float $weight = null): void |
|
| 435 | { |
||
| 436 | 2 | $this->edgeSpecifics->setEdgeWeight($edge, $weight); |
|
| 437 | 2 | } |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Assert the the vertex exists |
||
| 441 | * |
||
| 442 | * @param VertexInterface - the vertex |
||
| 443 | * |
||
| 444 | * @return bool |
||
| 445 | * |
||
| 446 | * @throws InvalidArgumentException |
||
| 447 | */ |
||
| 448 | 24 | private function assertVertexExists(VertexInterface $vertex): bool |
|
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.