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
11namespace SebastianBergmann\CodeCoverage;
12
13/**
14 * Utility methods.
15 */
16class Util
17{
18    /**
19     * @param float $a
20     * @param float $b
21     * @param bool  $asString
22     * @param bool  $fixedWidth
23     *
24     * @return float|int|string
25     */
26    public static function percent($a, $b, $asString = false, $fixedWidth = false)
27    {
28        if ($asString && $b == 0) {
29            return '';
30        }
31
32        if ($b > 0) {
33            $percent = ($a / $b) * 100;
34        } else {
35            $percent = 100;
36        }
37
38        if ($asString) {
39            if ($fixedWidth) {
40                return sprintf('%6.2F%%', $percent);
41            }
42
43            return sprintf('%01.2F%%', $percent);
44        } else {
45            return $percent;
46        }
47    }
48}
49