Skip to content

feat: add type hints for $this in routes/console.php #6559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2025

Conversation

AJenbo
Copy link
Contributor

@AJenbo AJenbo commented Mar 2, 2025

Summary

This PR adds a type hint for $this in routes/console.php, ensuring better IDE support and improved static analysis when working with console commands.

Why This Change?

  • Improves Developer Experience: IDEs can now provide autocompletion and proper type inference for $this within console command closures.
  • Enhances Static Analysis: Helps tools like PHPStan understand the type of $this, reducing potential false positives in type checks.
  • Better Code Readability: Explicitly declaring $this as an instance of Illuminate\Console\Command makes the code easier to understand for new developers.

Testing & Compatibility

  • No runtime behavior changes; this is a documentation-level improvement.
  • Fully backward compatible with existing Laravel applications.

Note: This replaces #6554, which I closed before realizing it was possible to properly hint the rebound scope. Thanks to @timacdonald for highlighting the dependency.

This further reinforces the importance of explicitly hinting both scopes, as they don’t seem to be well-documented elsewhere.

@AJenbo AJenbo force-pushed the this-console-route branch from d68c34c to 1eb3a2e Compare March 2, 2025 19:03
@taylorotwell taylorotwell merged commit 1390672 into laravel:12.x Mar 4, 2025
4 checks passed
@AJenbo AJenbo deleted the this-console-route branch March 4, 2025 16:03
@AJenbo
Copy link
Contributor Author

AJenbo commented Mar 4, 2025

... oh you edit actually will cause it to complain, it needed to overwrite the context for it to be evaluated correctly. Sorry if I didn't make that clear.

I'll send a new PR for that. Thanks for clearing up what style should be used for class names.

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