Phpinspectionsea: Suggest to use class constants instead of static properties

Created on 1 Mar 2017  路  7Comments  路  Source: kalessil/phpinspectionsea

Source: @Fleshgrinder

We could check the configured PHP version for the project and recommend the usage of a [public] const instead of a public static.

In some cases we can convert class static property to the class constant.

For example we have following code:

class User{
 public static $statuses = [
   'a'=>'Active',
   'd'=>'Deleted',
 ];
}

Inspection can handle property $statuses and suggest to use constant:

 class User{
    const STATUSES = [
      'a'=>'Active',
      'd'=>'Deleted',
    ];
  }

This inspection can be implemented in different ways:

  1. Detect static property in class and suggest to use Constant
  2. Check if all references to the property are Read Only. (No writes)

I prefer 1 case ;)

enhancement wontfix

Most helpful comment

On PHP 7.1, it could checks for privates and protecteds too.

All 7 comments

On PHP 7.1, it could checks for privates and protecteds too.

I prefer case 2, there is nothing wrong about writing to static properties.

@Fleshgrinder static properties change global state. And global state is always a bad way.
2 case can be implemented only if we have private property. With public properties it will be hard to scan whole project and find writes.

Obviously I was thinking of a max. visibility of protected since having a public property (other than a DTO) is always a huge smell in my book; regardless of whether it is static or not. 馃槈

Bebo beep, the StaleBot is here. For one year nothing have happened here. It would be great if someone looked into details here within next 21 days when I'll close it.

Bebo beep, the StaleBot is here. For one year nothing have happened here. It would be great if someone looked into details here within next 21 days when I'll close it.

That'd be a complete semantics change, so I'll keep this closed. @funivan: feel free to take the issue over for your plugin.

Was this page helpful?
0 / 5 - 0 ratings