hoaproject/Ruler

Wrong use of instanceof

julban opened this issue · 3 comments

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

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

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

Hywan commented

@Jir4 👍