Completed
Push — master ( 8c9cd6...ef07c2 )
by Adam
08:13
created

Benchmark::getLastName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Utility;
4
5
use BestServedCold\PhalueObjects\Metric;
6
use BestServedCold\PhalueObjects\Metric\DeclaredClass;
7
use BestServedCold\PhalueObjects\Metric\DeclaredInterface;
8
use BestServedCold\PhalueObjects\Metric\DeclaredTrait;
9
use BestServedCold\PhalueObjects\Metric\DefinedConstant;
10
use BestServedCold\PhalueObjects\Metric\DefinedFunction;
11
use BestServedCold\PhalueObjects\Metric\IncludedFile;
12
use BestServedCold\PhalueObjects\Metric\MemoryUsage;
13
use BestServedCold\PhalueObjects\Metric\MicroTime;
14
use BestServedCold\PhalueObjects\Metric\PeakMemoryUsage;
15
16
class Benchmark
0 ignored issues
show
Coding Style introduced by
Benchmark does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
{
18
    /**
19
     * @var array
20
     */
21
    protected static $markers = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected static $lastName;
27
28
    /**
29
     * @var array
30
     */
31
    protected static $metrics  = [
32
        MicroTime::class,
33
        MemoryUsage::class,
34
        DeclaredInterface::class,
35
        DeclaredTrait::class,
36
        DefinedFunction::class,
37
        DefinedConstant::class,
38
        IncludedFile::class,
39
        DeclaredClass::class,
40
    ];
41
42
    /**
43
     * @param  bool   $name
44
     * @return string
45
     */
46
    public static function human($name = false)
47
    {
48
        return implode(array_map(function ($key, $value) {
49
             return '[' . $key . ']: [' . (string) $value . "]\n";
50
        }, array_keys(self::get($name)), self::get($name)));
51
    }
52
53
    /**
54
     * @param  bool        $name
55
     * @return array|mixed
56
     */
57
    public static function get($name = false)
58
    {
59
        return $name ? self::$markers[$name] : self::$markers;
60
    }
61
62
    /**
63
     * @param bool $name
64
     */
65
    public static function start($name = false)
66
    {
67
        /** @var Metric $metric */
68
        foreach (self::$metrics as $metric) {
69
            $now = $metric::now();
70
            self::$markers[self::getName($name)][$now->getShortName()] = $now;
71
        }
72
    }
73
74
    /**
75
     * @param bool $name
76
     */
77
    public static function stop($name = false)
78
    {
79
        self::stopMarkers($name ?: self::getLastName());
80
    }
81
82
    /**
83
     * @param $name
84
     */
85
    private static function stopMarkers($name)
86
    {
87
        /** @var Metric $metric */
88
        foreach (self::$markers[$name] as $metric) {
89
            self::$markers[$name][$metric->getShortName()] = $metric::now()->diff($metric);
90
        }
91
92
        self::addPeak($name);
93
    }
94
95
    /**
96
     * @param $name
97
     */
98
    private static function addPeak($name)
99
    {
100
        $peak = PeakMemoryUsage::now();
101
        self::$markers[$name][$peak->getShortName()] = $peak;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    private static function getLastName()
108
    {
109
        $name           = self::$lastName;
110
        self::$lastName = null;
111
        return $name;
112
    }
113
114
    /**
115
     * @param  bool   $name
116
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     */
118
    private static function getName($name = false)
119
    {
120
        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...
121
        return self::$lastName;
122
    }
123
}
124