Skip to content

Commit

Permalink
bug #4915 Fix handling property PHPDocs with unsupported type (julien…
Browse files Browse the repository at this point in the history
…falque)

This PR was merged into the 2.15 branch.

Discussion
----------

Fix handling property PHPDocs with unsupported type

Fixes #4913.

Commits
-------

38cb8bc Fix handling property PHPDocs with unsupported type
  • Loading branch information
julienfalque committed Apr 15, 2020
2 parents ddaf702 + 38cb8bc commit 0ac4405
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,32 +230,25 @@ private function fixFunctionDocComment($content, Tokens $tokens, $functionIndex,

/**
* @param string $content
* @param int $docCommentIndex
* @param int $index Index of the DocComment token
*
* @return string
*/
private function fixPropertyDocComment($content, Tokens $tokens, $docCommentIndex, array $shortNames)
private function fixPropertyDocComment($content, Tokens $tokens, $index, array $shortNames)
{
$docBlock = new DocBlock($content);

$index = $tokens->getNextMeaningfulToken($docCommentIndex);

$kindsBeforeProperty = [T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC];

if (!$tokens[$index]->isGivenKind($kindsBeforeProperty)) {
return $content;
}

do {
$index = $tokens->getNextMeaningfulToken($index);
} while ($tokens[$index]->isGivenKind([T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC]));

$propertyTypeInfo = $this->parseTypeHint($tokens, $index);
foreach ($docBlock->getAnnotationsOfType('var') as $annotation) {
if ($this->annotationIsSuperfluous($annotation, $propertyTypeInfo, $shortNames)) {
$annotation->remove();
}
$propertyTypeInfo = $this->getPropertyTypeInfo($tokens, $index);

foreach ($docBlock->getAnnotationsOfType('var') as $annotation) {
if ($this->annotationIsSuperfluous($annotation, $propertyTypeInfo, $shortNames)) {
$annotation->remove();
}
} while ($tokens[$index]->isGivenKind($kindsBeforeProperty));
}

return $docBlock->getContent();
}
Expand Down Expand Up @@ -318,6 +311,23 @@ private function getReturnTypeInfo(Tokens $tokens, $closingParenthesisIndex)
];
}

/**
* @param int $index The index of the first token of the type hint
*
* @return array
*/
private function getPropertyTypeInfo(Tokens $tokens, $index)
{
if ($tokens[$index]->isGivenKind(T_VARIABLE)) {
return [
'type' => null,
'allows_null' => true,
];
}

return $this->parseTypeHint($tokens, $index);
}

/**
* @param int $index The index of the first token of the type hint
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,25 @@ class Foo {
* {@inheritdocs}
*/
private $foo;
}',
],
'property with unsupported type' => [
'<?php
class Foo {
/**
* @var foo:bar
*/
private $foo;
}',
],
'method with unsupported types' => [
'<?php
class Foo {
/**
* @param foo:bar $foo
* @return foo:bar
*/
public function foo($foo) {}
}',
],
];
Expand Down

0 comments on commit 0ac4405

Please sign in to comment.