Skip to content

Dynamic class name constructor with named arguments issue #952

@eithed

Description

@eithed

Given this code:

<?php

declare(strict_types=1);

namespace App;

class Foo { 
    public function __construct(
        public string $bar
    ){}
}

class Bar { 
    public function __construct(
    ){
        $class = "Foo";
        new $class(bar: 'xyz');
    }
}

will result in following error

Internal error: Call to undefined method PhpParser\Node\Expr\Variable::toString() while analysing file                                                              
     test/app/Foo.php                                                                                               
     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml:                                                           
     ## test/vendor/ergebnis/phpstan-rules/src/CallLikes/NoNamedArgumentRule.php(160)                               
     #0 test/vendor/ergebnis/phpstan-rules/src/CallLikes/NoNamedArgumentRule.php(53):                               
     Ergebnis\PHPStan\Rules\CallLikes\NoNamedArgumentRule::describeCallable(Object(PhpParser\Node\Expr\New_), Object(PHPStan\Analyser\MutatingScope))                    
     #1 phar://test/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(110):                         
     Ergebnis\PHPStan\Rules\CallLikes\NoNamedArgumentRule->processNode(Object(PhpParser\Node\Expr\New_), Object(PHPStan\Analyser\MutatingScope))                         
     #2 phar://test/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassStatementsGatherer.php(116):                  
     PHPStan\Analyser\FileAnalyser->{closure:PHPStan\Analyser\FileAnalyser::analyseFile():90}(Object(PhpParser\Node\Expr\New_),                                          
     Object(PHPStan\Analyser\MutatingScope))

PHPStan version is 2.1.17
ergebnis/phpstan-rules version is 2.10.4

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions