canvural/larastan-strict-rules

Extra strict and opinionated PHPStan rules for Laravel.

Fund package maintenance!
canvural
www.paypal.me/canvural

Installs: 580 632

Dependents: 2

Suggesters: 0

Security: 0

Stars: 73

Watchers: 3

Forks: 4

Open Issues: 6

Type:phpstan-extension

2.1.10 2023-12-07 08:24 UTC

README

Extra strict and opinionated PHPStan rules for Laravel.

Tests codecov PHPStan

Installation

You can install the package via composer:

composer require --dev canvural/larastan-strict-rules

To enable all the rules, include rules.neon in your project's PHPStan config:

includes:
    - vendor/canvural/larastan-strict-rules/rules.neon

Enabling rules one-by-one

If you don't want to start using all the available strict rules at once but only one or two, you can! Just don't include the whole rules.neon from this package in your configuration, but look at its contents and copy only the rules you want to your configuration under the services key. For example:

services:
    -
        class: Vural\LarastanStrictRules\Rules\NoDynamicWhereRule
        tags:
            - phpstan.rules.rule
    -
        class: Vural\LarastanStrictRules\Rules\NoFacadeRule
        tags:
            - phpstan.rules.rule

Rules

NoDynamicWhereRule

This rule disallows the usage of dynamic where methods on Eloquent query builder.

NoFacadeRule

This rule disallows the usage of Laravel Facades. Also, checks for the real time facade usage.

NoGlobalLaravelFunctionRule

This rule disallows the usage of global helper functions that comes with Laravel.

If you want to allow some functions, you can use the allowedFunctions parameter for this rule. Like so:

-
    class: Vural\LarastanStrictRules\Rules\NoGlobalLaravelFunctionRule
    arguments:
        allowedFunctions:
            - app
            - event

NoValidationInControllerRule

This rule disallows validating the request in controllers.

ScopeShouldReturnQueryBuilderRule

This rule makes sure Illuminate\Database\Eloquent\Builder instance is returned from Eloquent local query scopes.

NoLocalQueryScopeRule

This rule disallows the usage of local model query scopes all together.

NoPropertyAccessorRule

This rule disallows the usage of model property accessors.

ListenerShouldHaveVoidReturnTypeRule

This rule makes sure your event listeners have a void return type.

If you return false from an event listener, Laravel will stop the propagation of an event to other listeners. Sometimes this can be useful. But other time it can cause bugs that you will need to debug for hours. So this opinionated rule makes sure you always have void return type for your event listeners.

You need to configure this rule by adding the directories that your event listeners are in to the listenerPaths parameter:

-
    class: Vural\LarastanStrictRules\Rules\ListenerShouldHaveVoidReturnTypeRule
    arguments:
        listenerPaths:
            - app/Listeners
            - app/DomainA/Listeners

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

People:

License

The MIT License (MIT). Please see License File for more information.