Skip to content

Commit 8e06c72

Browse files
committed
Merge branch '2.14' into 2.15
* 2.14: fix FileLintingIterator current value on end/invalid MethodArgumentSpaceFixer - handle misplaced ) fix conflicts MethodArgumentSpaceFixer - fix for on_multiline:ensure_fully_multiline with trailing comma in function call DX: test to ensure @PHPUnitMigration rule sets are correctly defined Fix non-static closure unbinding this on PHP 7.4 FunctionTypehintSpaceFixer - Ensure single space between type declaration and parameter Test ReadmeCommand with CommandTester fix access to reference without checking existence DX: static call of markTestSkippedOrFail # Conflicts: # tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php
2 parents 63ac2c6 + db00b77 commit 8e06c72

18 files changed

+374
-62
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ Choose from the list of available rules:
714714

715715
* **function_typehint_space** [@Symfony, @PhpCsFixer]
716716

717-
Add missing space between function's argument and its typehint.
717+
Ensure single space between function's argument and its typehint.
718718

719719
* **general_phpdoc_annotation_remove**
720720

src/Fixer/ControlStructure/NoBreakCommentFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ protected function createConfigurationDefinition()
7474
(new FixerOptionBuilder('comment_text', 'The text to use in the added comment and to detect it.'))
7575
->setAllowedTypes(['string'])
7676
->setAllowedValues([
77-
function ($value) {
77+
static function ($value) {
7878
if (\is_string($value) && Preg::match('/\R/', $value)) {
7979
throw new InvalidOptionsException('The comment text must not contain new lines.');
8080
}
8181

8282
return true;
8383
},
8484
])
85-
->setNormalizer(function (Options $options, $value) {
85+
->setNormalizer(static function (Options $options, $value) {
8686
return rtrim($value);
8787
})
8888
->setDefault('no break')

src/Fixer/FunctionNotation/FunctionTypehintSpaceFixer.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PhpCsFixer\AbstractFixer;
1616
use PhpCsFixer\FixerDefinition\CodeSample;
1717
use PhpCsFixer\FixerDefinition\FixerDefinition;
18+
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
19+
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
1820
use PhpCsFixer\Tokenizer\Token;
1921
use PhpCsFixer\Tokenizer\Tokens;
2022

@@ -29,8 +31,11 @@ final class FunctionTypehintSpaceFixer extends AbstractFixer
2931
public function getDefinition()
3032
{
3133
return new FixerDefinition(
32-
'Add missing space between function\'s argument and its typehint.',
33-
[new CodeSample("<?php\nfunction sample(array\$a)\n{}\n")]
34+
'Ensure single space between function\'s argument and its typehint.',
35+
[
36+
new CodeSample("<?php\nfunction sample(array\$a)\n{}\n"),
37+
new CodeSample("<?php\nfunction sample(array \$a)\n{}\n"),
38+
]
3439
);
3540
}
3641

@@ -47,40 +52,35 @@ public function isCandidate(Tokens $tokens)
4752
*/
4853
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
4954
{
55+
$functionsAnalyzer = new FunctionsAnalyzer();
56+
5057
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
5158
$token = $tokens[$index];
5259

5360
if (!$token->isGivenKind(T_FUNCTION)) {
5461
continue;
5562
}
5663

57-
$startParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(', ';', [T_CLOSE_TAG]]);
58-
if (!$tokens[$startParenthesisIndex]->equals('(')) {
59-
continue;
60-
}
64+
$arguments = $functionsAnalyzer->getFunctionArguments($tokens, $index);
6165

62-
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
66+
foreach (array_reverse($arguments) as $argument) {
67+
$type = $argument->getTypeAnalysis();
6368

64-
for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) {
65-
if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) {
69+
if (!$type instanceof TypeAnalysis) {
6670
continue;
6771
}
6872

69-
// skip ... before $variable for variadic parameter
70-
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
71-
if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) {
72-
$iter = $prevNonWhitespaceIndex;
73-
}
73+
$whitespaceTokenIndex = $type->getEndIndex() + 1;
7474

75-
// skip & before $variable for parameter passed by reference
76-
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
77-
if ($tokens[$prevNonWhitespaceIndex]->equals('&')) {
78-
$iter = $prevNonWhitespaceIndex;
79-
}
75+
if ($tokens[$whitespaceTokenIndex]->equals([T_WHITESPACE])) {
76+
if (' ' === $tokens[$whitespaceTokenIndex]->getContent()) {
77+
continue;
78+
}
8079

81-
if (!$tokens[$iter - 1]->equalsAny([[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], '(', ','])) {
82-
$tokens->insertAt($iter, new Token([T_WHITESPACE, ' ']));
80+
$tokens->clearAt($whitespaceTokenIndex);
8381
}
82+
83+
$tokens->insertAt($whitespaceTokenIndex, new Token([T_WHITESPACE, ' ']));
8484
}
8585
}
8686
}

src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,13 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionInde
357357
$indentation = $existingIndentation.$this->whitespacesConfig->getIndent();
358358
$endFunctionIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startFunctionIndex);
359359

360-
if (!$this->isNewline($tokens[$endFunctionIndex - 1])) {
361-
$tokens->ensureWhitespaceAtIndex(
362-
$endFunctionIndex,
363-
0,
364-
$this->whitespacesConfig->getLineEnding().$existingIndentation
365-
);
360+
$wasWhitespaceBeforeEndFunctionAddedAsNewToken = $tokens->ensureWhitespaceAtIndex(
361+
$tokens[$endFunctionIndex - 1]->isWhitespace() ? $endFunctionIndex - 1 : $endFunctionIndex,
362+
0,
363+
$this->whitespacesConfig->getLineEnding().$existingIndentation
364+
);
366365

366+
if ($wasWhitespaceBeforeEndFunctionAddedAsNewToken) {
367367
++$endFunctionIndex;
368368
}
369369

@@ -421,6 +421,11 @@ private function fixNewline(Tokens $tokens, $index, $indentation, $override = tr
421421
return;
422422
}
423423

424+
$nextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($index);
425+
if ($tokens[$nextMeaningfulTokenIndex]->equals(')')) {
426+
return;
427+
}
428+
424429
$tokens->ensureWhitespaceAtIndex($index + 1, 0, $this->whitespacesConfig->getLineEnding().$indentation);
425430
}
426431

src/Runner/FileLintingIterator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class FileLintingIterator extends \IteratorIterator
2828
private $currentResult;
2929

3030
/**
31-
* @var LinterInterface
31+
* @var null|LinterInterface
3232
*/
3333
private $linter;
3434

@@ -39,6 +39,9 @@ public function __construct(\Iterator $iterator, LinterInterface $linter)
3939
$this->linter = $linter;
4040
}
4141

42+
/**
43+
* @return null|LinterInterface
44+
*/
4245
public function currentLintingResult()
4346
{
4447
return $this->currentResult;
@@ -48,18 +51,14 @@ public function next()
4851
{
4952
parent::next();
5053

51-
if ($this->valid()) {
52-
$this->currentResult = $this->handleItem($this->current());
53-
}
54+
$this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
5455
}
5556

5657
public function rewind()
5758
{
5859
parent::rewind();
5960

60-
if ($this->valid()) {
61-
$this->currentResult = $this->handleItem($this->current());
62-
}
61+
$this->currentResult = $this->valid() ? $this->handleItem($this->current()) : null;
6362
}
6463

6564
private function handleItem(\SplFileInfo $file)

src/ToolInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getComposerVersion()
6464

6565
$versionSuffix = '';
6666

67-
if (isset($package['dist'])) {
67+
if (isset($package['dist']['reference'])) {
6868
$versionSuffix = '#'.$package['dist']['reference'];
6969
}
7070

tests/AutoReview/ProjectCodeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ final class ProjectCodeTest extends TestCase
4848
\PhpCsFixer\Fixer\Operator\AlignEqualsFixerHelper::class,
4949
\PhpCsFixer\Fixer\Whitespace\NoExtraConsecutiveBlankLinesFixer::class,
5050
\PhpCsFixer\Runner\FileCachingLintingIterator::class,
51-
\PhpCsFixer\Runner\FileLintingIterator::class,
5251
\PhpCsFixer\Test\AccessibleObject::class,
5352
];
5453

tests/Console/Command/ReadmeCommandTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
use PhpCsFixer\Console\Application;
1616
use PhpCsFixer\Tests\TestCase;
17-
use Symfony\Component\Console\Input\ArrayInput;
18-
use Symfony\Component\Console\Output\BufferedOutput;
19-
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Tester\CommandTester;
2018

2119
/**
2220
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
@@ -36,15 +34,12 @@ public function testIfReadmeFileIsCorrect()
3634
$fileContent = file_get_contents($readmeFile);
3735
static::assertInternalType('string', $fileContent, sprintf('Failed to get content of "%s"', $readmeFile));
3836

39-
$app = new Application();
40-
$input = new ArrayInput(['readme']);
37+
$application = new Application();
4138

42-
$output = new BufferedOutput();
43-
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
44-
$output->setDecorated(false);
39+
$commandTester = new CommandTester($application->get('readme'));
4540

46-
$exitCode = $app->get('readme')->run($input, $output);
47-
$output = $output->fetch();
41+
$exitCode = $commandTester->execute([]);
42+
$output = $commandTester->getDisplay();
4843
// normalize line breaks, these are not important for the tests
4944
$output = str_replace(PHP_EOL, "\n", $output);
5045

tests/Fixer/FunctionNotation/FunctionTypehintSpaceFixerTest.php

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,58 +52,124 @@ public function provideFixCases()
5252
[
5353
'<?php function foo(/**int*/$param) {}',
5454
],
55+
[
56+
'<?php function foo(bool /**bla bla*/ $param) {}',
57+
],
58+
[
59+
'<?php function foo(bool /**bla bla*/$param) {}',
60+
'<?php function foo(bool/**bla bla*/$param) {}',
61+
],
62+
[
63+
'<?php function foo(bool /**bla bla*/$param) {}',
64+
'<?php function foo(bool /**bla bla*/$param) {}',
65+
],
5566
[
5667
'<?php function foo(callable $param) {}',
5768
'<?php function foo(callable$param) {}',
5869
],
70+
[
71+
'<?php function foo(callable $param) {}',
72+
'<?php function foo(callable $param) {}',
73+
],
5974
[
6075
'<?php function foo(array &$param) {}',
6176
'<?php function foo(array&$param) {}',
6277
],
78+
[
79+
'<?php function foo(array &$param) {}',
80+
'<?php function foo(array &$param) {}',
81+
],
6382
[
6483
'<?php function foo(array & $param) {}',
6584
'<?php function foo(array& $param) {}',
6685
],
86+
[
87+
'<?php function foo(array & $param) {}',
88+
'<?php function foo(array & $param) {}',
89+
],
6790
[
6891
'<?php function foo(Bar $param) {}',
6992
'<?php function foo(Bar$param) {}',
7093
],
94+
[
95+
'<?php function foo(Bar $param) {}',
96+
'<?php function foo(Bar $param) {}',
97+
],
7198
[
7299
'<?php function foo(Bar\Baz $param) {}',
73100
'<?php function foo(Bar\Baz$param) {}',
74101
],
102+
[
103+
'<?php function foo(Bar\Baz $param) {}',
104+
'<?php function foo(Bar\Baz $param) {}',
105+
],
75106
[
76107
'<?php function foo(Bar\Baz &$param) {}',
77108
'<?php function foo(Bar\Baz&$param) {}',
78109
],
110+
[
111+
'<?php function foo(Bar\Baz &$param) {}',
112+
'<?php function foo(Bar\Baz &$param) {}',
113+
],
79114
[
80115
'<?php function foo(Bar\Baz & $param) {}',
81116
'<?php function foo(Bar\Baz& $param) {}',
82117
],
118+
[
119+
'<?php function foo(Bar\Baz & $param) {}',
120+
'<?php function foo(Bar\Baz & $param) {}',
121+
],
83122
[
84123
'<?php $foo = function(Bar\Baz $param) {};',
85124
'<?php $foo = function(Bar\Baz$param) {};',
86125
],
126+
[
127+
'<?php $foo = function(Bar\Baz $param) {};',
128+
'<?php $foo = function(Bar\Baz $param) {};',
129+
],
87130
[
88131
'<?php $foo = function(Bar\Baz &$param) {};',
89132
'<?php $foo = function(Bar\Baz&$param) {};',
90133
],
134+
[
135+
'<?php $foo = function(Bar\Baz &$param) {};',
136+
'<?php $foo = function(Bar\Baz &$param) {};',
137+
],
91138
[
92139
'<?php $foo = function(Bar\Baz & $param) {};',
93140
'<?php $foo = function(Bar\Baz& $param) {};',
94141
],
142+
[
143+
'<?php $foo = function(Bar\Baz & $param) {};',
144+
'<?php $foo = function(Bar\Baz & $param) {};',
145+
],
95146
[
96147
'<?php class Test { public function foo(Bar\Baz $param) {} }',
97148
'<?php class Test { public function foo(Bar\Baz$param) {} }',
98149
],
150+
[
151+
'<?php class Test { public function foo(Bar\Baz $param) {} }',
152+
'<?php class Test { public function foo(Bar\Baz $param) {} }',
153+
],
99154
[
100155
'<?php $foo = function(array $a,
101-
array $b, array $c, array
102-
$d) {};',
156+
array $b, array $c, array $d) {};',
103157
'<?php $foo = function(array $a,
104158
array$b, array $c, array
105159
$d) {};',
106160
],
161+
[
162+
'<?php $foo = function(
163+
array $a,
164+
$b
165+
) {};',
166+
],
167+
[
168+
'<?php $foo = function(
169+
$a,
170+
array $b
171+
) {};',
172+
],
107173
[
108174
'<?php function foo(...$param) {}',
109175
],

tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,25 @@ function foo(
878878
'keep_multiple_spaces_after_comma' => true,
879879
],
880880
],
881+
'fix closing parenthesis (without trailing comma)' => [
882+
'<?php
883+
if (true) {
884+
execute(
885+
$foo,
886+
$bar
887+
);
888+
}',
889+
'<?php
890+
if (true) {
891+
execute(
892+
$foo,
893+
$bar
894+
);
895+
}',
896+
[
897+
'on_multiline' => 'ensure_fully_multiline',
898+
],
899+
],
881900
];
882901
}
883902

@@ -942,6 +961,18 @@ public function provideFix73Cases()
942961
,
943962
['after_heredoc' => true],
944963
],
964+
[
965+
<<<'EXPECTED'
966+
<?php
967+
foo(
968+
$bar,
969+
$baz,
970+
);
971+
EXPECTED
972+
,
973+
null,
974+
['on_multiline' => 'ensure_fully_multiline'],
975+
],
945976
[
946977
'<?php
947978
functionCall(

0 commit comments

Comments
 (0)