Phpinspectionsea: Repair on "::class can be used" does not detect collision with current class

Created on 26 Jan 2018  路  2Comments  路  Source: kalessil/phpinspectionsea

Steps

Having two classes:

src/Abc/MyClass.php:

<?php

namespace OC\Abc;

class MyClass {
    public function abc() {
        echo '\OC\Abcd\MyClass';
    }
}

src/Abcd/MyClass.php:

<?php

namespace OC\Abcd;

class MyClass {
}

The inspection ::class can be used is shown:

bildschirmfoto 2018-01-26 um 12 13 59

Problem

Clicking the repair button now results in:

<?php

namespace OC\Abc;

use OC\Abcd\MyClass;

class MyClass {
    public function abc() {
        echo MyClass::class;
    }
}

This is obviously wrong, because MyClass is already defined. It seems that it does not detect collisions with the class it is currently repairing.

Possible solution no. 1

The code needs to be either the full name:

<?php

namespace OC\Abc;

class MyClass {
    public function abc() {
        echo \OC\Abcd\MyClass::class;
    }
}

Possible solution no. 2

or aliased:

<?php

namespace OC\Abc;

use OC\Abcd\MyClass as AbcdMyClass;

class MyClass {
    public function abc() {
        echo AbcdMyClass::class;
    }
}
bug / false-positive fixed

Most helpful comment

Fixed

All 2 comments

Thank you for reporting @MorrisJobke, we'll follow the Possible solution no. 1 strategy here.

Fixed

Was this page helpful?
0 / 5 - 0 ratings