Completed
Push — master ( 997958...c1a544 )
by Adam
02:44
created

Output::outputClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php
2
3
namespace BestServedCold\Benchmark;
4
5
use BestServedCold\Benchmark\Output\Console;
6
use BestServedCold\Benchmark\Output\Html;
7
use BestServedCold\Benchmark\Output\OutputInterface;
8
9
/**
10
 * Class Output
11
 *
12
 * @package BestServedCold\Benchmark
13
 */
14
class Output
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $interfaces = [
20
        Console::class => [
21
            'cli',
22
            'cli-server'
23
        ],
24
        Html::class    => [
25
            'aolserver',
26
            'apache',
27
            'apache2filter',
28
            'apache2handler',
29
            'caudium',
30
            'cgi-fcgi',
31
            'continuity',
32
            'embed',
33
            'fpm-fcgi',
34
            'isapi',
35
            'litespeed',
36
            'milter',
37
            'nsapi',
38
            'phttpd',
39
            'pi3web',
40
            'roxen',
41
            'thttpd',
42
            'tux',
43
            'webjames'
44
        ]
45
    ];
46
47
    /**
48
     * @param  Benchmark            $benchmark
49
     * @param  bool|OutputInterface $interface
50
     * @throws \Exception
51
     * @return string
52
     */
53 3
    public static function output(Benchmark $benchmark, $interface = false)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
54
    {
55 3
        if (! $class = self::outputClass($interface ?: php_sapi_name())) {
56 1
            throw new \Exception('Interface ['. $interface . '] is unknown.');
57
        }
58
        
59 2
        return $class::output($benchmark);
60
    }
61
62
    /**
63
     * @param  $sapi
64
     * @return OutputInterface
65
     */
66 3
    private static function outputClass($sapi)
67
    {
68 3
        foreach (static::$interfaces as $key => $value) {
0 ignored issues
show
Bug introduced by
Since $interfaces is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $interfaces to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
69 3
            if (in_array($sapi, $value)) {
70 2
                return $key;
71
            }
72 3
        }
73 1
    }
74
75
}
76