Skip to content

Commit db00b77

Browse files
committed
Merge branch '2.12' into 2.14
* 2.12: 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/PhpdocToReturnTypeFixerTest.php
2 parents 022ff1e + 74666dc commit db00b77

18 files changed

+413
-105
lines changed

README.rst

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

706706
* **function_typehint_space** [@Symfony, @PhpCsFixer]
707707

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

710710
* **general_phpdoc_annotation_remove**
711711

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
@@ -326,13 +326,13 @@ private function ensureFunctionFullyMultiline(Tokens $tokens, $startFunctionInde
326326
$indentation = $existingIndentation.$this->whitespacesConfig->getIndent();
327327
$endFunctionIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startFunctionIndex);
328328

329-
if (!$this->isNewline($tokens[$endFunctionIndex - 1])) {
330-
$tokens->ensureWhitespaceAtIndex(
331-
$endFunctionIndex,
332-
0,
333-
$this->whitespacesConfig->getLineEnding().$existingIndentation
334-
);
329+
$wasWhitespaceBeforeEndFunctionAddedAsNewToken = $tokens->ensureWhitespaceAtIndex(
330+
$tokens[$endFunctionIndex - 1]->isWhitespace() ? $endFunctionIndex - 1 : $endFunctionIndex,
331+
0,
332+
$this->whitespacesConfig->getLineEnding().$existingIndentation
333+
);
335334

335+
if ($wasWhitespaceBeforeEndFunctionAddedAsNewToken) {
336336
++$endFunctionIndex;
337337
}
338338

@@ -390,6 +390,11 @@ private function fixNewline(Tokens $tokens, $index, $indentation, $override = tr
390390
return;
391391
}
392392

393+
$nextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($index);
394+
if ($tokens[$nextMeaningfulTokenIndex]->equals(')')) {
395+
return;
396+
}
397+
393398
$tokens->ensureWhitespaceAtIndex($index + 1, 0, $this->whitespacesConfig->getLineEnding().$indentation);
394399
}
395400

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
],

0 commit comments

Comments
 (0)