├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src ├── Calculator.php ├── Complexity ├── Complexity.php ├── ComplexityCollection.php └── ComplexityCollectionIterator.php ├── Exception ├── Exception.php └── RuntimeException.php └── Visitor ├── ComplexityCalculatingVisitor.php └── CyclomaticComplexityCalculatingVisitor.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # ChangeLog 2 | 3 | All notable changes are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles. 4 | 5 | ## [5.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.2 10 | 11 | ## [4.0.1] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [4.0.0] - 2024-02-02 18 | 19 | ### Removed 20 | 21 | * This component now requires PHP-Parser 5 22 | * This component is no longer supported on PHP 8.1 23 | 24 | ## [3.2.0] - 2023-12-21 25 | 26 | ### Added 27 | 28 | * `ComplexityCollection::sortByDescendingCyclomaticComplexity()` 29 | * Support for `match` arms 30 | 31 | ### Changed 32 | 33 | * This component is now compatible with `nikic/php-parser` 5.0 34 | 35 | ## [3.1.0] - 2023-09-28 36 | 37 | ### Added 38 | 39 | * `Complexity::isFunction()` and `Complexity::isMethod()` 40 | * `ComplexityCollection::isFunction()` and `ComplexityCollection::isMethod()` 41 | * `ComplexityCollection::mergeWith()` 42 | 43 | ### Fixed 44 | 45 | * Anonymous classes are not processed correctly 46 | 47 | ## [3.0.1] - 2023-08-31 48 | 49 | ### Fixed 50 | 51 | * [#7](https://github.com/sebastianbergmann/complexity/pull/7): `ComplexityCalculatingVisitor` tries to process interface methods 52 | 53 | ## [3.0.0] - 2023-02-03 54 | 55 | ### Removed 56 | 57 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 58 | 59 | ## [2.0.2] - 2020-10-26 60 | 61 | ### Fixed 62 | 63 | * `SebastianBergmann\Complexity\Exception` now correctly extends `\Throwable` 64 | 65 | ## [2.0.1] - 2020-09-28 66 | 67 | ### Changed 68 | 69 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 70 | 71 | ## [2.0.0] - 2020-07-25 72 | 73 | ### Removed 74 | 75 | * The `ParentConnectingVisitor` has been removed (it should have been marked as `@internal`) 76 | 77 | ## [1.0.0] - 2020-07-22 78 | 79 | * Initial release 80 | 81 | [5.0.0]: https://github.com/sebastianbergmann/complexity/compare/4.0...5.0.0 82 | [4.0.1]: https://github.com/sebastianbergmann/complexity/compare/4.0.0...4.0.1 83 | [4.0.0]: https://github.com/sebastianbergmann/complexity/compare/3.2...4.0.0 84 | [3.2.0]: https://github.com/sebastianbergmann/complexity/compare/3.1.0...3.2.0 85 | [3.1.0]: https://github.com/sebastianbergmann/complexity/compare/3.0.1...3.1.0 86 | [3.0.1]: https://github.com/sebastianbergmann/complexity/compare/3.0.0...3.0.1 87 | [3.0.0]: https://github.com/sebastianbergmann/complexity/compare/2.0.2...3.0.0 88 | [2.0.2]: https://github.com/sebastianbergmann/complexity/compare/2.0.1...2.0.2 89 | [2.0.1]: https://github.com/sebastianbergmann/complexity/compare/2.0.0...2.0.1 90 | [2.0.0]: https://github.com/sebastianbergmann/complexity/compare/1.0.0...2.0.0 91 | [1.0.0]: https://github.com/sebastianbergmann/complexity/compare/70ee0ad32d9e2be3f85beffa3e2eb474193f2487...1.0.0 92 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/sebastian/complexity/v)](https://packagist.org/packages/sebastian/complexity) 2 | [![CI Status](https://github.com/sebastianbergmann/complexity/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/complexity/actions) 3 | [![codecov](https://codecov.io/gh/sebastianbergmann/complexity/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/complexity) 4 | 5 | # sebastian/complexity 6 | 7 | Library for calculating the complexity of PHP code units. 8 | 9 | ## Installation 10 | 11 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 12 | 13 | ``` 14 | composer require sebastian/complexity 15 | ``` 16 | 17 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 18 | 19 | ``` 20 | composer require --dev sebastian/complexity 21 | ``` 22 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sebastian/complexity", 3 | "description": "Library for calculating the complexity of PHP code units", 4 | "type": "library", 5 | "homepage": "https://github.com/sebastianbergmann/complexity", 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Sebastian Bergmann", 10 | "email": "sebastian@phpunit.de", 11 | "role": "lead" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/sebastianbergmann/complexity/issues", 16 | "security": "https://github.com/sebastianbergmann/complexity/security/policy" 17 | }, 18 | "prefer-stable": true, 19 | "require": { 20 | "php": ">=8.3", 21 | "nikic/php-parser": "^5.0" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^12.0" 25 | }, 26 | "config": { 27 | "platform": { 28 | "php": "8.3.0" 29 | }, 30 | "optimize-autoloader": true, 31 | "sort-packages": true 32 | }, 33 | "autoload": { 34 | "classmap": [ 35 | "src/" 36 | ] 37 | }, 38 | "extra": { 39 | "branch-alias": { 40 | "dev-main": "5.0-dev" 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Calculator.php: -------------------------------------------------------------------------------- 1 | 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 assert; 13 | use function file_exists; 14 | use function file_get_contents; 15 | use function is_readable; 16 | use function is_string; 17 | use PhpParser\Error; 18 | use PhpParser\Node; 19 | use PhpParser\NodeTraverser; 20 | use PhpParser\NodeVisitor\NameResolver; 21 | use PhpParser\NodeVisitor\ParentConnectingVisitor; 22 | use PhpParser\ParserFactory; 23 | 24 | final class Calculator 25 | { 26 | /** 27 | * @param non-empty-string $sourceFile 28 | * 29 | * @throws RuntimeException 30 | */ 31 | public function calculateForSourceFile(string $sourceFile): ComplexityCollection 32 | { 33 | assert(file_exists($sourceFile)); 34 | assert(is_readable($sourceFile)); 35 | 36 | $source = file_get_contents($sourceFile); 37 | 38 | assert(is_string($source)); 39 | 40 | return $this->calculateForSourceString($source); 41 | } 42 | 43 | /** 44 | * @throws RuntimeException 45 | */ 46 | public function calculateForSourceString(string $source): ComplexityCollection 47 | { 48 | try { 49 | $nodes = (new ParserFactory)->createForHostVersion()->parse($source); 50 | 51 | assert($nodes !== null); 52 | 53 | return $this->calculateForAbstractSyntaxTree($nodes); 54 | // @codeCoverageIgnoreStart 55 | } catch (Error $error) { 56 | throw new RuntimeException( 57 | $error->getMessage(), 58 | $error->getCode(), 59 | $error, 60 | ); 61 | } 62 | // @codeCoverageIgnoreEnd 63 | } 64 | 65 | /** 66 | * @param Node[] $nodes 67 | * 68 | * @throws RuntimeException 69 | */ 70 | public function calculateForAbstractSyntaxTree(array $nodes): ComplexityCollection 71 | { 72 | $traverser = new NodeTraverser; 73 | $complexityCalculatingVisitor = new ComplexityCalculatingVisitor(true); 74 | 75 | $traverser->addVisitor(new NameResolver); 76 | $traverser->addVisitor(new ParentConnectingVisitor); 77 | $traverser->addVisitor($complexityCalculatingVisitor); 78 | 79 | try { 80 | /* @noinspection UnusedFunctionResultInspection */ 81 | $traverser->traverse($nodes); 82 | // @codeCoverageIgnoreStart 83 | } catch (Error $error) { 84 | throw new RuntimeException( 85 | $error->getMessage(), 86 | $error->getCode(), 87 | $error, 88 | ); 89 | } 90 | // @codeCoverageIgnoreEnd 91 | 92 | return $complexityCalculatingVisitor->result(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/Complexity/Complexity.php: -------------------------------------------------------------------------------- 1 | 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 str_contains; 13 | 14 | /** 15 | * @immutable 16 | */ 17 | final readonly class Complexity 18 | { 19 | /** 20 | * @var non-empty-string 21 | */ 22 | private string $name; 23 | 24 | /** 25 | * @var positive-int 26 | */ 27 | private int $cyclomaticComplexity; 28 | 29 | /** 30 | * @param non-empty-string $name 31 | * @param positive-int $cyclomaticComplexity 32 | */ 33 | public function __construct(string $name, int $cyclomaticComplexity) 34 | { 35 | $this->name = $name; 36 | $this->cyclomaticComplexity = $cyclomaticComplexity; 37 | } 38 | 39 | /** 40 | * @return non-empty-string 41 | */ 42 | public function name(): string 43 | { 44 | return $this->name; 45 | } 46 | 47 | /** 48 | * @return positive-int 49 | */ 50 | public function cyclomaticComplexity(): int 51 | { 52 | return $this->cyclomaticComplexity; 53 | } 54 | 55 | public function isFunction(): bool 56 | { 57 | return !$this->isMethod(); 58 | } 59 | 60 | public function isMethod(): bool 61 | { 62 | return str_contains($this->name, '::'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Complexity/ComplexityCollection.php: -------------------------------------------------------------------------------- 1 | 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 array_filter; 13 | use function array_merge; 14 | use function array_reverse; 15 | use function array_values; 16 | use function count; 17 | use function usort; 18 | use Countable; 19 | use IteratorAggregate; 20 | 21 | /** 22 | * @template-implements IteratorAggregate 23 | * 24 | * @psalm-immutable 25 | */ 26 | final readonly class ComplexityCollection implements Countable, IteratorAggregate 27 | { 28 | /** 29 | * @var list 30 | */ 31 | private array $items; 32 | 33 | public static function fromList(Complexity ...$items): self 34 | { 35 | return new self(array_values($items)); 36 | } 37 | 38 | /** 39 | * @param list $items 40 | */ 41 | private function __construct(array $items) 42 | { 43 | $this->items = $items; 44 | } 45 | 46 | /** 47 | * @return list 48 | */ 49 | public function asArray(): array 50 | { 51 | return $this->items; 52 | } 53 | 54 | public function getIterator(): ComplexityCollectionIterator 55 | { 56 | return new ComplexityCollectionIterator($this); 57 | } 58 | 59 | /** 60 | * @return non-negative-int 61 | */ 62 | public function count(): int 63 | { 64 | return count($this->items); 65 | } 66 | 67 | public function isEmpty(): bool 68 | { 69 | return $this->items === []; 70 | } 71 | 72 | /** 73 | * @return non-negative-int 74 | */ 75 | public function cyclomaticComplexity(): int 76 | { 77 | $cyclomaticComplexity = 0; 78 | 79 | foreach ($this as $item) { 80 | $cyclomaticComplexity += $item->cyclomaticComplexity(); 81 | } 82 | 83 | return $cyclomaticComplexity; 84 | } 85 | 86 | public function isFunction(): self 87 | { 88 | return new self( 89 | array_values( 90 | array_filter( 91 | $this->items, 92 | static fn (Complexity $complexity): bool => $complexity->isFunction(), 93 | ), 94 | ), 95 | ); 96 | } 97 | 98 | public function isMethod(): self 99 | { 100 | return new self( 101 | array_values( 102 | array_filter( 103 | $this->items, 104 | static fn (Complexity $complexity): bool => $complexity->isMethod(), 105 | ), 106 | ), 107 | ); 108 | } 109 | 110 | public function mergeWith(self $other): self 111 | { 112 | return new self( 113 | array_merge( 114 | $this->asArray(), 115 | $other->asArray(), 116 | ), 117 | ); 118 | } 119 | 120 | public function sortByDescendingCyclomaticComplexity(): self 121 | { 122 | $items = $this->items; 123 | 124 | usort( 125 | $items, 126 | static function (Complexity $a, Complexity $b): int 127 | { 128 | return $a->cyclomaticComplexity() <=> $b->cyclomaticComplexity(); 129 | }, 130 | ); 131 | 132 | return new self(array_reverse($items)); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/Complexity/ComplexityCollectionIterator.php: -------------------------------------------------------------------------------- 1 | 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 Iterator; 13 | 14 | /** 15 | * @template-implements Iterator 16 | */ 17 | final class ComplexityCollectionIterator implements Iterator 18 | { 19 | /** 20 | * @var list 21 | */ 22 | private readonly array $items; 23 | private int $position = 0; 24 | 25 | public function __construct(ComplexityCollection $items) 26 | { 27 | $this->items = $items->asArray(); 28 | } 29 | 30 | public function rewind(): void 31 | { 32 | $this->position = 0; 33 | } 34 | 35 | public function valid(): bool 36 | { 37 | return isset($this->items[$this->position]); 38 | } 39 | 40 | public function key(): int 41 | { 42 | return $this->position; 43 | } 44 | 45 | public function current(): Complexity 46 | { 47 | return $this->items[$this->position]; 48 | } 49 | 50 | public function next(): void 51 | { 52 | $this->position++; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Exception/Exception.php: -------------------------------------------------------------------------------- 1 | 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 Throwable; 13 | 14 | interface Exception extends Throwable 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/Exception/RuntimeException.php: -------------------------------------------------------------------------------- 1 | 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 | final class RuntimeException extends \RuntimeException implements Exception 13 | { 14 | } 15 | -------------------------------------------------------------------------------- /src/Visitor/ComplexityCalculatingVisitor.php: -------------------------------------------------------------------------------- 1 | 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 assert; 13 | use function is_array; 14 | use PhpParser\Node; 15 | use PhpParser\Node\Expr\New_; 16 | use PhpParser\Node\Stmt; 17 | use PhpParser\Node\Stmt\Class_; 18 | use PhpParser\Node\Stmt\ClassMethod; 19 | use PhpParser\Node\Stmt\Function_; 20 | use PhpParser\Node\Stmt\Interface_; 21 | use PhpParser\Node\Stmt\Trait_; 22 | use PhpParser\NodeTraverser; 23 | use PhpParser\NodeVisitor; 24 | use PhpParser\NodeVisitorAbstract; 25 | 26 | final class ComplexityCalculatingVisitor extends NodeVisitorAbstract 27 | { 28 | /** 29 | * @var list 30 | */ 31 | private array $result = []; 32 | private bool $shortCircuitTraversal; 33 | 34 | public function __construct(bool $shortCircuitTraversal) 35 | { 36 | $this->shortCircuitTraversal = $shortCircuitTraversal; 37 | } 38 | 39 | public function enterNode(Node $node): ?int 40 | { 41 | if (!$node instanceof ClassMethod && !$node instanceof Function_) { 42 | return null; 43 | } 44 | 45 | if ($node instanceof ClassMethod) { 46 | if ($node->getAttribute('parent') instanceof Interface_) { 47 | return null; 48 | } 49 | 50 | if ($node->isAbstract()) { 51 | return null; 52 | } 53 | 54 | $name = $this->classMethodName($node); 55 | } else { 56 | $name = $this->functionName($node); 57 | } 58 | 59 | $statements = $node->getStmts(); 60 | 61 | assert(is_array($statements)); 62 | 63 | $this->result[] = new Complexity( 64 | $name, 65 | $this->cyclomaticComplexity($statements), 66 | ); 67 | 68 | if ($this->shortCircuitTraversal) { 69 | return NodeVisitor::DONT_TRAVERSE_CHILDREN; 70 | } 71 | 72 | return null; 73 | } 74 | 75 | public function result(): ComplexityCollection 76 | { 77 | return ComplexityCollection::fromList(...$this->result); 78 | } 79 | 80 | /** 81 | * @param Stmt[] $statements 82 | * 83 | * @return positive-int 84 | */ 85 | private function cyclomaticComplexity(array $statements): int 86 | { 87 | $traverser = new NodeTraverser; 88 | 89 | $cyclomaticComplexityCalculatingVisitor = new CyclomaticComplexityCalculatingVisitor; 90 | 91 | $traverser->addVisitor($cyclomaticComplexityCalculatingVisitor); 92 | 93 | /* @noinspection UnusedFunctionResultInspection */ 94 | $traverser->traverse($statements); 95 | 96 | return $cyclomaticComplexityCalculatingVisitor->cyclomaticComplexity(); 97 | } 98 | 99 | /** 100 | * @return non-empty-string 101 | */ 102 | private function classMethodName(ClassMethod $node): string 103 | { 104 | $parent = $node->getAttribute('parent'); 105 | 106 | assert($parent instanceof Class_ || $parent instanceof Trait_); 107 | 108 | if ($parent->getAttribute('parent') instanceof New_) { 109 | return 'anonymous class'; 110 | } 111 | 112 | assert(isset($parent->namespacedName)); 113 | 114 | return $parent->namespacedName->toString() . '::' . $node->name->toString(); 115 | } 116 | 117 | /** 118 | * @return non-empty-string 119 | */ 120 | private function functionName(Function_ $node): string 121 | { 122 | assert(isset($node->namespacedName)); 123 | 124 | return $node->namespacedName->toString(); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/Visitor/CyclomaticComplexityCalculatingVisitor.php: -------------------------------------------------------------------------------- 1 | 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 PhpParser\Node; 13 | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; 14 | use PhpParser\Node\Expr\BinaryOp\BooleanOr; 15 | use PhpParser\Node\Expr\BinaryOp\LogicalAnd; 16 | use PhpParser\Node\Expr\BinaryOp\LogicalOr; 17 | use PhpParser\Node\Expr\Ternary; 18 | use PhpParser\Node\Stmt\Case_; 19 | use PhpParser\Node\Stmt\Catch_; 20 | use PhpParser\Node\Stmt\ElseIf_; 21 | use PhpParser\Node\Stmt\For_; 22 | use PhpParser\Node\Stmt\Foreach_; 23 | use PhpParser\Node\Stmt\If_; 24 | use PhpParser\Node\Stmt\While_; 25 | use PhpParser\NodeVisitorAbstract; 26 | 27 | final class CyclomaticComplexityCalculatingVisitor extends NodeVisitorAbstract 28 | { 29 | /** 30 | * @var positive-int 31 | */ 32 | private int $cyclomaticComplexity = 1; 33 | 34 | public function enterNode(Node $node): null 35 | { 36 | switch ($node::class) { 37 | case BooleanAnd::class: 38 | case BooleanOr::class: 39 | case Case_::class: 40 | case Catch_::class: 41 | case ElseIf_::class: 42 | case For_::class: 43 | case Foreach_::class: 44 | case If_::class: 45 | case LogicalAnd::class: 46 | case LogicalOr::class: 47 | case Node\MatchArm::class: 48 | case Ternary::class: 49 | case While_::class: 50 | $this->cyclomaticComplexity++; 51 | } 52 | 53 | return null; 54 | } 55 | 56 | /** 57 | * @return positive-int 58 | */ 59 | public function cyclomaticComplexity(): int 60 | { 61 | return $this->cyclomaticComplexity; 62 | } 63 | } 64 | --------------------------------------------------------------------------------