aik099 / CodingStandard

The PHP_CodeSniffer coding standard I'm using on all of my projects
BSD 3-Clause "New" or "Revised" License
5 stars 2 forks source link

Validate "@var" comment #77

Closed aik099 closed 8 years ago

aik099 commented 9 years ago

The correct @var comment looks like this:

/** @var ClassName $variable_name */
$variable_name = new ClassName();

Several things can be wrong here:

$variable_name = new ClassName();
/** @var ClassName $variable_name */
/** @var $variable_name ClassName */
$variable_name = new ClassName();
/* @var $variable_name ClassName */
$variable_name = new ClassName();
/** @var $another_variable_name ClassName */
$variable_name = new ClassName();

All of above can be combined.

Tricky case No. 1:

$supply_order =& $this->Application->recallObject(
    'supply-order.-item',
    null,
    array('skip_autoload' => true)
);
/** @var kDBItem $supply_order */

When we find ; on the above line, then we need to traverse up to statement start to figure out where to move the comment to.

aik099 commented 8 years ago

To find matching variable:

  1. find next T_VARIABLE (excluding T_WHITESPACE) that name marches @var comment
  2. if not found or name doesn't match find previous T_VARIABLE (excluding T_WHITESPACE) that name marches @var comment
  3. if not found, then we have no way of detecting if comment is placed correctly - ignore
aik099 commented 8 years ago

variable name in comment doesn't match variable name in assignment on next line (if any found):

All except above supports auto-fixing.