hoaproject / Ruler

The Hoa\Ruler library.
https://hoa-project.net
625 stars 66 forks source link

Wrong use of instanceof #78

Closed julban closed 8 years ago

julban commented 8 years ago

Hi, I have spotted a wrong use of instanceof having anyway the expected result. Have a look at Disassembly class :

if ($element instanceof Ruler\Model) {

should fail because expected class is Ruler\Model\Model , BUT it just works. I didn't know that instanceof works with upper level in namespace. In fact it does not, it's just because last level in checked namespace matches the classname.

To test it:

        $model = new Hoa\Ruler\Model\Model();
        var_dump($model instanceof Hoa);
        var_dump($model instanceof Hoa\Ruler);
        var_dump($model instanceof Hoa\Ruler\Model);
        var_dump($model instanceof Hoa\Ruler\Model\Model);
        $operator = new Hoa\Ruler\Model\Operator('and');
        var_dump($operator instanceof Hoa);
        var_dump($operator instanceof Hoa\Ruler);
        var_dump($operator instanceof Hoa\Ruler\Model);
        var_dump($operator instanceof Hoa\Ruler\Model\Operator);

results in:

bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)

FYI, we're using PHP 5.5.9-1ubuntu4.14 . I guess that the condition I was talking about should be:

if ($element instanceof Ruler\Model\Model) {

Thanks in advance!

Jir4 commented 8 years ago

A class_alias is defined so an instance of Hoa\Ruler\Model is an instance of Hoa\Ruler\Model\Model, this behavior is here to shorten the call.

plz see : https://github.com/hoaproject/Ruler/blob/master/Model/Model.php#L200

and : https://github.com/hoaproject/Core/blob/305a881847478d635f3cf565fd5e8ef382c21a70/Consistency.php#L511-L523

julban commented 8 years ago

Ok I see, thx for the explanation! I guess the issue is resolved then :-)

Hywan commented 8 years ago

@Jir4 :+1: