Skip to content

Commit 273c744

Browse files
committed
Merge branch '2.15' into 2.16
* 2.15: NoUnusedImportsFixer - do not match variable name as import SingleTraitInsertPerStatement - fix formatting for multiline \"use\" Remove superfluous phpdocs for typed properties (PHP 7.4) Fix merge duplicate row # Conflicts: # src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php # tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php
2 parents ba4381d + 0ca79a2 commit 273c744

File tree

8 files changed

+436
-17
lines changed

8 files changed

+436
-17
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,8 @@ Choose from the list of available rules:
11501150

11511151
* **no_superfluous_phpdoc_tags** [@Symfony, @PhpCsFixer]
11521152

1153-
Removes ``@param`` and ``@return`` tags that don't provide any useful
1154-
information.
1153+
Removes ``@param``, ``@return`` and ``@var`` tags that don't provide any
1154+
useful information.
11551155

11561156
Configuration options:
11571157

src/Fixer/ClassNotation/SingleTraitInsertPerStatementFixer.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpCsFixer\AbstractFixer;
1616
use PhpCsFixer\FixerDefinition\CodeSample;
1717
use PhpCsFixer\FixerDefinition\FixerDefinition;
18+
use PhpCsFixer\Preg;
1819
use PhpCsFixer\Tokenizer\CT;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
@@ -58,24 +59,35 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5859
if ($tokens[$index]->isGivenKind(CT::T_USE_TRAIT)) {
5960
$candidates = $this->getCandidates($tokens, $index);
6061
if (\count($candidates) > 0) {
61-
$this->fixTraitUse($tokens, array_reverse($candidates));
62+
$this->fixTraitUse($tokens, $index, $candidates);
6263
}
6364
}
6465
}
6566
}
6667

6768
/**
68-
* @param int[] $candidates ',' indexes to fix
69+
* @param int $useTraitIndex
70+
* @param int[] $candidates ',' indexes to fix
6971
*/
70-
private function fixTraitUse(Tokens $tokens, array $candidates)
72+
private function fixTraitUse(Tokens $tokens, $useTraitIndex, array $candidates)
7173
{
72-
foreach ($candidates as $nextInsertIndex) {
73-
$tokens[$nextInsertIndex] = new Token(';');
74-
$tokens->insertAt($nextInsertIndex + 1, new Token([CT::T_USE_TRAIT, 'use']));
74+
foreach ($candidates as $commaIndex) {
75+
$inserts = [
76+
new Token([CT::T_USE_TRAIT, 'use']),
77+
new Token([T_WHITESPACE, ' ']),
78+
];
7579

76-
if (!$tokens[$nextInsertIndex + 2]->isWhitespace()) {
77-
$tokens->insertAt($nextInsertIndex + 2, new Token([T_WHITESPACE, ' ']));
80+
$nextImportStartIndex = $tokens->getNextMeaningfulToken($commaIndex);
81+
82+
if ($tokens[$nextImportStartIndex - 1]->isWhitespace()) {
83+
if (1 === Preg::match('/\R/', $tokens[$nextImportStartIndex - 1]->getContent())) {
84+
array_unshift($inserts, clone $tokens[$useTraitIndex - 1]);
85+
}
86+
$tokens->clearAt($nextImportStartIndex - 1);
7887
}
88+
89+
$tokens[$commaIndex] = new Token(';');
90+
$tokens->insertAt($nextImportStartIndex, $inserts);
7991
}
8092
}
8193

@@ -98,6 +110,6 @@ private function getCandidates(Tokens $tokens, $index)
98110
$index = $tokens->getNextTokenOfKind($index, [',', ';', '{']);
99111
}
100112

101-
return $indexes;
113+
return array_reverse($indexes);
102114
}
103115
}

src/Fixer/Import/NoUnusedImportsFixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra
153153

