Rector: StaticCallOnNonStaticToInstanceCallRector changes method call from static to non-static even though the method is static

Created on 15 May 2019  路  11Comments  路  Source: rectorphp/rector

This code uses a normal static function (signature: public static function uuid()), but the StaticCallOnNonStaticToInstanceCallRector still changes it to this

See the output here: https://gist.github.com/Spriz/93f0a79c9d1c5257713c2a783f1a0f1f#file-rector-cli-txt

bug

Most helpful comment

The fix was very simple in the end :)

Thanks all for reporting :+1:

All 11 comments

This code is properly let alone:

<?php

class Bar
{
    public static function baz()
    {
        return 'foo';
    }
}

class Foo
{
    public function bar()
    {
        return Bar::baz();
    }
}

Output:

$ rector process ./src/Foo.php --dry-run --level php70 -vvv
Rector v0.4.12
Config file: /Users/spriz/code/apacta/apacta-2/rector.yaml

/Users/spriz/code/apacta/apacta-2/src/Foo.php
/Users/spriz/code/apacta/apacta-2/src/Foo.php
Rector\Php\Rector\FunctionLike\Php4ConstructorRector
Rector\Php\Rector\FunctionLike\ExceptionHandlerTypehintRector
Rector\Php\Rector\FunctionLike\Php4ConstructorRector
Rector\Php\Rector\FunctionLike\ExceptionHandlerTypehintRector
Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector
/Users/spriz/code/apacta/apacta-2/src/Foo.php



 [OK] Rector is done! 0 changed files

If you need anything else from me just poke me!

I confirm the bug:

-        if ($traversedPath === ArrayWriter::pathize($this->endpoint)) {
-            $subPath          = '' !== $subPath ? ArrayWriter::pathize($subPath) : $subPath;
+        if ($traversedPath === (new ArrayWriter())->pathize($this->endpoint)) {
+            $subPath          = '' !== $subPath ? (new ArrayWriter())->pathize($subPath) : $subPath;

ArrayWriter is actually a class with a static method ArrayWriter::pathize().

I also receive an error:

...
Rector\Php\Rector\Assign\AssignArrayToStringRector
Rector\CodeQuality\Rector\Assign\CombinedAssignRector
Rector\PHPStan\Rector\Assign\PHPStormVarAnnotationRector
Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector
PHP Fatal error:  Uncaught ReflectionException: Class arrayWriter does not exist in /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php:148
Stack trace:
#0 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php(148): ReflectionClass->__construct('arrayWriter')
#1 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php(122): Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector->isInstantiable(Object(PhpParser\Node\Expr\StaticCall))
#2 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/src/Rector/AbstractRector.php(95): Rector\Php\Rector\StaticCall\St in /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php on line 148

Fatal error: Uncaught ReflectionException: Class arrayWriter does not exist in /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php:148
Stack trace:
#0 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php(148): ReflectionClass->__construct('arrayWriter')
#1 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php(122): Rector\Php\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector->isInstantiable(Object(PhpParser\Node\Expr\StaticCall))
#2 /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/src/Rector/AbstractRector.php(95): Rector\Php\Rector\StaticCall\St in /Users/Aerendir/Documents/Path/To/My/Library/Components/my-library/vendor/rector/rector/packages/Php/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php on line 148

The rector looks for a class arrayWriter that doesn't exist because its name is actually ArrayWriter

Please provide failing test to StaticCallOnNonStaticToInstanceCallRectorTest.
I'll cover it and fix it then.

@TomasVotruba , I'm not able to find the test you mention (nor here on GitHub, nor forking the project in pHPStorm): can you link it here, please?

@ADmad Thanks!

@Aerendir Search sucks here. Better is to hit t and copy paste the name of the class

@TomasVotruba , no, I was simply searching for in the wrong directory: I was searching in tests while the right folder is actually packages/[package]/tests 馃槄

Anyway, now I now know where to look for test! 馃挭

I'm looking at the fixtures: why there is something like:

<?php
...
?>
-----
<?php
...

What is the purpose of -----? How should have I use it? Should have to use it at all to provide a failing test case? Is there any resource about how to submit test cases?

```

Before

After

no, I was simply searching for in the wrong directory:

Yea, search sucks :D give the t a try ;)

The fix was very simple in the end :)

Thanks all for reporting :+1:

Was this page helpful?
0 / 5 - 0 ratings