| Conditions | 25 |
| Paths | 62 |
| Total Lines | 113 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 238 | 8 | public function verify(): bool |
|
| 239 | { |
||
| 240 | 8 | if ($this->isEmpty()) { |
|
| 241 | 1 | return true; |
|
| 242 | } |
||
| 243 | |||
| 244 | 8 | if (!empty($this->vertexList)) { |
|
| 245 | 7 | if (!$this->startVertex->equals($this->vertexList[0])) { |
|
| 246 | throw new InvalidArgumentException( |
||
| 247 | "The start vertex must be the first vertex in the vertex list" |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | 7 | if (!$this->endVertex->equals($this->vertexList[count($this->vertexList) - 1])) { |
|
| 251 | throw new InvalidArgumentException( |
||
| 252 | "The end vertex must be the last vertex in the vertex list" |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | // All vertices and edges in the path must be contained in the graph |
||
| 256 | 7 | if (count(array_diff($this->vertexList, $this->graph->vertexSet()->getArrayCopy())) >= 1) { |
|
| 257 | throw new InvalidArgumentException( |
||
| 258 | "Not all vertices in the path are contained in the graph" |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 262 | 7 | if (empty($this->edgeList)) { |
|
| 263 | // Verify sequence |
||
| 264 | 7 | $vertexIterator = (new ArrayObject($this->vertexList))->getIterator(); |
|
| 265 | 7 | $sourceVertex = $vertexIterator->current(); |
|
| 266 | 7 | $vertexIterator->next(); |
|
| 267 | 7 | while ($vertexIterator->valid()) { |
|
| 268 | 6 | $targetVertex = $vertexIterator->current(); |
|
| 269 | 6 | if (is_null($this->graph->getEdge($sourceVertex, $targetVertex))) { |
|
| 270 | 2 | throw new InvalidArgumentException( |
|
| 271 | "The vertexList does not constitute to a feasible path. Edge (" . |
||
| 272 | 2 | (string) $sourceVertex . "," . (string) $targetVertex . ") does not exist in the graph." |
|
| 273 | ); |
||
| 274 | } |
||
| 275 | 5 | $vertexIterator->next(); |
|
| 276 | 5 | $sourceVertex = $targetVertex; |
|
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | 6 | if (!empty($this->edgeList)) { |
|
| 282 | 2 | if (!GraphUtils::testIncidence($this->graph, $this->edgeList[0], $this->startVertex)) { |
|
| 283 | throw new InvalidArgumentException( |
||
| 284 | "The first edge in the edge list must leave the start vertex" |
||
| 285 | ); |
||
| 286 | } |
||
| 287 | 2 | if (count(array_diff($this->edgeList, $this->graph->edgeSet()->getArrayCopy())) >= 1) { |
|
| 288 | throw new InvalidArgumentException( |
||
| 289 | "Not all edges in the path are contained in the graph" |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | 2 | if (empty($this->vertexList)) { |
|
| 294 | 2 | $vertex = $this->startVertex; |
|
| 295 | 2 | foreach ($this->edgeList as $edge) { |
|
| 296 | 2 | if (!GraphUtils::testIncidence($this->graph, $edge, $vertex)) { |
|
| 297 | 1 | throw new InvalidArgumentException( |
|
| 298 | "The edgeList does not constitute to a feasible path. Conflicting edge: " . |
||
| 299 | 1 | (string) $edge |
|
| 300 | ); |
||
| 301 | } |
||
| 302 | 2 | $vertex = GraphUtils::getOppositeVertex($this->graph, $edge, $vertex); |
|
| 303 | } |
||
| 304 | 1 | if (!$vertex->equals($this->endVertex)) { |
|
| 305 | throw new InvalidArgumentException( |
||
| 306 | "The path defined by the edgeList does not end in the endVertex." |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | 5 | if (!empty($this->vertexList) && !empty($this->edgeList)) { |
|
| 313 | // Verify that the path is an actual path in the graph |
||
| 314 | if (count($this->edgeList) + 1 != count($this->vertexList)) { |
||
| 315 | throw new InvalidArgumentException( |
||
| 316 | "VertexList and edgeList do not correspond to the same path (cardinality of " . |
||
| 317 | "vertexList +1 must equal the cardinality of the edgeList)" |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | $len = count($this->vertexList) - 1; |
||
| 322 | $edges = $this->getEdgeList(); |
||
| 323 | for ($i = 0; $i < $len; $i += 1) { |
||
| 324 | $startVertex = $this->vertexList[$i]; |
||
| 325 | $endVertex = $this->vertexList[$i + 1]; |
||
| 326 | $edge = $edges[$i]; |
||
| 327 | |||
| 328 | if ($this->graph->getType()->isDirected()) { |
||
| 329 | if ( |
||
| 330 | !$this->graph->getEdgeSource($edge)->equals($startVertex) |
||
| 331 | || !$this->graph->getEdgeTarget($edge)->equals($endVertex) |
||
| 332 | ) { |
||
| 333 | throw new InvalidArgumentException( |
||
| 334 | "VertexList and edgeList do not form a feasible path" |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | if ( |
||
| 339 | !GraphUtils::testIncidence($this->graph, $edge, $startVertex) |
||
| 340 | || !GraphUtils::getOppositeVertex($this->graph, $edge, $startVertex)->equals($endVertex) |
||
| 341 | ) { |
||
| 342 | throw new InvalidArgumentException( |
||
| 343 | "VertexList and edgeList do not form a feasible path" |
||
| 344 | ); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | 5 | return true; |
|
| 351 | } |
||
| 463 |