Skip to content

AddParamTypeFromDependsRector causes error when no depends was used #9351

@LorisZ

Description

@LorisZ

Bug Report

Subject Details
Rector version 2.1.5

In some phpunit tests with methods that only have one parameter which is untyped, it can cause this error: PhpParser\Node\Stmt\ClassLike::getMethod(): Argument #1 ($name) must be of type string, null given, called in vendor/rector/rector/vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector.php128

The class does not use the depends annotation or attribute.

Minimal PHP Code Causing Issue

Somehow I was not able to reproduce it on the demo, but here is an example that caused it for me locally:

<?php

use PHPUnit\Framework\TestCase;

final class ExampleTest extends TestCase
{
    public function test($arg): void
    {
    }
}

./vendor/bin/rector --only="Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddParamTypeFromDependsRector" ExampleTest.php -vvv
the stacktrace:
        "System error: "PhpParser\Node\Stmt\ClassLike::getMethod(): Argument #1 ($name) must be of type string, null   
         given, called in                                                                                               
         vendor/rector/rector/vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector
         .php126"                                                                                                       
                                                                                                                        
         Stack trace:                                                                                                   
         #0                                                                                                             
         vendor/rector/rector/vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector
         .php(126): PhpParser\Node\Stmt\ClassLike->getMethod()                                                          
         #1                                                                                                             
         vendor/rector/rector/vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddParamTypeFromDependsRector
         .php(111):                                                                                                     
         Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector->resolveReturnTypeOfDependsMethod()     
         #2 vendor/rector/rector/src/Rector/AbstractRector.php(119):                                                    
         Rector\PHPUnit\CodeQuality\Rector\Class_\AddParamTypeFromDependsRector->refactor()                             
         #3 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(173):                          
         Rector\Rector\AbstractRector->enterNode()                                                                      
         #4 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(90):                           
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #5 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(200):                          
         PhpParser\NodeTraverser->traverseNode()                                                                        
         #6 vendor/rector/rector/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(71):                           
         PhpParser\NodeTraverser->traverseArray()                                                                       
         #7 vendor/rector/rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php(49):                               
         PhpParser\NodeTraverser->traverse()                                                                            
         #8 vendor/rector/rector/src/Application/FileProcessor.php(95):                                                 
         Rector\PhpParser\NodeTraverser\RectorNodeTraverser->traverse()                                                 
         #9 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(183):                                     
         Rector\Application\FileProcessor->processFile()                                                                
         #10 vendor/rector/rector/src/Application/ApplicationFileProcessor.php(160):                                    
         Rector\Application\ApplicationFileProcessor->processFile()                                                     
         #11 vendor/rector/rector/src/Console/Command/WorkerCommand.php(128):                                           
         Rector\Application\ApplicationFileProcessor->processFiles()                                                    
         #12 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         Rector\Console\Command\WorkerCommand->{closure:Rector\Console\Command\WorkerCommand::runWorker():120}()        
         #13 vendor/rector/rector/vendor/clue/ndjson-react/src/Decoder.php(117):                                        
         RectorPrefix202509\Evenement\EventEmitter->emit()                                                              
         #14 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202509\Clue\React\NDJson\Decoder->handleData()                                                     
         #15 vendor/rector/rector/vendor/react/stream/src/Util.php(62):                                                 
         RectorPrefix202509\Evenement\EventEmitter->emit()                                                              
         #16 vendor/rector/rector/vendor/evenement/evenement/src/EventEmitterTrait.php(111):                            
         RectorPrefix202509\React\Stream\Util::{closure:RectorPrefix202509\React\Stream\Util::forwardEvents():61}()     
         #17 vendor/rector/rector/vendor/react/stream/src/DuplexResourceStream.php(168):                                
         RectorPrefix202509\Evenement\EventEmitter->emit()                                                              
         #18 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(201):                                
         RectorPrefix202509\React\Stream\DuplexResourceStream->handleData()                                             
         #19 vendor/rector/rector/vendor/react/event-loop/src/StreamSelectLoop.php(173):                                
         RectorPrefix202509\React\EventLoop\StreamSelectLoop->waitForStreamActivity()                                   
         #20 vendor/rector/rector/src/Console/Command/WorkerCommand.php(98):                                            
         RectorPrefix202509\React\EventLoop\StreamSelectLoop->run()                                                     
         #21 vendor/rector/rector/vendor/symfony/console/Command/Command.php(289):                                      
         Rector\Console\Command\WorkerCommand->execute()                                                                
         #22 vendor/rector/rector/vendor/symfony/console/Application.php(900):                                          
         RectorPrefix202509\Symfony\Component\Console\Command\Command->run()                                            
         #23 vendor/rector/rector/vendor/symfony/console/Application.php(279):                                          
         RectorPrefix202509\Symfony\Component\Console\Application->doRunCommand()                                       
         #24 vendor/rector/rector/src/Console/ConsoleApplication.php(66):                                               
         RectorPrefix202509\Symfony\Component\Console\Application->doRun()                                              
         #25 vendor/rector/rector/vendor/symfony/console/Application.php(162):                                          
         Rector\Console\ConsoleApplication->doRun()                                                                     
         #26 vendor/rector/rector/bin/rector.php(130): RectorPrefix202509\Symfony\Component\Console\Application->run()  
         #27 vendor/rector/rector/bin/rector(5): require_once('...')                                                    
         #28 vendor/bin/rector(119): include('...')                                                                     
         #29 {main}". On line: 99                                               

Expected Behaviour

Rector should not give any error as the file does not even use the depends annotation or attribute.

possible solution

Maybe there's a check missing that the depends annotation or attribute is actually used? Alternatively what seems to work is checking if the method is different from null inside the resolveReturnTypeOfDependsMethod method, but I'm not sure this is correct. Probably a check before would be better, but I don't know what the best way to do that is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions