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

Fixed undefined index referring #818

Merged
merged 1 commit into from Sep 4, 2020
Merged

Conversation

a-menshchikov
Copy link
Contributor

Type: bugfix
Issue: Resolves #816
Breaking change: no

@kylekatarnls
Copy link
Member

Thank you @a-menshchikov did you find when/why (with which analyzed code) $parameters[$argumentPosition] is undefined?

@a-menshchikov
Copy link
Contributor Author

@kylekatarnls error rising on the following code:

<?php

declare(strict_types=1);

namespace App\Controller\Api\admin\Settings\Language;

use App\Controller\Api\AbstractApiController;
use App\Entity\Language;
use App\Entity\User\Role;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

final class Activate extends AbstractApiController
{
    /**
     * @Rest\Patch("/settings/language/{id}/activate", name="language.activate")
     * @Rest\View(serializerGroups={"admin:language:status"})
     *
     * @IsGranted(Role::ADMIN)
     */
    public function __invoke(Language $language): View
    {
        $language->setActive(true);

        $this->flush($language);

        return $this->success($language);
    }
}

Full error output (with xdebug enabled):

PHP Notice:  Undefined offset: 0 in /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Rule/AbstractLocalVariable.php on line 271
PHP Stack trace:
PHP   1. {main}() /usr/src/app/vendor/phpmd/phpmd/src/bin/phpmd:0
PHP   2. PHPMD\TextUI\Command::main($args = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/bin/phpmd:61
PHP   3. PHPMD\TextUI\Command->run($opts = *uninitialized*, $ruleSetFactory = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php:148
PHP   4. PHPMD\PHPMD->processFiles($inputPath = *uninitialized*, $ruleSets = *uninitialized*, $renderers = *uninitialized*, $ruleSetFactory = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/TextUI/Command.php:103
PHP   5. PHPMD\Parser->parse($report = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/PHPMD.php:192
PHP   6. PDepend\Engine->analyze() /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php:95
PHP   7. PHPMD\Parser->close() /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Engine.php:357
PHP   8. PDepend\Source\AST\ASTNamespace->accept($visitor = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php:148
PHP   9. PHPMD\Parser->visitNamespace($namespace = *uninitialized*) /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Source/AST/ASTNamespace.php:320
PHP  10. PDepend\Source\AST\ASTClass->accept($visitor = *uninitialized*) /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Source/ASTVisitor/AbstractASTVisitor.php:214
PHP  11. PHPMD\Parser->visitClass($node = *uninitialized*) /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Source/AST/ASTClass.php:196
PHP  12. PHPMD\Parser->visitClass($class = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php:188
PHP  13. PDepend\Source\AST\ASTMethod->accept($visitor = *uninitialized*) /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Source/ASTVisitor/AbstractASTVisitor.php:110
PHP  14. PHPMD\Parser->visitMethod($node = *uninitialized*) /usr/src/app/vendor/pdepend/pdepend/src/main/php/PDepend/Source/AST/ASTMethod.php:219
PHP  15. PHPMD\Parser->apply($node = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php:234
PHP  16. PHPMD\RuleSet->apply($node = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php:259
PHP  17. PHPMD\Rule\CleanCode\UndefinedVariable->apply($node = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/RuleSet.php:255
PHP  18. PHPMD\Rule\CleanCode\UndefinedVariable->isPassedByReference($variable = *uninitialized*) /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php:78
PHP Fatal error:  Uncaught Error: Call to a member function isPassedByReference() on null in /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Rule/AbstractLocalVariable.php:271
Stack trace:
#0 /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php(78): PHPMD\Rule\AbstractLocalVariable->isPassedByReference()
#1 /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/RuleSet.php(255): PHPMD\Rule\CleanCode\UndefinedVariable->apply()
#2 /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php(259): PHPMD\RuleSet->apply()
#3 /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Parser.php(234): PHPMD\Parser->apply()
#4 /usr/src/app/vendor/pdepend/pdepend/src/main/p in /usr/src/app/vendor/phpmd/phpmd/src/main/php/PHPMD/Rule/AbstractLocalVariable.php on line 271

Maybe it will be useful: $functionName in AbstractLocalVariable::isPassedByReference() is equal "flush" and UndefinedVariable check $language variable on error.

@tvbeek
Copy link
Member

tvbeek commented Sep 2, 2020

@a-menshchikov thanks for the PR (and the issue) do you have the possibility to add a test to the PR? With that we have a verification that it is fixed and we don't destroy it in another release.

I assign this PR to you, but feel free to say if you need help creating the tests.

@a-menshchikov
Copy link
Contributor Author

@tvbeek I think I need help with tests because at the moment I have no idea how to create it`s.

@kylekatarnls kylekatarnls added this to the 2.9.1 milestone Sep 3, 2020
@kylekatarnls kylekatarnls self-assigned this Sep 3, 2020
@kylekatarnls
Copy link
Member

No problem. I will use the example you provided to add it.

@kylekatarnls
Copy link
Member

I just tried the same code chunk but get no error. I also tried with PDepend 2.7.1. I think we should merge this and #821, release the patch 2.9.1 to fix the error then take the time to understand why and how it happen to fix it properly.

@kylekatarnls
Copy link
Member

I could reproduce the bug. I'll add the tests in a second PR.

@kylekatarnls kylekatarnls merged commit 81bc04d into phpmd:master Sep 4, 2020
@a-menshchikov a-menshchikov deleted the patch-1 branch September 4, 2020 14:36
@kylekatarnls
Copy link
Member

kylekatarnls commented Sep 4, 2020

For the record: the error happen if there is more arguments in call than in definition:

function foo($a, $b) {}

foo($v1, $v2, $v3);

Or if the definition of the function/method couldn't not be found. I both situation, it would be fine IMO to consider by default the variable is not passed by reference.

@kylekatarnls
Copy link
Member

kylekatarnls commented Sep 4, 2020

Erratum: the actual trigger is the method name "flush". PHPMD tried to match the arguments with the flush() function of PHP to check arguments passed by reference. So it's a problem to detect $this-> and understand that flush is a method and not a function.

So it could lead to argument wrongly detected as passed by reference.

@kylekatarnls
Copy link
Member

Funny fact: PHPStorm seems to do the same mistake as it underlines $undefined in $this->Xpreg_match('', '', $undefined) but not in $this->preg_match('', '', $undefined) .

@a-menshchikov
Copy link
Contributor Author

@kylekatarnls flush method used from trait that imported in parent class:

    protected function flush(?object $entity = null): void
    {
        $this->getManager($entity)->flush();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Fatal error in AbstractLocalVariable
3 participants