Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Phpstan Psr3 Laravel Package

staabm/phpstan-psr3

PHPStan rules that detect PSR-3 logger misuse and security pitfalls: invalid placeholders, context issues, and message formatting mistakes. Works with psr/log, Monolog, Laravel (illuminate/log, LogManager), and REDAXO rex_logger.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package via Composer:
    composer require --dev staabm/phpstan-psr3
  2. Enable it in your PHPStan config (phpstan.neon):
    includes:
        - vendor/staabm/phpstan-psr3/config.neon
    
  3. First use case: Write a logging call using a PSR-3 logger (e.g., Monolog\Logger or any implementation of Psr\Log\LoggerInterface), and PHPStan will now validate placeholders and context keys at compile time — e.g., flagging log('error', 'User {id} not found', ['user_id' => 123]) as invalid because {id}{user_id}.

Implementation Patterns

  • Standard PSR-3 usage: Use context arrays with consistent keys matching placeholders ({key} in the message). The extension validates that all placeholders are covered and no extra context is provided.
  • Typed context arrays: Define custom context structures (e.g., ['userId' => int, 'action' => string]) in PHPDocs or via generics, and PHPStan will check type conformance.
  • Integrations: Works seamlessly with Laravel’s Log::channel(), Symfony’s Logger, or direct Psr\Log\LoggerInterface injection — ideal for service classes where structured logging is enforced.
  • Gradual adoption: Start by enabling only strict level (level: 5) and progressively tighten; the extension adds no new errors to pre-existing code unless mismatches exist.

Gotchas and Tips

  • Message placeholder syntax: Only {placeholder} (curly braces) is supported; %placeholder% (Symfony-style) won’t be validated — ensure your messages follow PSR-3.
  • Context key case sensitivity: {UserId} and {userid} are treated as different placeholders — match exactly in your context keys.
  • False positives? Some monolog handlers or processors may mutate context — ignore via ignoreErrors or use @phpstan-ignore-next-line for edge cases where context is dynamically altered.
  • Extensibility: You can extend validation by creating custom rules for domain-specific context keys (e.g., ['tenantId' => string]) using PHPStan’s Custom Rules API — the package doesn’t enforce strict schemas, just checks for consistency with placeholders.
  • Laravel tip: For Log::info('...', ['some_key' => ...]), ensure PHPStan can infer the type via @method annotations if using facades; the extension works best when the logger instance type is unambiguous.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport