Skip to content
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

[TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector rule #5547

Conversation

peterfox
Copy link
Contributor

@peterfox peterfox commented Feb 3, 2024

Changes

  • Adds the new AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector in the TypeDeclaration category
  • Adds a new value object for configuring the rule
  • Adds tests for the rule
  • Updates the rule list

Why

The benefits are hopefully obvious. In PHP frameworks like Laravel, methods etc. are often used with FunctionLike parameters but it can be difficult to know the correct type for Parameters. This rule would allow for enforcing types on method and static calls.

-(new SomeClass)->process(function ($parameter) {});
+(new SomeClass)->process(function (string $parameter) {});

Using example parameters:

new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration(
    'SomeClass',
    'process',
    0, // position of the FunctionLike in the CallLike object, can also be a string of the args name.
    0, // position of the Param in the FunctionLike
    new StringType()
),

Notes

Both numeric position and named args for a CallLike are supported but positional args are ignored if they have a name. For example if you wanted to cover both scenarios in a code base you could reference the position and the named arg successfully and not worry about declaring the type on the wrong param.

@peterfox peterfox force-pushed the feature/add-param-type-for-function-like-within-method-call-declaration-rector branch from 2561766 to de98d5c Compare February 4, 2024 10:58
@peterfox peterfox force-pushed the feature/add-param-type-for-function-like-within-method-call-declaration-rector branch from de98d5c to b416e12 Compare February 4, 2024 15:34
@peterfox peterfox changed the title [TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeDeclarationRector rule [TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector rule Feb 4, 2024
peterfox and others added 3 commits February 18, 2024 04:32
Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
…-type-for-function-like-within-method-call-declaration-rector

# Conflicts:
#	build/target-repository/docs/rector_rules_overview.md
…call-declaration-rector' of github.com:peterfox/rector-src into feature/add-param-type-for-function-like-within-method-call-declaration-rector
@TomasVotruba
Copy link
Member

Thank you 👍

@TomasVotruba TomasVotruba merged commit 18a8aec into rectorphp:main Feb 22, 2024
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants