kalessil / yii2inspections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[fixed, bug] “Missing @property annotations” inspection is ignoring getters

brandonkelly opened this issue · comments

Example:

<?php
namespace craft;

use yii\base\Object;

/**
 * Class Foo
 */
class Foo extends Object
{
    private $_bar = 'bar';

    /**
     * @return string
     */
    public function getBar()
    {
        return $this->_bar;
    }
}

The “Missing @Property annotations” inspection should be saying this class is missing a $bar property, but it doesn’t. It appears to only be checking for setters.

If you add this method:

    /**
     * @param string $bar
     */
    public function setBar(string $bar)
    {
        $this->_bar = $bar;
    }

the inspection will work as expected.

Then perhaps we should change the behaviour:

  • if getProperty presented and $_property exists -> analyze
  • if setProperty presented and $_property exists -> analyze
    What do you think about this approach?

The current implementation requires complimentary getProperty and setProperty existence.
The existence of $_property is not checked.

I wouldn't worry about whether a private $_property exists at all.

These are the checks it should have:

  • class is an instance of yii\base\Object
  • class has ONE or BOTH of these:
    • a method whose name passes /^get[A-Z]/ with 0 required arguments (it may have arguments if they have a default value, e.g. getFoo($bar = 'bar'))
    • a method whose name passes /^set[A-Z]/ with 1 required argument (it may have additional arguments if they have default values, e.g. setFoo($foo, $bar = 'bar'))

For determining the type, it should be a combination of all the unique types from:

  • the getter’s @return type
  • the setter’s first @param type

For example:

<?php

namespace craft;

use yii\base\Object;

/**
 * Class Foo
 */
class Foo extends Object
{
    private $_bar;

    /**
     * @return string|null
     */
    public function getBar()
    {
        return $this->_bar;
    }

    /**
     * @param string $bar
     */
    public function setBar(string $bar)
    {
        $this->_bar = $bar;
    }
}

should get this @Property annotation:

@property string|null $type

Oh, I see (ONE or BOTH), will fix and extend test cases

Not sure if this warrants a new issue, but in that example above, clicking the “Annotate properties” button adds this:

 * @property \null|\string $bar

Those \s should not be in there.

@property \null|\string $bar : fixed, working on the ONE or BOTH approach in the inspection

Now checks ONE or BOTH, with settings for using only BOTH approach.