Important
The next version 0.25.0
will have a BC break. Please refer Stubs
section.
struggle-for-php/sfp-phpstan-psr-log
is extra strict and opinionated psr/log (psr-3) rules for PHPStan.
Write these parameters to your project's phpstan.neon
.
parameters:
sfpPsrLog:
enableMessageStaticStringRule: false # default:true
enableContextRequireExceptionKeyRule: true
reportContextExceptionLogLevel: 'info'
contextKeyOriginalPattern: '#\A[A-Za-z0-9-_]+\z#'
Important
include psr/log stub be planned to dropped in next release.
To try out the changes in the next version,
DELETE vendor/struggle-for-php/sfp-phpstan-psr-log/extension.neon
line from your phpstan.neon
includes:
- vendor/struggle-for-php/sfp-phpstan-psr-log/extension.neon
and, set parameters enableLogLevelMethodRule
and enableContextTypeRule
parameters:
sfpPsrLog:
enableLogLevelMethodRule: true # default:false
enableContextTypeRule: true # default:false
Currently, this extension depends on our psr/log stub to serve strictness.
- eg.
@param LogLevel::* $level
atlog()
method@param array{exception?: \Throwable} $context
See psr/log stub repository page to get more detail.
This package provides the following rules.
Placeholder names SHOULD be composed only of the characters A-Z, a-z, 0-9, underscore _, and period .
π error identifier |
---|
sfpPsrLog.placeholderCharactersInvalidChar |
- reports when placeholder in
$message
characters are not,A-Z
,a-z
,0-9
, underscore_
, and period.
// bad
$logger->info('message are {foo-hyphen}');
π error identifier |
---|
sfpPsrLog.placeholderCharactersDoubleBraces |
- reports when double braces pair
{{
}}
are used.
// bad
$logger->info('message are {{foo}}');
Placeholder names MUST correspond to keys in the context array.
π error identifier |
---|
sfpPsrLog.placeholderCorrespondToKeysMissedContext |
- reports when placeholder exists in message, but
$context
parameter is missed.
// bad
$logger->info('message has {nonContext} .');
π error identifier |
---|
sfpPsrLog.placeholderCorrespondToKeysMissedKey |
- reports when placeholder exists in message, but key in
$context
does not exist against them.
// bad
$logger->info('user {user_id} gets an error {error} .', ['user_id' => $user_id]);
Note
PSR-3 has no provisions for array keys, but this is useful in many cases.
π error identifier |
---|
sfpPsrLog.contextKeyNonEmptyString |
- reports when context key is not non-empty-string.
// bad
[123 => 'foo']`, `['' => 'bar']`, `['baz']
π error identifier |
---|
sfpPsrLog.contextKeyOriginalPattern |
- reports when context key is not matched you defined pattern.
- if
contextKeyOriginalPattern
parameter is not set, this check would be ignored.
- if
- You can set specific key pattern by regex.(
preg_match()
)
parameters:
sfpPsrLog:
contextKeyOriginalPattern: '#\A[A-Za-z0-9-]+\z#'
Note
This is not a rule for along with PSR-3 specification, but provides best practices.
π error identifier |
---|
sfpPsrLog.contextRequireExceptionKey |
- It forces
exception
key into context parameter when current scope has\Throwable
object.
<?php
/** @var \Psr\Log\LoggerInterface $logger */
try {
//
} catch (LogicException $exception) {
$logger->warning("foo");
}
$ ../vendor/bin/phpstan analyse
Note: Using configuration file /tmp/your-project/phpstan.neon.
2/2 [ββββββββββββββββββββββββββββ] 100%
------ -------------------------------------------------------------
Line Demo.php
------ -------------------------------------------------------------
6 Parameter $context of logger method Psr\Log\LoggerInterface::warning() requires \'exception\' key. Current scope has Throwable variable - $exception
------ -------------------------------------------------------------
[ERROR] Found 1 error
- You can set the minimum required level to report. (default level is
debug
)
parameters:
sfpPsrLog:
reportContextExceptionLogLevel: 'warning'
Then, debug
| info
| notice
LogLevel is ignored for report.
} catch (\Exception $e) {
$logger->info('more info'); // allow
$logger->warning($e->getMessage(), ['exception' => $e]);
}
- If you want to enable this rule, please add
enableContextRequireExceptionKeyRule
as true.
parameters:
sfpPsrLog:
enableContextRequireExceptionKeyRule: true
π error identifier |
---|
sfpPsrLog.messageNotStaticString |
- reports when $message is not static string value.
// bad
$logger->info(sprintf('Message contains %s variable', $var));
- If you want to disable this rule, please add
enableMessageStaticStringRule
as false.
parameters:
sfpPsrLog:
enableMessageStaticStringRule: false
To use this extension, require it in Composer:
composer require --dev struggle-for-php/sfp-phpstan-psr-log
include extension.neon & rules.neon in your project's PHPStan config:
includes:
- vendor/struggle-for-php/sfp-phpstan-psr-log/extension.neon
- vendor/struggle-for-php/sfp-phpstan-psr-log/rules.neon