Skip to content

Conversation

RuZniki
Copy link
Contributor

@RuZniki RuZniki commented Jan 13, 2025

Copy link
Collaborator

@klausi klausi left a comment

Choose a reason for hiding this comment

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

@RuZniki nice to hear from you :)

Left some comments, please also check the test fails.

{
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr + 1]['type'] === 'T_STRING'
Copy link
Collaborator

Choose a reason for hiding this comment

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

for all those conditions we should check first with isset() if the next token is there. This will help prevent failures in live coding when the code is just being written.

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 swtiched to ->findNext, do you think this is enough?

@RuZniki RuZniki force-pushed the attribute_hook_argument branch from ff01836 to 0f46b15 Compare January 21, 2025 07:38
@RuZniki
Copy link
Contributor Author

RuZniki commented Jan 21, 2025

@klausi I am also happy to get feedback from you, thanks a lot for your review.

I moved sniff to \Drupal\Sniffs\Attributes\ValidHookNameSniff.
And moved tests from bad.php to \Drupal\Test\Attributes\ValidHookNameUnitTest.
I decide not to test, when name arguments for Hook attribute in wrong order. Example:

#[Hook(method: "userLogin", hook: "hook_user_login")] 

I believe this should not be a part of this sniff.

I add special handling for hook_hook_info(). But still not sure about this case.

PR is ready for review.

@RuZniki RuZniki requested a review from klausi January 21, 2025 08:17
@RuZniki
Copy link
Contributor Author

RuZniki commented Jan 21, 2025

I removed special handling for "hook_hook_info()".

Copy link
Collaborator

@klausi klausi left a comment

Choose a reason for hiding this comment

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

Sorry for the delay!

Looks good to me, just 2 minor things inline.


if (strpos($hookNameValue, 'hook_') === 0) {
$fix = $phpcsFile->addFixableWarning("Hook name should not start with 'hook_'. Hook name used: $hookNameValue", $hookName, 'HookPrefix');
if ($fix === true && strlen($hookNameValue) > 5) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

the strlen() condition here can be removed, this was already checked before.

We should not add additional conditions with $fix, it would be very confusing if the fixer does not always work.

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 tried to avoid case when hook is not finished, and it's just "hook_".
Now I get your point, I will move second clause to above statement. Thanks.

$fix = $phpcsFile->addFixableWarning("Hook name should not start with 'hook_'. Hook name used: $hookNameValue", $hookName, 'HookPrefix');
if ($fix === true && strlen($hookNameValue) > 5) {
// Remove "hook_" prefix.
$hookNameValueFixed = substr($hookNameValue, 5);
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should use this in the error message above like "The hook name should not start with 'hook_', expected '%s' but found '%s".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I update error message.
Result example

The hook name should not start with 'hook_', expected 'node_alter' but found 'hook_node_alter' (Drupal.Attributes.ValidHookName.HookPrefix)

@RuZniki RuZniki force-pushed the attribute_hook_argument branch from 5e27267 to 826142d Compare February 1, 2025 08:21
@RuZniki RuZniki requested a review from klausi February 1, 2025 11:10
@klausi klausi changed the title feat(ValidHookAttributeArgumentName): Attribute hook argument name check feat(ValidHookName): Add sniff for hook name in PHP attributes Feb 3, 2025
@klausi
Copy link
Collaborator

klausi commented Feb 3, 2025

Thanks, Drupal core regression tests are failing, but looks unrelated to this issue. Will check in a follow-up!

@klausi klausi merged commit d8c10bc into pfrenssen:8.3.x Feb 3, 2025
7 of 8 checks passed
@RuZniki RuZniki deleted the attribute_hook_argument branch February 3, 2025 17:31
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