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

Stubs to let Psalm understand psr/log (PSR-3) strictly

Installs: 227

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Language:Gherkin

Type:psalm-plugin

0.4.0 2023-03-18 10:23 UTC

This package is auto-updated.

Last update: 2024-10-18 13:49:30 UTC


README

PSR-3 (psr/log) plugin for Psalm

Installation:

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

Example

<?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()]);
        }
    }
}

would be ERROR output:

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()]);

Configure

If you want to allow Throwable to pass logger context's exception.

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