Util.php 932 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. class Util
  15. {
  16. /**
  17. * @param float $a
  18. * @param float $b
  19. * @param bool $asString
  20. * @param bool $fixedWidth
  21. *
  22. * @return float|int|string
  23. */
  24. public static function percent($a, $b, $asString = false, $fixedWidth = false)
  25. {
  26. if ($asString && $b == 0) {
  27. return '';
  28. }
  29. $percent = 100;
  30. if ($b > 0) {
  31. $percent = ($a / $b) * 100;
  32. }
  33. if ($asString) {
  34. $format = $fixedWidth ? '%6.2F%%' : '%01.2F%%';
  35. return sprintf($format, $percent);
  36. }
  37. return $percent;
  38. }
  39. }