1 | <?php |
||
2 | |||
3 | namespace AlibabaCloud\Client\Traits; |
||
4 | |||
5 | use DateTime; |
||
6 | use DateTimeZone; |
||
7 | use Exception; |
||
8 | use Psr\Log\LoggerInterface; |
||
9 | |||
10 | /** |
||
11 | * Trait LogTrait |
||
12 | * |
||
13 | * @package AlibabaCloud\Client\Traits |
||
14 | */ |
||
15 | trait LogTrait |
||
16 | { |
||
17 | /** |
||
18 | * @var LoggerInterface |
||
19 | */ |
||
20 | private static $logger; |
||
21 | |||
22 | /** |
||
23 | * @var float |
||
24 | */ |
||
25 | private static $logStartTime = 0; |
||
26 | |||
27 | /** |
||
28 | 75 | * @var string |
|
29 | */ |
||
30 | 75 | private static $logFormat; |
|
31 | |||
32 | /** |
||
33 | * @var DateTime |
||
34 | */ |
||
35 | private static $ts; |
||
36 | |||
37 | /** |
||
38 | 1 | * @return LoggerInterface |
|
39 | */ |
||
40 | 1 | public static function getLogger() |
|
41 | 1 | { |
|
42 | return self::$logger; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | 2 | * @param LoggerInterface $logger |
|
47 | * |
||
48 | * @throws Exception |
||
49 | 2 | */ |
|
50 | public static function setLogger(LoggerInterface $logger) |
||
51 | { |
||
52 | self::$logger = $logger; |
||
53 | self::$logStartTime = microtime(true); |
||
54 | $timezone = new DateTimeZone(date_default_timezone_get() ?: 'UTC'); |
||
55 | if (PHP_VERSION_ID < 70100) { |
||
56 | self::$ts = DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $timezone); |
||
0 ignored issues
–
show
|
|||
57 | } else { |
||
58 | self::$ts = new DateTime(null, $timezone); |
||
59 | } |
||
60 | 1 | } |
|
61 | |||
62 | 1 | /** |
|
63 | 1 | * @return string |
|
64 | */ |
||
65 | public static function getLogFormat() |
||
66 | { |
||
67 | $template = self::$logFormat |
||
68 | ?: '"{method} {uri} HTTP/{version}" {code} {cost} {hostname} {pid}'; |
||
69 | |||
70 | return str_replace( |
||
71 | ['{pid}', '{cost}', '{start_time}'], |
||
72 | [getmypid(), self::getCost(), self::$ts->format('Y-m-d H:i:s.u')], |
||
73 | $template |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Apache Common Log Format. |
||
79 | * |
||
80 | * @param string $formatter |
||
81 | * |
||
82 | * @link http://httpd.apache.org/docs/2.4/logs.html#common |
||
83 | * @see \GuzzleHttp\MessageFormatter |
||
84 | */ |
||
85 | public static function setLogFormat($formatter) |
||
86 | { |
||
87 | self::$logFormat = $formatter; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return float|mixed |
||
92 | */ |
||
93 | private static function getCost() |
||
94 | { |
||
95 | return microtime(true) - self::$logStartTime; |
||
96 | } |
||
97 | } |
||
98 |
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 theid
property of an instance of theAccount
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.