Weave 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

localheinz/phpstan-rules

A Composer package of PHPStan rules from ergebnis that enforce stricter coding standards, covering areas like final classes, named arguments, and class extension. Install via Composer and include rules.neon, or let phpstan/extension-installer handle it automatically. Best layered on top of PHPStan's...

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package as a development dependency with composer require --dev ergebnis/phpstan-rules. Include its built-in rules.neon configuration in your main phpstan.neon (automatically handled if using phpstan/extension-installer). Start by running vendor/bin/phpstan analyze to see the new rules in action — they enforce strict, modern PHP idioms, so expect immediate feedback on patterns like missing declare(strict_types=1), non-final classes, or null default values. The simplest first win is enabling declareStrictTypesRule, which ensures every file opt-in to strict typing.

If you are upgrading from 2.14.1 or earlier, be aware that 2.14.2 fixes a registration bug: Closures\NoParameterWithNullDefaultValueRule was previously declared in the source but never wired into rules.neon, so it silently did nothing. After upgrading, that rule becomes active and will report closures with nullable parameters defaulting to null. Run the analyzer once on a clean branch to gauge the blast radius before merging.

Implementation Patterns

  • Gradual adoption: Use parameters.ergebnis.*.enabled: false to temporarily disable rules during migration (e.g., disable FinalRule while refactoring legacy codebases), then enable gradually.
  • Template for new projects: Run phpstan-rules on fresh repos to enforce idioms like NoNamedArgumentRule (avoiding brittle named args), NoEvalRule, and TestCaseWithSuffixRule for consistent test names.
  • Customization per domain: Override noExtends.classesAllowedToBeExtended to permit base entities from frameworks (e.g., Symfony’s AbstractController) while blocking arbitrary inheritance elsewhere.
  • CI enforcement: Add a dedicated phpstan-rules check in CI with --error-format=github for actionable PR comments, using the -c flag to point to a dedicated phpstan-rules.neon that extends rules.neon but overrides exclusions as needed.
  • Closure signatures after 2.14.2: The newly registered NoParameterWithNullDefaultValueRule targets patterns like function (?string $x = null) {}. The idiomatic fix is to drop the nullable marker (function (string $x = '') {}) or use an explicit sentinel; where the null really is meaningful, keep it and suppress at the call site rather than downgrading the rule globally.

Gotchas and Tips

  • Silent rule activation on upgrade: Because 2.14.2 only changed rules.neon, nothing in your composer.json constraints signals a behavioural change — a routine composer update will surface new errors on closure defaults. Pin ergebnis/phpstan-rules to ~2.14.2 if you need deterministic CI across a release window.
  • Stale baselines: Any baseline generated before 2.14.2 cannot contain entries for Closures\NoParameterWithNullDefaultValueRule (the rule never ran). Regenerate the baseline after upgrading, and review the new entries rather than blindly committing them.
  • Conflict with phpstan/phpstan-strict-rules: Both define NoNullableReturnTypeDeclarationRule — ensure your config loads ergebnis/phpstan-rules after to prevent subtle override issues.
  • False positives with doctrine annotations: The FinalRule intentionally skips Doctrine entities annotated with @ORM\Entity — ensure your annotations are correct and up-to-date, otherwise false negatives may occur.
  • noIssetRule pitfalls: This rule blocks isset() entirely — consider adding #phpstan-ignore-next-line# for performance-critical null checks on arrays where array_key_exists() is too expensive.
  • Refactoring with IDE support: Pair FinalRule and NoExtendsRule with IDE features (e.g., PhpStorm’s "Make final" quick fix) to streamline refactoring.
  • Debugging config: Run vendor/bin/phpstan analyse -c vendor/ergebnis/phpstan-rules/rules.neon to verify the ruleset is correctly loaded, especially if extension-installer fails silently. To confirm a specific rule is actually registered after an upgrade, dump the effective config with vendor/bin/phpstan analyse --debug or grep rules.neon for the rule’s service name — the 2.14.2 bug is a good reminder that source presence does not imply registration.
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.
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core