Skip to content

Commit

Permalink
PhpdocNoIncorrectVarAnnotationFixer - handle class properties when va…
Browse files Browse the repository at this point in the history
…riable names are different and constants with visibility (#487)
  • Loading branch information
kubawerlos committed Jul 20, 2021
1 parent 34a49bf commit 96f12d0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## v2.6.0
- Add StringableInterfaceFixer
- NoDuplicatedArrayKeyFixer - add option "ignore_expressions"
- PhpdocNoIncorrectVarAnnotationFixer - handle class properties when variable names are different and constants with visibility

## v2.5.0
- Add PHP CS Fixer v3 support
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-2831-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-2833-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
Expand Down
26 changes: 26 additions & 0 deletions src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php
Expand Up @@ -77,6 +77,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$nextToken = $tokens[$nextIndex];

if ($nextToken->isGivenKind([\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_VAR, \T_STATIC])) {
$this->removeForClassElement($tokens, $index);
continue;
}

Expand All @@ -99,6 +100,31 @@ private function isTokenCandidate(Token $token): bool
return $token->isGivenKind(\T_DOC_COMMENT) && \stripos($token->getContent(), '@var') !== false;
}

private function removeForClassElement(Tokens $tokens, int $index): void
{
/** @var int $nextIndex */
$nextIndex = $tokens->getTokenNotOfKindsSibling($index, 1, [\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_VAR, \T_STATIC, \T_WHITESPACE]);

/** @var Token $nextToken */
$nextToken = $tokens[$nextIndex];

if ($nextToken->isGivenKind(\T_CONST)) {
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, null);

return;
}

if ($nextToken->isGivenKind(\T_VARIABLE)) {
/** @var Token $token */
$token = $tokens[$index];

if (Preg::match('/ \$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $token->getContent()) === 1) {
// \$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff] */
$this->removeVarAnnotation($tokens, $index, [$nextToken->getContent()]);
}
}
}

/**
* @param string[] $allowedVariables
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php
Expand Up @@ -329,5 +329,49 @@ public function hello()
/** @var this is incorrect */
',
];

yield 'remove PHPDoc for class properties when variable names are different' => [
'<?php class Foo {
static $a;
public $b;
protected $c;
private $d;
var $e;
private static $f;
}',
'<?php class Foo {
/** @var int $x */
static $a;
/** @var int $x */
public $b;
/** @var int $x */
protected $c;
/** @var int $x */
private $d;
/** @var int $x */
var $e;
/** @var int $x */
private static $f;
}',
];

yield 'remove PHPDoc for constants' => [
'<?php class Foo {
const A = 1;
public const B = 2;
protected const C = 3;
private const D = 4;
}',
'<?php class Foo {
/** @var int */
const A = 1;
/** @var int */
public const B = 2;
/** @var int */
protected const C = 3;
/** @var int */
private const D = 4;
}',
];
}
}

0 comments on commit 96f12d0

Please sign in to comment.