Skip to content

[RFC] Add PHPStan rules for forbidden methods and unnecessary named arguments #209

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

Closed
wants to merge 1 commit into from

Conversation

chr-hertel
Copy link
Contributor

Q A
Bug fix? no
New feature? no
Docs? no
Issues
License MIT

I wanted to catch some stuff that a) happens to me and b) could be a rule to enforce while working with named arguments if you ask me.

  • Add ForbiddenMethodCallsRule to prevent debugging functions like dump(), var_dump(), print_r(), dd() in production code
  • Add ForbidUnnecessaryNamedArgumentsRule to detect unnecessary named arguments when all parameters are provided in correct order
  • Support function calls, method calls, and constructor calls
  • Handle mixed positional and named arguments
  • Register both rules in extension.neon for automatic enforcement across all components

🤖 Generated with Claude Code

- Add ForbiddenMethodCallsRule to prevent debugging functions like dump(), var_dump(), print_r(), dd() in production code
- Add ForbidUnnecessaryNamedArgumentsRule to detect unnecessary named arguments when all parameters are provided in correct order
- Support function calls, method calls, and constructor calls
- Handle mixed positional and named arguments
- Register both rules in extension.neon for automatic enforcement across all components

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@chr-hertel chr-hertel added the RFC RFC = Request For Comments (proposals about features that you want to be discussed) label Jul 26, 2025
* @implements Rule<FuncCall>
* @author Claude <[email protected]>
*/
final class ForbiddenMethodCallsRule implements Rule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consider using https://github.com/spaze/phpstan-disallowed-calls for this.

@@ -28,6 +28,7 @@
* for better error handling and consistency across the Symfony AI monorepo.
*
* @implements Rule<Node>
* @author Claude <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be removed

* @implements Rule<Node>
* @author Claude <[email protected]>
*/
final class ForbidUnnecessaryNamedArgumentsRule implements Rule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a nice one for phpstan official repo 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was rather thinking of PHP CS Fixer - feels a bit too opinionated and more about style than types

@chr-hertel
Copy link
Contributor Author

Thanks @OskarStark & @derrabus for having a look! Would you in general agree on that rule about named arguments or is it too opinionated?

@derrabus
Copy link
Member

derrabus commented Jul 26, 2025

Would you in general agree on that rule about named arguments or is it too opinionated?

What does the rule do? Forbid the usage of named arguments if I could've made the same call with ordered arguments?

It is opinionated all right. Not sure what you're trying to solve. If I have a value object like this:

final readonly class Foo {
    public function __construct(
        public string $a,
        public string $b,
        public string $c,
    ) {}
}

Then I wouldn't be allowed to do this?

$foo = new Foo(
    a: 'some value',
    b: 'some value',
    c: 'some value',
);

Meh.

@chr-hertel
Copy link
Contributor Author

chr-hertel commented Jul 26, 2025

Not sure what you're trying to solve.

To be honest, I just don't want to think about it while reviewing code 😬
(edit: maybe i'm too lazy here :D)

Then I wouldn't be allowed to do this?

$foo = new Foo(
    a: 'some value',
    b: 'some value',
    c: 'some value',
);

Yup, and it's not that I care to much, but was thinking that's the easier rule.

But if we don't mind in general, I can just drop thinking about it while reviewing 😆

@chr-hertel
Copy link
Contributor Author

no real need, let's close it

@chr-hertel chr-hertel closed this Jul 27, 2025
@chr-hertel chr-hertel deleted the feature/phpstan-rules branch July 27, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants