Completed
Push — master ( 509b14...929e7e )
by Barney
04:52 queued 03:02
created

Stopwatch::startStopWatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @codingStandardsIgnoreStart
4
 *
5
 * @author       Barney Hanlon <[email protected]>
6
 * @copyright    Barney Hanlon 2017
7
 * @license      https://opensource.org/licenses/MIT
8
 *
9
 * @codingStandardsIgnoreEnd
10
 */
11
12
namespace Shrikeh\GuzzleMiddleware\TimerLogger\Timer;
13
14
use DateTimeImmutable;
15
use Litipk\BigNumbers\Decimal;
16
17
/**
18
 * Class Stopwatch.
19
 */
20
class Stopwatch implements TimerInterface
21
{
22
    /**
23
     * @var float
24
     */
25
    private $start;
26
27
    /**
28
     * @var float
29
     */
30
    private $end;
31
32
    /**
33
     * @return self
34
     */
35
    public static function startStopWatch()
36
    {
37
        $t = \microtime(true);
38
39
        return new self($t);
0 ignored issues
show
Bug introduced by
It seems like $t can also be of type string; however, parameter $start of Shrikeh\GuzzleMiddleware...topwatch::__construct() does only seem to accept null|double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        return new self(/** @scrutinizer ignore-type */ $t);
Loading history...
40
    }
41
42
    /**
43
     * Stopwatch constructor.
44
     *
45
     * @param float|null $start The start time
46
     */
47
    public function __construct($start = null)
48
    {
49
        $this->start = $start;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 View Code Duplication
    public function start()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $t = \microtime(true);
58
        if (!$this->start) {
59
            $this->start = $t;
0 ignored issues
show
Documentation Bug introduced by
It seems like $t can also be of type string. However, the property $start is declared as type double. 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...
60
        }
61
62
        return $this->dateTime($this->start);
0 ignored issues
show
Bug introduced by
$this->start of type string|double is incompatible with the type Litipk\BigNumbers\Decimal expected by parameter $time of Shrikeh\GuzzleMiddleware...r\Stopwatch::dateTime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        return $this->dateTime(/** @scrutinizer ignore-type */ $this->start);
Loading history...
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 View Code Duplication
    public function stop()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $t = \microtime(true);
71
        if (!$this->end) {
72
            $this->end = $t;
0 ignored issues
show
Documentation Bug introduced by
It seems like $t can also be of type string. However, the property $end is declared as type double. 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...
73
        }
74
75
        return $this->dateTime($this->end);
0 ignored issues
show
Bug introduced by
$this->end of type string|double is incompatible with the type Litipk\BigNumbers\Decimal expected by parameter $time of Shrikeh\GuzzleMiddleware...r\Stopwatch::dateTime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        return $this->dateTime(/** @scrutinizer ignore-type */ $this->end);
Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function duration($precision = 0)
82
    {
83
        $this->stop();
84
85
        $start = $this->decimal($this->start);
86
        $end = $this->decimal($this->end);
87
88
        return Decimal::fromDecimal($end->sub($start)
89
            ->mul(Decimal::fromInteger(1000)), $precision)->asFloat();
90
    }
91
92
    /**
93
     * @param Decimal $time The time to format to a DateTimeImmutable
94
     *
95
     * @return \DateTimeImmutable
96
     */
97
    private function dateTime($time)
98
    {
99
        $time = $this->decimal($time);
0 ignored issues
show
Bug introduced by
$time of type Litipk\BigNumbers\Decimal is incompatible with the type double expected by parameter $t of Shrikeh\GuzzleMiddleware...er\Stopwatch::decimal(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $time = $this->decimal(/** @scrutinizer ignore-type */ $time);
Loading history...
100
        $micro = sprintf('%06d', $this->mantissa($time)->asInteger());
101
102
        return new DateTimeImmutable(
103
            \date('Y-m-d H:i:s.'.$micro, $time->asFloat())
0 ignored issues
show
Bug introduced by
$time->asFloat() of type double is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            \date('Y-m-d H:i:s.'.$micro, /** @scrutinizer ignore-type */ $time->asFloat())
Loading history...
104
        );
105
    }
106
107
    /**
108
     * @param Decimal $time The time to get the mantissa from
109
     *
110
     * @return Decimal
111
     */
112
    private function mantissa(Decimal $time)
113
    {
114
        $mantissa = ($time->sub($time->floor()));
115
116
        return $mantissa->mul(Decimal::fromInteger(1000000));
117
    }
118
119
    /**
120
     * @param float $t The float to turn into a Decimal
121
     *
122
     * @return Decimal
123
     */
124
    private function decimal($t)
125
    {
126
        return Decimal::fromFloat($t);
127
    }
128
}
129