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

ArrayKeyFirstLastRector fails when calling key without reset or end #2455

Closed
JoyceBabu opened this issue Dec 19, 2019 · 1 comment · Fixed by #2489
Closed

ArrayKeyFirstLastRector fails when calling key without reset or end #2455

JoyceBabu opened this issue Dec 19, 2019 · 1 comment · Fixed by #2489
Labels

Comments

@JoyceBabu
Copy link
Contributor

Subject Details
Rector version v0.6.2
PHP version PHP 7.3.12
Full Command ./vendor/bin/rector process rector/abc.php -vvv

Current Behaviour

composer/bin/rector process rector/abc.php -vvv
Rector v0.6.2
Config file: rector.yaml

[parsing] rector/abc.php
[refactoring] rector/abc.php
    [applying] Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector
PHP Notice:  Undefined index: ksort in /path/to/my/files/vendor/rector/rector/packages/Php73/src/Rector/FuncCall/ArrayKeyFirstLastRector.php on line 114

In Name.php line 219:

  [InvalidArgumentException]
  Expected string, array of parts or Name instance


Exception trace:
  at phar:///path/to/my/files/vendor/phpstan/phpstan/phpstan.phar/vendor/nikic/php-parser/lib/PhpParser/Node/Name.php:219
 PhpParser\Node\Name::prepareName() at phar:///path/to/my/files/vendor/phpstan/phpstan/phpstan.phar/vendor/nikic/php-parser/lib/PhpParser/Node/Name.php:21
 PhpParser\Node\Name->__construct() at /path/to/my/files/vendor/rector/rector/packages/Php73/src/Rector/FuncCall/ArrayKeyFirstLastRector.php:115
 Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector->refactor() at /path/to/my/files/vendor/rector/rector/src/Rector/AbstractRector.php:116
 Rector\Rector\AbstractRector->enterNode() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:123
 PhpParser\NodeTraverser->traverseNode() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
 PhpParser\NodeTraverser->traverseArray() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
 PhpParser\NodeTraverser->traverseNode() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
 PhpParser\NodeTraverser->traverseArray() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
 PhpParser\NodeTraverser->traverseNode() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
 PhpParser\NodeTraverser->traverseArray() at /path/to/my/files/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:91
 PhpParser\NodeTraverser->traverse() at /path/to/my/files/vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php:62
 Rector\PhpParser\NodeTraverser\RectorNodeTraverser->traverse() at /path/to/my/files/vendor/rector/rector/src/Application/FileProcessor.php:134
 Rector\Application\FileProcessor->refactor() at /path/to/my/files/vendor/rector/rector/src/Application/RectorApplication.php:155
 Rector\Application\RectorApplication->Rector\Application\{closure}() at /path/to/my/files/vendor/rector/rector/src/Application/RectorApplication.php:221
 Rector\Application\RectorApplication->tryCatchWrapper() at /path/to/my/files/vendor/rector/rector/src/Application/RectorApplication.php:156
 Rector\Application\RectorApplication->runOnFileInfos() at /path/to/my/files/vendor/rector/rector/src/Console/Command/ProcessCommand.php:188
 Rector\Console\Command\ProcessCommand->execute() at /path/to/my/files/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /path/to/my/files/vendor/rector/rector/src/Console/Command/AbstractCommand.php:34
 Rector\Console\Command\AbstractCommand->run() at /path/to/my/files/vendor/symfony/console/Application.php:1011
 Symfony\Component\Console\Application->doRunCommand() at /path/to/my/files/vendor/symfony/console/Application.php:272
 Symfony\Component\Console\Application->doRun() at /path/to/my/files/vendor/rector/rector/src/Console/Application.php:93
 Rector\Console\Application->doRun() at /path/to/my/files/vendor/symfony/console/Application.php:148
 Symfony\Component\Console\Application->run() at /path/to/my/files/vendor/rector/rector/bin/rector:48

process [-n|--dry-run] [-a|--autoload-file AUTOLOAD-FILE] [-e|--hide-autoload-errors] [--match-git-diff] [-r|--rule RULE] [-o|--output-format [OUTPUT-FORMAT]] [--] [<source>...]

Minimal PHP Code Causing Issue

<?php
$a = [];
ksort($a);
key($a);

Expected Behaviour

Rector should run without any changes.

To be frank, I am not sure what should be the expected behaviour. Should rector leave the code as-is, or should it still replace the key with array_key_first since ksort internally resets the array pointer?

@TomasVotruba
Copy link
Member

Thanks for reporting the bug. I'll look into it

TomasVotruba added a commit that referenced this issue Jun 9, 2022
rectorphp/rector-src@cb1473a [Renaming] Do not rename class with same name inside different namespace on RenameClassRector (#2455)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants