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 Rules Laravel Package

voku/phpstan-rules

Additional PHPStan rules to catch risky and redundant condition logic and comparisons. Detects double negatives, PHP 8 behavior changes (0 vs ''), insane/invalid comparisons (0=='0foo', 0==='0'), and type-mismatch checks (e.g., object vs non-object).

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install the package: Run composer require --dev voku/phpstan-rules.
  2. Enable automatically (recommended): If you use phpstan/extension-installer, rules will be loaded automatically.
  3. Enable manually: Add includes: - vendor/voku/phpstan-rules/rules.neon to your phpstan.neon file.
  4. First use case: Run PHPStan at level 1 or 2 — this package helps catch subtle logical errors (e.g., (string)$foo != ''), which are often missed even at higher levels. It’s especially valuable for teams avoiding PHP 8 BC breaks or enforcing strict value-object usage.

Implementation Patterns

  • Default inclusive rules: You get most value immediately — no config needed. Key checks include:
    • Double negatives (e.g., != '', != 0, !== false)
    • Type coercion gotchas (e.g., 0 == '0foo' under PHP 8)
    • Impossible conditions (e.g., false && true, 0 === '0')
    • Non-empty array/string misuse (e.g., checking if a known non-empty array is empty)
  • Customize enforcement via phpstan.neon:
    parameters:
        voku:
            classesNotInIfConditions: [App\Domain\ValueObject\AbstractValueObject]
            checkForAssignments: true
            checkYodaConditions: true
    
    This enables stricter validation for value objects and enforces preferred coding style (e.g., no assignments in conditionals).
  • Layered with PHPStan levels: Use with higher levels (4–8) to catch edge cases PHPStan’s core doesn’t flag. Especially useful for projects migrating PHP versions or dealing with legacy code.
  • Targeted rule inclusion: Though bundled, rules like DisallowedCallMethodOnNullRule can be included individually for partial adoption (e.g., before upgrading full PHPStan level).

Gotchas and Tips

  • False positives on 0 vs '': PHP 8 changed behavior for loose comparisons (0 == ''false, 0 == '0'true). The rule warns on both, but verify context — some legacy apps rely on pre-PHP 8 semantics.
  • Yoda conditions are style preferences: checkYodaConditions: true may conflict with team conventions. Disable if your team uses Yoda style intentionally (e.g.,防错 if (null === $foo)).
  • Assignment detection caveats: checkForAssignments: true blocks if ($a = $b) but not if (($a = $b) !== null). Use parentheses explicitly if intentional assignment is needed — otherwise, PHPStan will warn.
  • Value-object enforcement: Declaring classesNotInIfConditions reduces noise for VO usage, but ensure subclasses inherit correctly — if EmailVO extends AbstractValueObject, only if ($email->isValid()) is safe.
  • Debug tip: Use --debug with PHPStan to see which helper (IfConditionHelper) flagged an error. Check 3v4l links in docs to verify PHP version behavior.
  • Maturity note: Small stars but active maintenance — verify CI badges and releases. Since it’s a niche ruleset, always composer show -i voku/phpstan-rules before upgrades.
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