Skip to content

Commit 63ab555

Browse files
authored
refactor: fix various phpstan errors in Log component (#9581)
* refactor Logs collector * refactor Logger config * refactor Logger * refactor BaseHandler * refactor FileHandler * refactor Mock FileHandler * refactor ErrorlogHandler * refactor ChromeLoggerHandler * regenerate baseline * Fix per review
1 parent 315f55b commit 63ab555

18 files changed

+157
-343
lines changed

app/Config/Logger.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CodeIgniter\Config\BaseConfig;
66
use CodeIgniter\Log\Handlers\FileHandler;
7+
use CodeIgniter\Log\Handlers\HandlerInterface;
78

89
class Logger extends BaseConfig
910
{
@@ -73,7 +74,7 @@ class Logger extends BaseConfig
7374
* Handlers are executed in the order defined in this array, starting with
7475
* the handler on top and continuing down.
7576
*
76-
* @var array<class-string, array<string, int|list<string>|string>>
77+
* @var array<class-string<HandlerInterface>, array<string, int|list<string>|string>>
7778
*/
7879
public array $handlers = [
7980
/*

system/Debug/Toolbar/Collectors/Logs.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ class Logs extends BaseCollector
4545
/**
4646
* Our collected data.
4747
*
48-
* @var array
48+
* @var list<array{level: string, msg: string}>
4949
*/
5050
protected $data;
5151

5252
/**
53-
* Returns the data of this collector to be formatted in the toolbar
53+
* Returns the data of this collector to be formatted in the toolbar.
54+
*
55+
* @return array{logs: list<array{level: string, msg: string}>}
5456
*/
5557
public function display(): array
5658
{
@@ -66,7 +68,7 @@ public function isEmpty(): bool
6668
{
6769
$this->collectLogs();
6870

69-
return empty($this->data);
71+
return $this->data !== [];
7072
}
7173

7274
/**
@@ -82,14 +84,18 @@ public function icon(): string
8284
/**
8385
* Ensures the data has been collected.
8486
*
85-
* @return array
87+
* @return list<array{level: string, msg: string}>
8688
*/
8789
protected function collectLogs()
8890
{
89-
if (! empty($this->data)) {
91+
if ($this->data !== []) {
9092
return $this->data;
9193
}
9294

93-
return $this->data = service('logger', true)->logCache ?? [];
95+
$cache = service('logger')->logCache;
96+
97+
$this->data = $cache ?? [];
98+
99+
return $this->data;
94100
}
95101
}

system/Log/Handlers/BaseHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class BaseHandler implements HandlerInterface
2121
/**
2222
* Handles
2323
*
24-
* @var array
24+
* @var list<string>
2525
*/
2626
protected $handles;
2727

@@ -33,7 +33,7 @@ abstract class BaseHandler implements HandlerInterface
3333
protected $dateFormat = 'Y-m-d H:i:s';
3434

3535
/**
36-
* Constructor
36+
* @param array{handles?: list<string>} $config
3737
*/
3838
public function __construct(array $config)
3939
{

system/Log/Handlers/ChromeLoggerHandler.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use CodeIgniter\HTTP\ResponseInterface;
1717

1818
/**
19-
* Class ChromeLoggerHandler
20-
*
2119
* Allows for logging items to the Chrome console for debugging.
2220
* Requires the ChromeLogger extension installed in your browser.
2321
*
@@ -41,7 +39,16 @@ class ChromeLoggerHandler extends BaseHandler
4139
/**
4240
* The final data that is sent to the browser.
4341
*
44-
* @var array
42+
* @var array{
43+
* version: float,
44+
* columns: list<string>,
45+
* rows: list<array{
46+
* 0: list<string>,
47+
* 1: string,
48+
* 2: string,
49+
* }>,
50+
* request_uri?: string,
51+
* }
4552
*/
4653
protected $json = [
4754
'version' => self::VERSION,
@@ -63,7 +70,7 @@ class ChromeLoggerHandler extends BaseHandler
6370
/**
6471
* Maps the log levels to the ChromeLogger types.
6572
*
66-
* @var array
73+
* @var array<string, string>
6774
*/
6875
protected $levels = [
6976
'emergency' => 'error',
@@ -77,7 +84,7 @@ class ChromeLoggerHandler extends BaseHandler
7784
];
7885

7986
/**
80-
* Constructor
87+
* @param array{handles?: list<string>} $config
8188
*/
8289
public function __construct(array $config = [])
8390
{
@@ -97,10 +104,8 @@ public function __construct(array $config = [])
97104
*/
98105
public function handle($level, $message): bool
99106
{
100-
// Format our message
101107
$message = $this->format($message);
102108

103-
// Generate Backtrace info
104109
$backtrace = debug_backtrace(0, $this->backtraceLevel);
105110
$backtrace = end($backtrace);
106111

@@ -116,11 +121,7 @@ public function handle($level, $message): bool
116121
$type = $this->levels[$level];
117122
}
118123

119-
$this->json['rows'][] = [
120-
[$message],
121-
$backtraceMessage,
122-
$type,
123-
];
124+
$this->json['rows'][] = [[$message], $backtraceMessage, $type];
124125

125126
$this->sendLogs();
126127

@@ -130,9 +131,9 @@ public function handle($level, $message): bool
130131
/**
131132
* Converts the object to display nicely in the Chrome Logger UI.
132133
*
133-
* @param array|int|object|string $object
134+
* @param object|string $object
134135
*
135-
* @return array
136+
* @return array<string, mixed>|string
136137
*/
137138
protected function format($object)
138139
{

system/Log/Handlers/ErrorlogHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ class ErrorlogHandler extends BaseHandler
3838
* Says where the error should go. Currently supported are
3939
* 0 (`TYPE_OS`) and 4 (`TYPE_SAPI`).
4040
*
41-
* @var int
41+
* @var 0|4
4242
*/
4343
protected $messageType = 0;
4444

4545
/**
4646
* Constructor.
4747
*
48-
* @param list<mixed> $config
48+
* @param array{handles?: list<string>, messageType?: int} $config
4949
*/
5050
public function __construct(array $config = [])
5151
{
@@ -79,6 +79,8 @@ public function handle($level, $message): bool
7979
/**
8080
* Extracted call to `error_log()` in order to be tested.
8181
*
82+
* @param 0|4 $messageType
83+
*
8284
* @codeCoverageIgnore
8385
*/
8486
protected function errorLog(string $message, int $messageType): bool

system/Log/Handlers/FileHandler.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ class FileHandler extends BaseHandler
4545
protected $filePermissions;
4646

4747
/**
48-
* Constructor
48+
* @param array{handles?: list<string>, path?: string, fileExtension?: string, filePermissions?: int} $config
4949
*/
5050
public function __construct(array $config = [])
5151
{
5252
parent::__construct($config);
5353

54-
$this->path = empty($config['path']) ? WRITEPATH . 'logs/' : $config['path'];
54+
$defaults = ['path' => WRITEPATH . 'logs/', 'fileExtension' => 'log', 'filePermissions' => 0644];
55+
$config = [...$defaults, ...$config];
5556

56-
$this->fileExtension = empty($config['fileExtension']) ? 'log' : $config['fileExtension'];
57-
$this->fileExtension = ltrim($this->fileExtension, '.');
57+
$this->path = $config['path'] === '' ? $defaults['path'] : $config['path'];
5858

59-
$this->filePermissions = $config['filePermissions'] ?? 0644;
59+
$this->fileExtension = $config['fileExtension'] === ''
60+
? $defaults['fileExtension']
61+
: ltrim($config['fileExtension'], '.');
62+
63+
$this->filePermissions = $config['filePermissions'];
6064
}
6165

6266
/**
@@ -108,10 +112,8 @@ public function handle($level, $message): bool
108112

109113
for ($written = 0, $length = strlen($msg); $written < $length; $written += $result) {
110114
if (($result = fwrite($fp, substr($msg, $written))) === false) {
111-
// if we get this far, we'll never see this during travis-ci
112-
// @codeCoverageIgnoreStart
113-
break;
114-
// @codeCoverageIgnoreEnd
115+
// if we get this far, we'll never see this during unit testing
116+
break; // @codeCoverageIgnore
115117
}
116118
}
117119

0 commit comments

Comments
 (0)