Skip to content

Feat/laravel usage provider#293

Closed
seryak wants to merge 4 commits intoshipmonk-rnd:masterfrom
seryak:feat/laravel-usage-provider
Closed

Feat/laravel usage provider#293
seryak wants to merge 4 commits intoshipmonk-rnd:masterfrom
seryak:feat/laravel-usage-provider

Conversation

@seryak
Copy link

@seryak seryak commented Mar 1, 2026

Thank you for this package.

I started using dead-code-detector in my Laravel projects and immediately ran into a wall of false positives. Laravel relies heavily on convention-based invocation: the framework calls controller methods, Artisan command handlers, service provider lifecycle hooks, and Eloquent accessors/mutators through its own runtime machinery — none of which is visible to PHPStan as a static call.

What this covers
Pattern Mechanism Example
Route-registered controller methods AST: StaticCall on Route::get/post/... Route::get('/home', [HomeController::class, 'index']) and 'HomeController@index' string syntax
Artisan command handle() ClassMethod + reflection Classes extending Illuminate\Console\Command
Service provider lifecycle ClassMethod + reflection register() / boot() in classes extending Illuminate\Support\ServiceProvider
Eloquent accessors & mutators ClassMethod + reflection getAttribute() / setAttribute() in classes extending Illuminate\Database\Eloquent\Model
The provid

TODO / known limitations
The following patterns are not yet handled and are candidates for follow-up PRs:

Form Request validation — rules() and authorize() methods in classes extending Illuminate\Foundation\Http\FormRequest
Event Listeners — handle() in classes implementing Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit or registered via Event::listen()
I am open to feedback on the approach and happy to extend the coverage before merging if the maintainers prefer.

seryak added 2 commits March 1, 2026 18:44
Adds a new MemberUsageProvider for Laravel that suppresses false
positives from dead code detection for framework-specific patterns.

Detects as used:
- Controller methods registered via Route::get/post/etc(), supporting
  both [Controller::class, 'method'] array and 'Controller@method'
  string syntax
- handle() in Artisan command classes (Illuminate\Console\Command)
- register() and boot() in service provider classes
  (Illuminate\Support\ServiceProvider)
- get*Attribute() and set*Attribute() accessor/mutator methods in
  Eloquent model classes (Illuminate\Database\Eloquent\Model)

The provider is auto-enabled when laravel/framework is detected in the
project's installed packages. It can be forced on/off via:

    parameters:
        shipmonkDeadCode:
            usageProviders:
                laravel:
                    enabled: true
@seryak seryak marked this pull request as draft March 1, 2026 12:26
seryak added 2 commits March 1, 2026 19:28
…atives

str_contains(), str_starts_with(), str_ends_with() require PHP 8.0.
Replace with strpos()/substr() to maintain PHP 7.4 compatibility.

Also fixes PHPStan 'Offset 1 might not exist on non-empty-list<string>'
by replacing explode() destructuring with explicit strpos()/substr().
- Move constants before properties (ClassStructure)
- Split multi-parameter method signatures to multiple lines
- Sort use statements alphabetically
- Remove duplicate spaces in comments
@seryak seryak marked this pull request as ready for review March 1, 2026 12:35
@janedbal
Copy link
Member

janedbal commented Mar 1, 2026

Hello, I tried bigger-scope in #294, it should cover all your cases. Can you try it out? Thank you!

composer require --dev shipmonk/dead-code-detector:dev-laravel-support

@seryak
Copy link
Author

seryak commented Mar 2, 2026

For the next few days I'll be traveling. In about a week I'll be able to test the Eloquent and Laravel rules on a real project and provide feedback. Should I close my PR for now?

@janedbal
Copy link
Member

janedbal commented Mar 2, 2026

I think we can close this one, thanks anyway!

@janedbal janedbal closed this Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants