struggle-for-php / sfp-psalm-psr-log-plugin

为Psalm严格理解psr/log(PSR-3)提供占位符

安装: 227

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 1

开放问题: 0

语言:Gherkin

类型:psalm-plugin

0.4.0 2023-03-18 10:23 UTC

This package is auto-updated.

Last update: 2024-09-18 13:40:11 UTC


README

PSR-3(psr/log)插件,适用于 Psalm

安装

$ composer require --dev struggle-for-php/sfp-psalm-psr-log-plugin
$ vendor/bin/psalm-plugin enable struggle-for-php/sfp-psalm-psr-log-plugin

示例

<?php

use Psr\Log\LoggerInterface;

class FooController
{
    /** @var LoggerInterface */
    private $logger;

    public function anyAction()
    {
        try {
            // do something
        } catch (\Exception $e) {
            $this->logger->error('error happened.', ['exception' => $e->getMessage()]);
        }
    }
}

将产生 ERROR 输出

ERROR: InvalidArgument - src/Foo.php:14:45 - Argument 2 of Psr\Log\LoggerInterface::error expects array{exception?: Exception}, array{exception: string} provided
            $this->logger->error('error happened.', ['exception' => $e->getMessage()]);

配置

如果您想允许 Throwable 通过日志上下文的 exception

        <plugins>
          <pluginClass class="Sfp\Psalm\PsrLogPlugin\Plugin">
            <throwable>1</throwable>
          </pluginClass>
        </plugins>