CyclomaticComplexityCalculatingVisitorTest.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. use function file_get_contents;
  12. use PhpParser\NodeTraverser;
  13. use PhpParser\ParserFactory;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @covers \SebastianBergmann\Complexity\CyclomaticComplexityCalculatingVisitor
  17. *
  18. * @small
  19. */
  20. final class CyclomaticComplexityCalculatingVisitorTest extends TestCase
  21. {
  22. public function testCalculatesCyclomaticComplexityForAbstractSyntaxTree(): void
  23. {
  24. $nodes = (new ParserFactory)->createForHostVersion()->parse(
  25. file_get_contents(__DIR__ . '/../_fixture/example_function.php')
  26. );
  27. $traverser = new NodeTraverser;
  28. $visitor = new CyclomaticComplexityCalculatingVisitor;
  29. $traverser->addVisitor($visitor);
  30. /* @noinspection UnusedFunctionResultInspection */
  31. $traverser->traverse($nodes);
  32. $this->assertSame(14, $visitor->cyclomaticComplexity());
  33. }
  34. }