Skip to content

Commit

Permalink
Fix handling property PHPDocs with unsupported type
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed Apr 12, 2020
1 parent 13492d1 commit 38cb8bc
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 38cb8bc

Please sign in to comment.