Skip to content

refactor: fix various phpstan errors in Log component #9581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Config/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Log\Handlers\FileHandler;
use CodeIgniter\Log\Handlers\HandlerInterface;

class Logger extends BaseConfig
{
Expand Down Expand Up @@ -73,7 +74,7 @@ class Logger extends BaseConfig
* Handlers are executed in the order defined in this array, starting with
* the handler on top and continuing down.
*
* @var array<class-string, array<string, int|list<string>|string>>
* @var array<class-string<HandlerInterface>, array<string, int|list<string>|string>>
*/
public array $handlers = [
/*
Expand Down
18 changes: 12 additions & 6 deletions system/Debug/Toolbar/Collectors/Logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ class Logs extends BaseCollector
/**
* Our collected data.
*
* @var array
* @var list<array{level: string, msg: string}>
*/
protected $data;

/**
* Returns the data of this collector to be formatted in the toolbar
* Returns the data of this collector to be formatted in the toolbar.
*
* @return array{logs: list<array{level: string, msg: string}>}
*/
public function display(): array
{
Expand All @@ -66,7 +68,7 @@ public function isEmpty(): bool
{
$this->collectLogs();

return empty($this->data);
return $this->data !== [];
}

/**
Expand All @@ -82,14 +84,18 @@ public function icon(): string
/**
* Ensures the data has been collected.
*
* @return array
* @return list<array{level: string, msg: string}>
*/
protected function collectLogs()
{
if (! empty($this->data)) {
if ($this->data !== []) {
return $this->data;
}

return $this->data = service('logger', true)->logCache ?? [];
$cache = service('logger')->logCache;

$this->data = $cache ?? [];

return $this->data;
}
}
4 changes: 2 additions & 2 deletions system/Log/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class BaseHandler implements HandlerInterface
/**
* Handles
*
* @var array
* @var list<string>
*/
protected $handles;

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

/**
* Constructor
* @param array{handles?: list<string>} $config
*/
public function __construct(array $config)
{
Expand Down
29 changes: 15 additions & 14 deletions system/Log/Handlers/ChromeLoggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use CodeIgniter\HTTP\ResponseInterface;

/**
* Class ChromeLoggerHandler
*
* Allows for logging items to the Chrome console for debugging.
* Requires the ChromeLogger extension installed in your browser.
*
Expand All @@ -41,7 +39,16 @@ class ChromeLoggerHandler extends BaseHandler
/**
* The final data that is sent to the browser.
*
* @var array
* @var array{
* version: float,
* columns: list<string>,
* rows: list<array{
* 0: list<string>,
* 1: string,
* 2: string,
* }>,
* request_uri?: string,
* }
*/
protected $json = [
'version' => self::VERSION,
Expand All @@ -63,7 +70,7 @@ class ChromeLoggerHandler extends BaseHandler
/**
* Maps the log levels to the ChromeLogger types.
*
* @var array
* @var array<string, string>
*/
protected $levels = [
'emergency' => 'error',
Expand All @@ -77,7 +84,7 @@ class ChromeLoggerHandler extends BaseHandler
];

/**
* Constructor
* @param array{handles?: list<string>} $config
*/
public function __construct(array $config = [])
{
Expand All @@ -97,10 +104,8 @@ public function __construct(array $config = [])
*/
public function handle($level, $message): bool
{
// Format our message
$message = $this->format($message);

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

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

$this->json['rows'][] = [
[$message],
$backtraceMessage,
$type,
];
$this->json['rows'][] = [[$message], $backtraceMessage, $type];

$this->sendLogs();

Expand All @@ -130,9 +131,9 @@ public function handle($level, $message): bool
/**
* Converts the object to display nicely in the Chrome Logger UI.
*
* @param array|int|object|string $object
* @param object|string $object
*
* @return array
* @return array<string, mixed>|string
*/
protected function format($object)
{
Expand Down
6 changes: 4 additions & 2 deletions system/Log/Handlers/ErrorlogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class ErrorlogHandler extends BaseHandler
* Says where the error should go. Currently supported are
* 0 (`TYPE_OS`) and 4 (`TYPE_SAPI`).
*
* @var int
* @var 0|4
*/
protected $messageType = 0;

/**
* Constructor.
*
* @param list<mixed> $config
* @param array{handles?: list<string>, messageType?: int} $config
*/
public function __construct(array $config = [])
{
Expand Down Expand Up @@ -79,6 +79,8 @@ public function handle($level, $message): bool
/**
* Extracted call to `error_log()` in order to be tested.
*
* @param 0|4 $messageType
*
* @codeCoverageIgnore
*/
protected function errorLog(string $message, int $messageType): bool
Expand Down
20 changes: 11 additions & 9 deletions system/Log/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ class FileHandler extends BaseHandler
protected $filePermissions;

/**
* Constructor
* @param array{handles?: list<string>, path?: string, fileExtension?: string, filePermissions?: int} $config
*/
public function __construct(array $config = [])
{
parent::__construct($config);

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

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

$this->filePermissions = $config['filePermissions'] ?? 0644;
$this->fileExtension = $config['fileExtension'] === ''
? $defaults['fileExtension']
: ltrim($config['fileExtension'], '.');

$this->filePermissions = $config['filePermissions'];
}

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

for ($written = 0, $length = strlen($msg); $written < $length; $written += $result) {
if (($result = fwrite($fp, substr($msg, $written))) === false) {
// if we get this far, we'll never see this during travis-ci
// @codeCoverageIgnoreStart
break;
// @codeCoverageIgnoreEnd
// if we get this far, we'll never see this during unit testing
break; // @codeCoverageIgnore
}
}

Expand Down
Loading
Loading