CrapIndex.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of phpunit/php-code-coverage.
  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\CodeCoverage\Node;
  11. use function sprintf;
  12. /**
  13. * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
  14. */
  15. final class CrapIndex
  16. {
  17. /**
  18. * @var int
  19. */
  20. private $cyclomaticComplexity;
  21. /**
  22. * @var float
  23. */
  24. private $codeCoverage;
  25. public function __construct(int $cyclomaticComplexity, float $codeCoverage)
  26. {
  27. $this->cyclomaticComplexity = $cyclomaticComplexity;
  28. $this->codeCoverage = $codeCoverage;
  29. }
  30. public function asString(): string
  31. {
  32. if ($this->codeCoverage === 0.0) {
  33. return (string) ($this->cyclomaticComplexity ** 2 + $this->cyclomaticComplexity);
  34. }
  35. if ($this->codeCoverage >= 95) {
  36. return (string) $this->cyclomaticComplexity;
  37. }
  38. return sprintf(
  39. '%01.2F',
  40. $this->cyclomaticComplexity ** 2 * (1 - $this->codeCoverage / 100) ** 3 + $this->cyclomaticComplexity
  41. );
  42. }
  43. }