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

phpcs error on rule classes - must be of the type integer #22947

Merged
merged 1 commit into from
May 24, 2019
Merged

phpcs error on rule classes - must be of the type integer #22947

merged 1 commit into from
May 24, 2019

Conversation

Nazar65
Copy link
Member

@Nazar65 Nazar65 commented May 20, 2019

Description (*)

Unfortunately #22081 does in no way fix the real issue in the Sniff, but pushes the problem a few lines down in the code.

The issue is that the PHP_CodeSniffer\Files\File::findPrevious method returns either a pointer to the previous token of a type or false if none is found. This case is triggered when the CodeSniffer is running on a file with no closing comment tags.

Earlier the issue was because this false was passed to a function which expected an int instead. What #22081 does is to cast the boolean into an integer, which obviously is not correct (as the Sniff now treats the code like there is a comment closing token at position 0 when in fact there is none in the file).

Now the Sniff returns a new error as it's trying to get the closing comment from the token list:

An error occurred during processing; checking has been aborted.
The error message was: Undefined index: comment_closer in
dev/tests/static/framework/Magento/Sniffs/Annotation/ClassAnnotationStructureSniff.php on line 102

Fixed Issues (if relevant)

#20186: Issue title

Manual testing scenarios (*)

  1. clean Magento installation on 2.3-develop branch
  2. create a Foo.php file with the following contents
<?php

class Foo{}
  1. run the phpcs on that file with the Magento ruleset ./vendor/bin/phpcs --standard=./dev/tests/static/framework/Magento/ Foo.php

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented May 20, 2019

Hi @Nazar65. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@Nazar65 Nazar65 requested review from orlangur and p-bystritsky and removed request for orlangur May 20, 2019 09:34
$phpcsFile->addError('Comment block is missing', $stackPtr -1, 'MethodArguments');
return;
}
$this->validateAnnotationBlockExists($phpcsFile, $previousCommentClosePtr, (int)$stackPtr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this cast to (int) still necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

@orlangur Nope, can i remove it ?

@@ -663,6 +663,10 @@ private function noneParamsAligned(array $argumentPositions, array $commentPosit
foreach ($argumentPositions as $index => $argumentPosition) {
$commentPosition = $commentPositions[$index];
$type = $paramDefinitions[$index]['type'];
if ($type == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why need this?

Copy link
Member Author

@Nazar65 Nazar65 May 20, 2019

Choose a reason for hiding this comment

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

@orlangur Found one new issue in dev branch
DeepinScreenshot_select-area_20190520153623

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add more info - I'm more suspicious about the unset

Copy link
Member Author

Choose a reason for hiding this comment

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

@orlangur well, unset is not needed, when we have type null, we can just skip iteration.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, please do so and also make a check strict (=== null).

@ghost ghost assigned orlangur May 20, 2019
@@ -663,6 +663,10 @@ private function noneParamsAligned(array $argumentPositions, array $commentPosit
foreach ($argumentPositions as $index => $argumentPosition) {
$commentPosition = $commentPositions[$index];
$type = $paramDefinitions[$index]['type'];
if ($type == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, please do so and also make a check strict (=== null).

@Nazar65
Copy link
Member Author

Nazar65 commented May 20, 2019

@orlangur done ✔️

@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-5139 has been created to process this Pull Request

@soleksii
Copy link

✔️ QA Passed

@mattijv
Copy link
Contributor

mattijv commented May 21, 2019

@Nazar65 The exact same problem is present in the Magento\Sniffs\Annotation\MethodArgumentsSniff class lines 551 to 553.

You can use this class to test.

<?php

class Foo {

    public function bar()
    {
        return 0;
    }

}

The run the sniffs with ./vendor/bin/phpcs --standard=./dev/tests/static/framework/Magento/ Foo.php, which leads to the following error:

PHP Fatal error:  Uncaught TypeError: Argument 2 passed to
Magento\Sniffs\Annotation\MethodArgumentsSniff::validateCommentBlockExists()
must be of the type integer, boolean given

@@ -97,8 +97,12 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0);
$this->validateAnnotationBlockExists($phpcsFile, (int)$previousCommentClosePtr, (int)$stackPtr);
$commentStartPtr = (int)$phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
if ($previousCommentClosePtr === false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Revert.

@@ -45,6 +45,10 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($stackPtr), 0);
$commentEndPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr), 0);
if ($commentStartPtr === false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Revert.

@@ -550,8 +550,12 @@ public function process(File $phpcsFile, $stackPtr)
$numTokens = count($tokens);
$previousCommentOpenPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
$previousCommentClosePtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, $stackPtr - 1, 0);
if (!$this->validateCommentBlockExists($phpcsFile, $previousCommentClosePtr, $stackPtr)) {
$phpcsFile->addError('Comment block is missing', $stackPtr, 'MethodArguments');
if ($previousCommentClosePtr !== false && $previousCommentOpenPtr !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Revert.

@Nazar65
Copy link
Member Author

Nazar65 commented May 22, 2019

@orlangur done ✔️

@magento-engcom-team
Copy link
Contributor

Hi @orlangur, thank you for the review.
ENGCOM-5139 has been created to process this Pull Request

@soleksii
Copy link

✔️ QA Passed

@m2-assistant
Copy link

m2-assistant bot commented May 24, 2019

Hi @Nazar65, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@magento-engcom-team magento-engcom-team added this to the Release: 2.3.3 milestone May 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants