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

Benchmark::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace BestServedCold\Benchmark;
4
5
use BestServedCold\Benchmark\Factory\Measure;
6
use BestServedCold\Benchmark\Factory\Peak;
7
8
/**
9
 * Class Benchmark
10
 * 
11
 * @package BestServedCold\Benchmark
12
 */
13
class Benchmark
14
{
15
    /**
16
     * @var array
17
     */
18
    protected static $markers = [];
19
20
    /**
21
     * @var string
22
     */
23
    protected static $lastName;
24
25
    /**
26
     * Benchmark constructor.
27
     *
28
     * @param $markers
29
     */
30 4
    public function __construct($markers = [])
31
    {
32 4
        self::$markers = $markers;
33 4
    }
34
35
    /**
36
     * @param  bool|string $name
37
     * @return string
38
     */
39 6
    public static function start($name = false)
40
    {
41 6
        $name = self::getName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by self::getName($name) on line 41 can also be of type string; however, BestServedCold\Benchmark\Benchmark::getName() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42 6
        self::$markers[$name] = Measure::now();
43 6
        return $name;
44
    }
45
46
    /**
47
     * @param  bool   $name
48
     * @return string
49
     */
50 6
    public static function stop($name = null)
51
    {
52 6
        $name = $name ?: self::getLastName();
53 6
        self::$markers[$name] = array_merge(Measure::diff(self::$markers[$name]), Peak::now());
54 6
        return $name;
55
    }
56
57
    /**
58
     * @param  bool        $name
59
     * @return array|mixed
60
     */
61 3
    public static function getMarkers($name = false)
62
    {
63 3
        return $name ? [ $name => self::$markers[$name]] : self::$markers;
64
    }
65
66
    /**
67
     * @param  string|bool $name
68
     * @return static
69
     */
70 4
    public static function get($name = false)
71
    {
72 4
        return $name ? new self([$name => self::$markers[$name]]) : new static(self::$markers);
73
    }
74
75
    /**
76
     * @return string
77
     */
78 2
    private static function getLastName()
79
    {
80 2
        $name           = self::$lastName;
81 2
        self::$lastName = null;
82 2
        return $name;
83
    }
84
85
    /**
86
     * @param  bool   $name
87
     * @return string
88
     */
89 6
    private static function getName($name = false)
90
    {
91 6
        self::$lastName = $name ?: uniqid();
0 ignored issues
show
Documentation Bug introduced by
It seems like $name ?: uniqid() can also be of type boolean. However, the property $lastName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
92 6
        return self::$lastName;
93
    }
94
95
    /**
96
     * @return void
97
     */
98 6
    public static function reset()
99
    {
100 6
        self::$lastName = null;
101 6
        self::$markers  = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $markers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102 6
    }
103
}
104