Skip to content

Commit 0ac4405

Browse files
committed
bug #4915 Fix handling property PHPDocs with unsupported type (julienfalque)
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
2 parents ddaf702 + 38cb8bc commit 0ac4405

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,32 +230,25 @@ private function fixFunctionDocComment($content, Tokens $tokens, $functionIndex,
230230

231231
/**
232232
* @param string $content
233-
* @param int $docCommentIndex
233+
* @param int $index Index of the DocComment token
234234
*
235235
* @return string
236236
*/
237-
private function fixPropertyDocComment($content, Tokens $tokens, $docCommentIndex, array $shortNames)
237+
private function fixPropertyDocComment($content, Tokens $tokens, $index, array $shortNames)
238238
{
239239
$docBlock = new DocBlock($content);
240240

241-
$index = $tokens->getNextMeaningfulToken($docCommentIndex);
242-
243-
$kindsBeforeProperty = [T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC];
244-
245-
if (!$tokens[$index]->isGivenKind($kindsBeforeProperty)) {
246-
return $content;
247-
}
248-
249241
do {
250242
$index = $tokens->getNextMeaningfulToken($index);
243+
} while ($tokens[$index]->isGivenKind([T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC]));
251244

252-
$propertyTypeInfo = $this->parseTypeHint($tokens, $index);
253-
foreach ($docBlock->getAnnotationsOfType('var') as $annotation) {
254-
if ($this->annotationIsSuperfluous($annotation, $propertyTypeInfo, $shortNames)) {
255-
$annotation->remove();
256-
}
245+
$propertyTypeInfo = $this->getPropertyTypeInfo($tokens, $index);
246+
247+
foreach ($docBlock->getAnnotationsOfType('var') as $annotation) {
248+
if ($this->annotationIsSuperfluous($annotation, $propertyTypeInfo, $shortNames)) {
249+
$annotation->remove();
257250
}
258-
} while ($tokens[$index]->isGivenKind($kindsBeforeProperty));
251+
}
259252

260253
return $docBlock->getContent();
261254
}
@@ -318,6 +311,23 @@ private function getReturnTypeInfo(Tokens $tokens, $closingParenthesisIndex)
318311
];
319312
}
320313

314+
/**
315+
* @param int $index The index of the first token of the type hint
316+
*
317+
* @return array
318+
*/
319+
private function getPropertyTypeInfo(Tokens $tokens, $index)
320+
{
321+
if ($tokens[$index]->isGivenKind(T_VARIABLE)) {
322+
return [
323+
'type' => null,
324+
'allows_null' => true,
325+
];
326+
}
327+
328+
return $this->parseTypeHint($tokens, $index);
329+
}
330+
321331
/**
322332
* @param int $index The index of the first token of the type hint
323333
*

tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,25 @@ class Foo {
406406
* {@inheritdocs}
407407
*/
408408
private $foo;
409+
}',
410+
],
411+
'property with unsupported type' => [
412+
'<?php
413+
class Foo {
414+
/**
415+
* @var foo:bar
416+
*/
417+
private $foo;
418+
}',
419+
],
420+
'method with unsupported types' => [
421+
'<?php
422+
class Foo {
423+
/**
424+
* @param foo:bar $foo
425+
* @return foo:bar
426+
*/
427+
public function foo($foo) {}
409428
}',
410429
],
411430
];

0 commit comments

Comments
 (0)