GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LogFormatter::format()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace AlibabaCloud\Client\Log;
4
5
use DateTime;
6
use Exception;
7
use DateTimeZone;
8
use GuzzleHttp\MessageFormatter;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * @deprecated Use GuzzleHttp\MessageFormatter.
14
 * Class LogFormatter
15
 *
16
 * @package AlibabaCloud\Client\Log
17
 */
18
class LogFormatter
19
{
20
    /**
21
     * @var float
22
     */
23
    private static $logStartTime = 0;
24
25
    /**
26
     * @var DateTime
27
     */
28
    private static $ts;
29
30
    /** @var string Template used to format log messages */
31
    public $template;
32
33
    /**
34
     * @param string $template Log message template
35
     *
36
     * @throws Exception
37 2
     */
38
    public function __construct($template)
39 2
    {
40 2
        // parent::__construct($template);
41 2
        self::$logStartTime = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $logStartTime 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...
42 2
        $this->template     = $template;
43 2
        $timezone           = new DateTimeZone(date_default_timezone_get() ?: 'UTC');
44 2
        if (PHP_VERSION_ID < 70100) {
45 2
            self::$ts = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone);
46
        } else {
47
            self::$ts = new DateTime('now', $timezone);
48 2
        }
49
    }
50
51
    /**
52
     * Returns a formatted message string.
53
     *
54
     * @param RequestInterface  $request  Request that was sent
55
     * @param ResponseInterface $response Response that was received
56
     * @param Exception         $error    Exception that was received
57
     *
58
     * @return string
59 2
     */
60
    public function format(
61
        RequestInterface $request,
62
        ResponseInterface $response = null,
63
        Exception $error = null
64 2
    ) {
65 2
        $this->template = str_replace('{pid}', getmypid(), $this->template);
66 2
        $this->template = str_replace('{cost}', self::getCost(), $this->template);
67
        $this->template = str_replace('{start_time}', self::$ts->format('Y-m-d H:i:s.u'), $this->template);
68 2
69
        return (new MessageFormatter($this->template))->format($request, $response, $error);
70
    }
71
72
    /**
73
     * @return float|mixed
74 2
     */
75
    private static function getCost()
76 2
    {
77
        return microtime(true) - self::$logStartTime;
78
    }
79
}
80