Util.php 845 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  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;
  11. /**
  12. * Utility methods.
  13. */
  14. final class Util
  15. {
  16. /**
  17. * @return float|int|string
  18. */
  19. public static function percent(float $a, float $b, bool $asString = false, bool $fixedWidth = false)
  20. {
  21. if ($asString && $b == 0) {
  22. return '';
  23. }
  24. $percent = 100;
  25. if ($b > 0) {
  26. $percent = ($a / $b) * 100;
  27. }
  28. if ($asString) {
  29. $format = $fixedWidth ? '%6.2F%%' : '%01.2F%%';
  30. return \sprintf($format, $percent);
  31. }
  32. return $percent;
  33. }
  34. }