Phpinspectionsea: References by "new self" instead of "new OwnClass"

Created on 18 Feb 2017  路  7Comments  路  Source: kalessil/phpinspectionsea

Basically, if you references the own class name instead of static or self, your should be warning. It's useful to code reuse because you don't need to change class names to new one.

class OwnClass {
    public function create() {
        // Warning: you should use "new self" here.
        return new OwnClass;
    }
}

I am really impresed because it was not suggested or implemented before. Maybe have some reason?

Edit: as quoted by @funivan, the unique option is new self, because new static should changes the code behaviour.

enhancement fixed

Most helpful comment

Also access ptoperty by OwnClass::$property

@kalessil Yes, code style. It would be cool to have inspection options (dropbown)
prefer self against class fqn
and
prefer class fqn against self

Maybe this feature can be useful for some developers.

P.S. Only if this is not so hard to implement ;)

All 7 comments

What is the difference ?
Inspection can only suggest to change to the return new self
In your case if user decide to change return new Company; to the return new static; it can change behaviour ;)

Yeah! You are totally right. I don't catch that before.

  • Code style?
  • I guess return OwnClass::CONSTANT should be covered as well?

I guess that it is a _code style_, because there not advantages to code speed.

I guess that all self class reference should be covered. For instance: new OwnClass, OwnClass::CONSTANT, OwnClass::staticMethod(), OwnClass::class (I can't remember other cases).

Also access ptoperty by OwnClass::$property

@kalessil Yes, code style. It would be cool to have inspection options (dropbown)
prefer self against class fqn
and
prefer class fqn against self

Maybe this feature can be useful for some developers.

P.S. Only if this is not so hard to implement ;)

Picking up for 2.3.9

Closing: PR #413

Was this page helpful?
0 / 5 - 0 ratings