Automattic / phpcs-neutron-standard

A set of phpcs sniffs for PHP >7 development
MIT License
94 stars 7 forks source link

Class static properties fail linting #73

Closed beaucollins closed 5 years ago

beaucollins commented 5 years ago

The $schema variable is defined as a static property on a PHP class:

/**
 * Day Settings Model class
 */
class Day_Settings extends Model {
    /**
     * Table schema for day settings.
     *
     * @var array $schema
     */
    public static $schema = array(
        'table'       => 'hs_day_settings',
        'entity'      => 'day settings',
        'primary_key' => 'hs_day_settings_id',
        'fields'      => array(
            'hs_day_settings_id' => 'int',
            'user_id'            => 'int',
            'date'               => 'date',
            'settings'           => 'json',
            'organization_id'    => 'int'
        ),
    );

The linter sees it as an undefined variable.

   Error  (PHPCS-CHANGEDVariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable) Unknown lint message!
    Variable $schema is undefined.

              52     */
              53    public function get_primary_id() {
              54        $called_class = get_called_class();
    >>>       55        $field = $called_class::$schema['primary_key'];
              56        return $this->$field;
              57    }
              58 

Is there a preferred way to access the $schema static property on a class that the linter allows?

sirbrillig commented 5 years ago

Good find! This is a bug in phpcs-variable-analysis. Moved to https://github.com/sirbrillig/phpcs-variable-analysis/issues/62