Skip to content

Commit 05c63d0

Browse files
committed
- chore: force native functions
- chore: use PHP 8 types
1 parent 5fe139a commit 05c63d0

1 file changed

Lines changed: 21 additions & 27 deletions

File tree

Log.php

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Koded\Logging;
1414

1515
use Koded\Logging\Processors\{Cli, Processor};
16+
use DateTimeZone;
1617
use Psr\Log\LoggerTrait;
1718
use Throwable;
1819

@@ -65,27 +66,27 @@ class Log implements Logger
6566
/**
6667
* @var bool Flag to control the messages processing
6768
*/
68-
private $deferred = false;
69+
private bool $deferred = false;
6970

7071
/**
7172
* @var string The date format for the message.
7273
*/
73-
private $dateFormat;
74+
private string $dateFormat;
7475

7576
/**
76-
* @var \DateTimeZone Valid timezone for the message.
77+
* @var DateTimeZone Valid timezone for the message.
7778
*/
78-
private $timezone;
79+
private DateTimeZone|bool $timezone;
7980

8081
/**
8182
* @var Processor[] Hash with all registered log processors.
8283
*/
83-
private $processors = [];
84+
private array $processors = [];
8485

8586
/**
8687
* @var array List with all accumulated messages.
8788
*/
88-
private $messages = [];
89+
private array $messages = [];
8990

9091
/**
9192
* Creates all requested log processors.
@@ -96,46 +97,40 @@ public function __construct(array $settings)
9697
{
9798
$this->deferred = (bool)($settings['deferred'] ?? false);
9899
$this->dateFormat = (string)($settings['dateformat'] ?? 'd/m/Y H:i:s.u');
99-
100-
if (false === $this->timezone = @timezone_open((string)($settings['timezone'] ?? 'UTC'))) {
101-
$this->timezone = timezone_open('UTC');
100+
if (false === $this->timezone = @\timezone_open((string)($settings['timezone'] ?? 'UTC'))) {
101+
$this->timezone = \timezone_open('UTC');
102102
}
103-
104103
foreach ((array)($settings['loggers'] ?? []) as $processor) {
105104
$this->attach(new $processor['class']($processor));
106105
}
107-
108106
if ($this->deferred) {
109-
register_shutdown_function([$this, 'process']);
107+
\register_shutdown_function([$this, 'process']);
110108
}
111109
}
112110

113111
public function attach(Processor $processor): Logger
114112
{
115113
if (0 !== $processor->levels()) {
116-
$this->processors[spl_object_hash($processor)] = $processor;
114+
$this->processors[\spl_object_hash($processor)] = $processor;
117115
}
118-
119116
return $this;
120117
}
121118

122119
public function log($level, $message, array $context = [])
123120
{
124121
try {
125-
$levelname = strtoupper($level);
126-
$level = constant('self::' . $levelname);
127-
} catch (Throwable $e) {
128-
$levelname = 'LOG';
122+
$levelName = \strtoupper($level);
123+
$level = \constant('static::' . $levelName);
124+
} catch (Throwable) {
125+
$levelName = 'LOG';
129126
$level = -1;
130127
}
131-
132128
$this->messages[] = [
133129
'level' => $level,
134-
'levelname' => $levelname,
130+
'levelname' => $levelName,
135131
'message' => $this->formatMessage($message, $context),
136-
'timestamp' => date_create_immutable('now', $this->timezone ?: null)->format($this->dateFormat),
132+
'timestamp' => \date_create_immutable('now', $this->timezone ?: null)->format($this->dateFormat),
137133
];
138-
139134
$this->deferred || $this->process();
140135
}
141136

@@ -159,25 +154,24 @@ public function exception(Throwable $e, Processor $processor = null): void
159154

160155
public function detach(Processor $processor): Logger
161156
{
162-
unset($this->processors[spl_object_hash($processor)]);
157+
unset($this->processors[\spl_object_hash($processor)]);
163158
return $this;
164159
}
165160

166161
/**
167162
* Parses the message as in the interface specification.
168163
*
169-
* @param string|object $message A string or object that implements __toString
164+
* @param object|string $message A string or object that implements __toString
170165
* @param array $params [optional] Arbitrary data with key-value pairs replacements
171166
*
172167
* @return string
173168
*/
174-
private function formatMessage($message, array $params = []): string
169+
private function formatMessage(object|string $message, array $params = []): string
175170
{
176171
$replacements = [];
177172
foreach ($params as $k => $v) {
178173
$replacements['{' . $k . '}'] = $v;
179174
}
180-
181-
return strtr((string)$message, $replacements);
175+
return \strtr((string)$message, $replacements);
182176
}
183177
}

0 commit comments

Comments
 (0)