154154
if ($token->isComment()
155155
&& Preg::match(
156-
'/(?<![[:alnum:]])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
156+
'/(?<![[:alnum:]\$])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
157157
$token->getContent()
158158
)
159159
) {

src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class NoSuperfluousPhpdocTagsFixer extends AbstractFixer implements Config
3636
public function getDefinition()
3737
{
3838
return new FixerDefinition(
39-
'Removes `@param` and `@return` tags that don\'t provide any useful information.',
39+
'Removes `@param`, `@return` and `@var` tags that don\'t provide any useful information.',
4040
[
4141
new CodeSample('<?php
4242
class Foo {
@@ -136,6 +136,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
136136

137137
if ($token->isGivenKind(T_FUNCTION)) {
138138
$content = $this->fixFunctionDocComment($content, $tokens, $index, $shortNames);
139+
} elseif ($token->isGivenKind(T_VARIABLE)) {
140+
$content = $this->fixPropertyDocComment($content, $tokens, $index, $shortNames);
139141
}
140142

141143
if ($this->configuration['remove_inheritdoc']) {
@@ -188,7 +190,7 @@ private function findDocumentedElement(Tokens $tokens, $docCommentIndex)
188190

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

191-
$kindsBeforeProperty = [T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC];
193+
$kindsBeforeProperty = [T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, T_STRING, T_NS_SEPARATOR];
192194

193195
if (!$tokens[$index]->isGivenKind($kindsBeforeProperty)) {
194196
return null;
@@ -251,6 +253,38 @@ private function fixFunctionDocComment($content, Tokens $tokens, $functionIndex,
251253
return $docBlock->getContent();
252254
}
253255

256+
/**
257+
* @param string $content
258+
* @param int $docCommentIndex
259+
*
260+
* @return string
261+
*/
262+
private function fixPropertyDocComment($content, Tokens $tokens, $docCommentIndex, array $shortNames)
263+
{
264+
$docBlock = new DocBlock($content);
265+
266+
$index = $tokens->getNextMeaningfulToken($docCommentIndex);
267+
268+
$kindsBeforeProperty = [T_STATIC, T_PRIVATE, T_PROTECTED, T_PUBLIC];
269+
270+
if (!$tokens[$index]->isGivenKind($kindsBeforeProperty)) {
271+
return $content;
272+
}
273+
274+
do {
275+
$index = $tokens->getNextMeaningfulToken($index);
276+
277+
$propertyTypeInfo = $this->parseTypeHint($tokens, $index);
278+
foreach ($docBlock->getAnnotationsOfType('var') as $annotation) {
279+
if ($this->annotationIsSuperfluous($annotation, $propertyTypeInfo, $shortNames)) {
280+
$annotation->remove();
281+
}
282+
}
283+
} while ($tokens[$index]->isGivenKind($kindsBeforeProperty));
284+
285+
return $docBlock->getContent();
286+
}
287+
254288
/**
255289
* @param int $start
256290
* @param int $end
@@ -330,7 +364,7 @@ private function parseTypeHint(Tokens $tokens, $index)
330364
}
331365

332366
return [
333-
'type' => $type,
367+
'type' => '' === $type ? null : $type,
334368
'allows_null' => $allowsNull,
335369
];
336370
}
@@ -344,6 +378,8 @@ private function annotationIsSuperfluous(Annotation $annotation, array $info, ar
344378
{
345379
if ('param' === $annotation->getTag()->getName()) {
346380
$regex = '/@param\s+(?:\S|\s(?!\$))+\s\$\S+\s+\S/';
381+
} elseif ('var' === $annotation->getTag()->getName()) {
382+
$regex = '/@var\s+\S+(\s+\$\S+)?(\s+)([^$\s]+)/';
347383
} else {
348384
$regex = '/@return\s+\S+\s+\S/';
349385
}

tests/AutoReview/FixerFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ static function (array $priorityPair1, array $priorityPair2) {
491491
}
492492

493493
static::assertSame($sortedDescription, $casesDescription);
494+
} else {
495+
$this->addToAssertionCount(1);
494496
}
495497
}
496498

tests/Fixer/ClassNotation/SingleTraitInsertPerStatementFixerTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ final class Example
8181
{
8282
use Foo, Bar ;
8383
}
84+
',
85+
],
86+
'simple III' => [
87+
'<?php
88+
class Example
89+
{
90+
use Foo;use Bar;
91+
92+
public function baz() {}
93+
}
94+
',
95+
'<?php
96+
class Example
97+
{
98+
use Foo, Bar;
99+
100+
public function baz() {}
101+
}
84102
',
85103
],
86104
'multiple' => [
@@ -101,6 +119,44 @@ final class Example
101119
use Foo10, Bar11, Bar110;
102120
use Foo20, Bar20, Bar200, Bar201;
103121
}
122+
',
123+
],
124+
'multiple_multiline' => [
125+
'<?php
126+
final class Example
127+
{
128+
use Foo;
129+
use Bar;
130+
use Baz;
131+
}
132+
',
133+
'<?php
134+
final class Example
135+
{
136+
use Foo,
137+
Bar,
138+
Baz;
139+
}
140+
',
141+
],
142+
'multiple_multiline_with_comment' => [
143+
'<?php
144+
final class Example
145+
{
146+
use Foo;
147+
use Bar;
148+
// Bazz,
149+
use Baz;
150+
}
151+
',
152+
'<?php
153+
final class Example
154+
{
155+
use Foo,
156+
Bar,
157+
// Bazz,
158+
Baz;
159+
}
104160
',
105161
],
106162
'namespaces' => [
@@ -126,9 +182,9 @@ class ZZ
126182
use#2
127183
Z/* 2 */ #3
128184
#4
129-
;use #5
185+
;#5
130186
#6
131-
T#7
187+
use T#7
132188
#8
133189
;#9
134190
#10

tests/Fixer/Import/NoUnusedImportsFixerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,31 @@ class Baz
10871087
var_dump($y);}
10881088
EOF
10891089
],
1090+
[
1091+
'<?php
1092+
use App\Http\Requests\StoreRequest;
1093+
1094+
class StoreController
1095+
{
1096+
/**
1097+
* @param \App\Http\Requests\StoreRequest $request
1098+
*/
1099+
public function __invoke(StoreRequest $request)
1100+
{}
1101+
}',
1102+
'<?php
1103+
use App\Http\Requests\StoreRequest;
1104+
use Illuminate\Http\Request;
1105+
1106+
class StoreController
1107+
{
1108+
/**
1109+
* @param \App\Http\Requests\StoreRequest $request
1110+
*/
1111+
public function __invoke(StoreRequest $request)
1112+
{}
1113+
}',
1114+
],
10901115
];
10911116
}
10921117

0 commit comments

Comments
 (0)