diff --git a/.gitignore b/.gitignore index e2a3893..91e84a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ composer.lock *.cache +/.phpstan-cache diff --git a/README.md b/README.md index bd4617e..d8c563f 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,10 @@ function withContext(array $context) : self Returns a new instance with the given context replacing the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + --- #### Method: LoggerWithContextInterface->withAddedContext @@ -127,6 +131,10 @@ function withAddedContext(array $context) : self Returns a new instance with the given context added to the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + ### Class: Corpus\Loggers\Interfaces\MultiLoggerInterface #### Method: MultiLoggerInterface->withAdditionalLoggers @@ -160,6 +168,10 @@ function withContext(array $context) : self Returns a new instance with the given context replacing the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + --- #### Method: WithContextInterface->withAddedContext @@ -171,6 +183,10 @@ function withAddedContext(array $context) : self Returns a new instance with the given context added to the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + ### Class: Corpus\Loggers\LoggerVerbosityFilter LoggerVerbosityFilter mutes log messages based on a given integer verbosity level. @@ -240,6 +256,7 @@ The given context will be added to all log messages. - ***\Psr\Log\LoggerInterface*** `$logger` - The logger to delegate to. - ***array*** `$context` - The context to add to all log messages. +- ***array*** `$context` --- @@ -252,6 +269,10 @@ function withContext(array $context) : self Returns a new instance with the given context replacing the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + --- #### Method: LoggerWithContext->withAddedContext @@ -263,6 +284,10 @@ function withAddedContext(array $context) : self Returns a new instance with the given context added to the existing context. +##### Parameters: + +- ***array*** `$context` - The context to add to all log messages. + ### Class: Corpus\Loggers\LogLevelFilter LogLevelFilter is a PSR Logger that filters logs based on the log level. diff --git a/composer.json b/composer.json index fb8c9a1..ebbcd2a 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "corpus/coding-standard": "^0.8.0", "friendsofphp/php-cs-fixer": "^3.65", "rawr/phpunit-data-provider": "^3.3", - "corpus/recursive-require": "^1.2" + "corpus/recursive-require": "^1.2", + "phpstan/phpstan": "^2.1" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..15bd816 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +parameters: + level: 8 + tmpDir: .phpstan-cache + paths: + - src + phpVersion: 70400 + treatPhpDocTypesAsCertain: false diff --git a/src/Interfaces/WithContextInterface.php b/src/Interfaces/WithContextInterface.php index c28844f..7d303a2 100644 --- a/src/Interfaces/WithContextInterface.php +++ b/src/Interfaces/WithContextInterface.php @@ -7,12 +7,16 @@ interface WithContextInterface { /** * Returns a new instance with the given context * replacing the existing context. + * + * @param mixed[] $context The context to add to all log messages. */ public function withContext( array $context ) : self; /** * Returns a new instance with the given context * added to the existing context. + * + * @param mixed[] $context The context to add to all log messages. */ public function withAddedContext( array $context ) : self; diff --git a/src/LoggerWithContext.php b/src/LoggerWithContext.php index 45c4bf0..ac9d5d7 100644 --- a/src/LoggerWithContext.php +++ b/src/LoggerWithContext.php @@ -17,6 +17,7 @@ class LoggerWithContext implements LoggerWithContextInterface { use LoggerTrait; + /** @var mixed[] */ private array $context; private LoggerInterface $logger; @@ -27,6 +28,7 @@ class LoggerWithContext implements LoggerWithContextInterface { * * @param LoggerInterface $logger The logger to delegate to. * @param array $context The context to add to all log messages. + * @param mixed[] $context */ public function __construct( LoggerInterface $logger, array $context = [] ) { $this->logger = $logger; diff --git a/src/MemoryLogger.php b/src/MemoryLogger.php index 3f5b127..425b894 100644 --- a/src/MemoryLogger.php +++ b/src/MemoryLogger.php @@ -18,6 +18,7 @@ class MemoryLogger implements LoggerInterface { public const KEY_MESSAGE = 'message'; public const KEY_CONTEXT = 'context'; + /** @var array */ private array $logs = []; /** @@ -37,6 +38,7 @@ public function log( $level, $message, array $context = [] ) : void { * - MemoryLogger::KEY_MESSAGE : The log message * - MemoryLogger::KEY_CONTEXT : The log context * + * @phpstan-return array * @return array[] */ public function getLogs() : array { @@ -58,6 +60,7 @@ public function clearLogs() : void { * @param mixed $level The log level * @param string $message The log message * @param mixed[] $context The log context + * @return array{level:mixed,message:string,context:mixed[]} * @mddoc-ignore */ public static function makeLogRecord( $level, $message, array $context = [] ) : array { diff --git a/src/StreamResourceLogger.php b/src/StreamResourceLogger.php index b9ef332..40b7280 100644 --- a/src/StreamResourceLogger.php +++ b/src/StreamResourceLogger.php @@ -49,7 +49,7 @@ public function log( $level, $message, array $context = [] ) : void { $this->stream, "%s %9s: %s\n", date('c'), - $level, + (string)$level, is_string($message) ? $message : var_export($message, true) );