| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of sebastian/complexity.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace SebastianBergmann\Complexity;
|
|
|
11 |
|
|
|
12 |
use function get_class;
|
|
|
13 |
use PhpParser\Node;
|
|
|
14 |
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
15 |
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
16 |
use PhpParser\Node\Expr\BinaryOp\LogicalAnd;
|
|
|
17 |
use PhpParser\Node\Expr\BinaryOp\LogicalOr;
|
|
|
18 |
use PhpParser\Node\Expr\Ternary;
|
|
|
19 |
use PhpParser\Node\Stmt\Case_;
|
|
|
20 |
use PhpParser\Node\Stmt\Catch_;
|
|
|
21 |
use PhpParser\Node\Stmt\ElseIf_;
|
|
|
22 |
use PhpParser\Node\Stmt\For_;
|
|
|
23 |
use PhpParser\Node\Stmt\Foreach_;
|
|
|
24 |
use PhpParser\Node\Stmt\If_;
|
|
|
25 |
use PhpParser\Node\Stmt\While_;
|
|
|
26 |
use PhpParser\NodeVisitorAbstract;
|
|
|
27 |
|
|
|
28 |
final class CyclomaticComplexityCalculatingVisitor extends NodeVisitorAbstract
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* @var int
|
|
|
32 |
*/
|
|
|
33 |
private $cyclomaticComplexity = 1;
|
|
|
34 |
|
|
|
35 |
public function enterNode(Node $node): void
|
|
|
36 |
{
|
|
|
37 |
/* @noinspection GetClassMissUseInspection */
|
|
|
38 |
switch (get_class($node)) {
|
|
|
39 |
case BooleanAnd::class:
|
|
|
40 |
case BooleanOr::class:
|
|
|
41 |
case Case_::class:
|
|
|
42 |
case Catch_::class:
|
|
|
43 |
case ElseIf_::class:
|
|
|
44 |
case For_::class:
|
|
|
45 |
case Foreach_::class:
|
|
|
46 |
case If_::class:
|
|
|
47 |
case LogicalAnd::class:
|
|
|
48 |
case LogicalOr::class:
|
|
|
49 |
case Ternary::class:
|
|
|
50 |
case While_::class:
|
|
|
51 |
$this->cyclomaticComplexity++;
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function cyclomaticComplexity(): int
|
|
|
56 |
{
|
|
|
57 |
return $this->cyclomaticComplexity;
|
|
|
58 |
}
|
|
|
59 |
}
|