Skip to content

Commit

Permalink
add type hints, FB discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Beeson committed Jan 30, 2025
1 parent 718d399 commit a5201fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
23 changes: 14 additions & 9 deletions plugins/Monolog/Formatter/LineMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class LineMessageFormatter implements FormatterInterface
* @param string $logMessageFormat
* @param bool $allowInlineLineBreaks If disabled, a log message will be created for each line
*/
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, $excludePatterns = null, $customFunctionFile = null)
public function __construct($logMessageFormat, bool $allowInlineLineBreaks = true, ?array $excludePatterns = null, ?string $customFunctionFile = null)
{
$this->logMessageFormat = $logMessageFormat;
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
$this->excludePatterns = $excludePatterns;
if (gettype($customFunctionFile) === "string") {
if ($customFunctionFile !== null) {
$this->customFunction = include_once $customFunctionFile;
}
}
Expand All @@ -48,7 +48,7 @@ public function format(array $record)

$message = trim($record['message']);

if (gettype($this->excludePatterns) === "array") {
if ($this->excludePatterns !== null) {
foreach ($this->excludePatterns as $p) {
if (strpos($message, $p) !== false) {
return;
Expand All @@ -59,7 +59,7 @@ public function format(array $record)
}
}

if ($this->logMessageFormat == 'json') {
if ($this->logMessageFormat === 'json') {
return $this->jsonMessage($class, $message, $date, $record);
}

Expand All @@ -82,7 +82,7 @@ public function format(array $record)
return $total;
}

private function jsonMessage($class, $message, $date, $record)
private function jsonMessage(string $class, string $message, string $date, array $record) : ?string

Check failure on line 85 in plugins/Monolog/Formatter/LineMessageFormatter.php

View workflow job for this annotation

GitHub Actions / PHPCS

There must not be a space before the colon in a return type declaration
{
$trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : '';
$requestId = isset($record['extra']['request_id']) ? $record['extra']['request_id'] : '';
Expand All @@ -93,17 +93,22 @@ private function jsonMessage($class, $message, $date, $record)
"message" => $message,
"level" => $record['level_name'],
"trace" => $trace,
"requestId" => $requestId
"requestId" => $requestId,
];

if (gettype($this->customFunction) === "string") {
if (is_callable($this->customFunction)) {
$message = call_user_func($this->customFunction, $message);
if ($message === null){

Check failure on line 101 in plugins/Monolog/Formatter/LineMessageFormatter.php

View workflow job for this annotation

GitHub Actions / PHPCS

Expected 1 space after closing parenthesis; found 0
# allow for custom function to filter out messages by returning null
return null;
}

Check failure on line 105 in plugins/Monolog/Formatter/LineMessageFormatter.php

View workflow job for this annotation

GitHub Actions / PHPCS

Blank line found at end of control structure
}

return json_encode($message) . "\n";
}

private function formatMessage($class, $message, $date, $record)
private function formatMessage(string $class, string $message, string $date, array $record) : ?string

Check failure on line 111 in plugins/Monolog/Formatter/LineMessageFormatter.php

View workflow job for this annotation

GitHub Actions / PHPCS

There must not be a space before the colon in a return type declaration
{
$trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : '';
$message = str_replace(
Expand All @@ -116,7 +121,7 @@ private function formatMessage($class, $message, $date, $record)
return $message;
}

private static function formatTrace(array $trace, $numLevels = 10)
private static function formatTrace(array $trace, int $numLevels = 10): string
{
$strTrace = '';
for ($i = 0; $i < $numLevels; $i++) {
Expand Down
17 changes: 3 additions & 14 deletions plugins/Monolog/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,6 @@
->constructorParameter('customFunctionFile', Piwik\DI::get('log.custom_function_file'))
->constructorParameter('excludePatterns', Piwik\DI::get('log.exclude_patterns')),

'log.exclude_patterns' => Piwik\DI::factory(function (Container $c) {
if ($c->has('ini.log.exclude_patterns')) {
$xcl = [];
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) {
$xcl[] = trim($p);
}
return $xcl;
}
return null;
}),

'log.custom_function_file' => Piwik\DI::factory(function (Container $c) {
if ($c->has('ini.log.custom_function_file')) {
$path = $c->get('ini.log.custom_function_file');
Expand All @@ -268,11 +257,11 @@

'log.exclude_patterns' => Piwik\DI::factory(function (Container $c) {
if ($c->has('ini.log.exclude_patterns')) {
$xcl = [];
$excl = [];
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) {
$xcl[] = trim($p);
$excl[] = trim($p);
}
return $xcl;
return $excl;
}
return null;
}),
Expand Down

0 comments on commit a5201fa

Please sign in to comment.