Skip to content

Commit da6acd9

Browse files
committed
Merge branch '2.14' into 2.15
* 2.14: DX: control names of public methods in test's classes NoLeadingImportSlashFixer - Add space if needed BlankLineAfterOpeningTagFixer - do not remove indent, handle comments fix typos in README NewWithBracesFixer - Fix object operator and curly brace open cases PHP7.4 DX: test that default config is not passed in RuleSet
2 parents 8e06c72 + 0fac8c1 commit da6acd9

27 files changed

+473
-177
lines changed

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ NOTE: the output for the following formats are generated in accordance with XML
167167
* ``checkstyle`` follows the common `"checkstyle" xml schema </doc/checkstyle.xsd>`_
168168

169169

170-
The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also displays progress notifications.
170+
The ``--verbose`` option will show the applied rules. When using the ``txt`` format it will also display progress notifications.
171171

172-
The ``--rules`` option limits the rules to apply on the
172+
The ``--rules`` option limits the rules to apply to the
173173
project:
174174

175175
.. code-block:: bash
@@ -214,7 +214,7 @@ The ``--diff-format`` option allows to specify in which format the fixer should
214214
* ``sbd``: Sebastianbergmann/diff format (default when using `--diff` without specifying `diff-format`).
215215

216216
The ``--allow-risky`` option (pass ``yes`` or ``no``) allows you to set whether risky rules may run. Default value is taken from config file.
217-
Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
217+
A rule is considered risky if it could change code behaviour. By default no risky rules are run.
218218

219219
The ``--stop-on-violation`` flag stops the execution upon first file that needs to be fixed.
220220

@@ -1644,7 +1644,7 @@ Choose from the list of available rules:
16441644
Classes must be in a path that matches their namespace, be at least one
16451645
namespace deep and the class name should match the file name.
16461646

1647-
*Risky rule: this fixer may change your class name, which will break the code that is depended on old name.*
1647+
*Risky rule: this fixer may change your class name, which will break the code that depends on the old name.*
16481648

16491649
Configuration options:
16501650

@@ -1655,7 +1655,7 @@ Choose from the list of available rules:
16551655

16561656
Class names should match the file name.
16571657

1658-
*Risky rule: this fixer may change your class name, which will break the code that is depended on old name.*
1658+
*Risky rule: this fixer may change your class name, which will break the code that depends on the old name.*
16591659

16601660
* **random_api_migration** [@PHP70Migration:risky, @PHP71Migration:risky]
16611661

src/Console/Command/HelpCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public static function getHelpCopy()
7272
* ``checkstyle`` follows the common `"checkstyle" xml schema </doc/checkstyle.xsd>`_
7373
7474
75-
The <comment>--verbose</comment> option will show the applied rules. When using the ``txt`` format it will also displays progress notifications.
75+
The <comment>--verbose</comment> option will show the applied rules. When using the ``txt`` format it will also display progress notifications.
7676
77-
The <comment>--rules</comment> option limits the rules to apply on the
77+
The <comment>--rules</comment> option limits the rules to apply to the
7878
project:
7979
8080
<info>$ php %command.full_name% /path/to/project --rules=@PSR2</info>
@@ -109,7 +109,7 @@ public static function getHelpCopy()
109109
* <comment>sbd</comment>: Sebastianbergmann/diff format (default when using `--diff` without specifying `diff-format`).
110110
111111
The <comment>--allow-risky</comment> option (pass ``yes`` or ``no``) allows you to set whether risky rules may run. Default value is taken from config file.
112-
Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
112+
A rule is considered risky if it could change code behaviour. By default no risky rules are run.
113113
114114
The <comment>--stop-on-violation</comment> flag stops the execution upon first file that needs to be fixed.
115115

src/Fixer/Alias/MbStrFunctionsFixer.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
4343
'substr_count' => ['alternativeName' => 'mb_substr_count', 'argumentCount' => [2, 3, 4]],
4444
];
4545

46+
/**
47+
* @var array<string, array>
48+
*/
49+
private $functions;
50+
51+
public function __construct()
52+
{
53+
parent::__construct();
54+
55+
$this->functions = array_filter(
56+
self::$functionsMap,
57+
static function (array $mapping) {
58+
return \function_exists($mapping['alternativeName']);
59+
}
60+
);
61+
}
62+
4663
/**
4764
* {@inheritdoc}
4865
*/
@@ -86,15 +103,8 @@ public function isCandidate(Tokens $tokens)
86103
*/
87104
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
88105
{
89-
$functions = array_filter(
90-
self::$functionsMap,
91-
static function ($mapping) {
92-
return \function_exists($mapping['alternativeName']);
93-
}
94-
);
95-
96106
$argumentsAnalyzer = new ArgumentsAnalyzer();
97-
foreach ($functions as $functionIdentity => $functionReplacement) {
107+
foreach ($this->functions as $functionIdentity => $functionReplacement) {
98108
$currIndex = 0;
99109
while (null !== $currIndex) {
100110
// try getting function reference and translate boundaries for humans

src/Fixer/Basic/Psr0Fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class InvalidName {}
5454
),
5555
],
5656
null,
57-
'This fixer may change your class name, which will break the code that is depended on old name.'
57+
'This fixer may change your class name, which will break the code that depends on the old name.'
5858
);
5959
}
6060

src/Fixer/Basic/Psr4Fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class InvalidName {}
4343
),
4444
],
4545
null,
46-
'This fixer may change your class name, which will break the code that is depended on old name.'
46+
'This fixer may change your class name, which will break the code that depends on the old name.'
4747
);
4848
}
4949

src/Fixer/Import/NoLeadingImportSlashFixer.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpCsFixer\FixerDefinition\CodeSample;
1717
use PhpCsFixer\FixerDefinition\FixerDefinition;
1818
use PhpCsFixer\Tokenizer\CT;
19+
use PhpCsFixer\Tokenizer\Token;
1920
use PhpCsFixer\Tokenizer\Tokens;
2021
use PhpCsFixer\Tokenizer\TokensAnalyzer;
2122

@@ -65,13 +66,33 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
6566
$nextToken = $tokens[$nextTokenIdx];
6667

6768
if ($nextToken->isGivenKind(T_NS_SEPARATOR)) {
68-
$tokens->clearAt($nextTokenIdx);
69+
$this->removeLeadingImportSlash($tokens, $nextTokenIdx);
6970
} elseif ($nextToken->isGivenKind([CT::T_FUNCTION_IMPORT, CT::T_CONST_IMPORT])) {
7071
$nextTokenIdx = $tokens->getNextMeaningfulToken($nextTokenIdx);
7172
if ($tokens[$nextTokenIdx]->isGivenKind(T_NS_SEPARATOR)) {
72-
$tokens->clearAt($nextTokenIdx);
73+
$this->removeLeadingImportSlash($tokens, $nextTokenIdx);
7374
}
7475
}
7576
}
7677
}
78+
79+
/**
80+
* @param Tokens $tokens
81+
* @param int $index
82+
*/
83+
private function removeLeadingImportSlash(Tokens $tokens, $index)
84+
{
85+
$previousIndex = $tokens->getPrevNonWhitespace($index);
86+
87+
if (
88+
$previousIndex < $index - 1
89+
|| $tokens[$previousIndex]->isComment()
90+
) {
91+
$tokens->clearAt($index);
92+
93+
return;
94+
}
95+
96+
$tokens[$index] = new Token([T_WHITESPACE, ' ']);
97+
}
7798
}

src/Fixer/NamespaceNotation/BlankLineAfterNamespaceFixer.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
1717
use PhpCsFixer\FixerDefinition\CodeSample;
1818
use PhpCsFixer\FixerDefinition\FixerDefinition;
19+
use PhpCsFixer\Preg;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
2122

@@ -62,7 +63,6 @@ public function isCandidate(Tokens $tokens)
6263
*/
6364
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
6465
{
65-
$ending = $this->whitespacesConfig->getLineEnding();
6666
$lastIndex = $tokens->count() - 1;
6767

6868
for ($index = $lastIndex; $index >= 0; --$index) {
@@ -75,21 +75,70 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
7575
$semicolonIndex = $tokens->getNextTokenOfKind($index, [';', '{', [T_CLOSE_TAG]]);
7676
$semicolonToken = $tokens[$semicolonIndex];
7777

78-
if (!isset($tokens[$semicolonIndex + 1]) || !$semicolonToken->equals(';')) {
78+
if (!$semicolonToken->equals(';')) {
7979
continue;
8080
}
8181

82-
$nextIndex = $semicolonIndex + 1;
83-
$nextToken = $tokens[$nextIndex];
82+
$indexToEnsureBlankLineAfter = $this->getIndexToEnsureBlankLineAfter($tokens, $semicolonIndex);
83+
$indexToEnsureBlankLine = $tokens->getNonEmptySibling($indexToEnsureBlankLineAfter, 1);
8484

85-
if (!$nextToken->isWhitespace()) {
86-
$tokens->insertAt($semicolonIndex + 1, new Token([T_WHITESPACE, $ending.$ending]));
85+
if (null !== $indexToEnsureBlankLine && $tokens[$indexToEnsureBlankLine]->isWhitespace()) {
86+
$tokens[$indexToEnsureBlankLine] = $this->getTokenToInsert($tokens[$indexToEnsureBlankLine]->getContent(), $indexToEnsureBlankLine === $lastIndex);
8787
} else {
88-
$tokens[$nextIndex] = new Token([
89-
T_WHITESPACE,
90-
($nextIndex === $lastIndex ? $ending : $ending.$ending).ltrim($nextToken->getContent()),
91-
]);
88+
$tokens->insertAt($indexToEnsureBlankLineAfter + 1, $this->getTokenToInsert('', $indexToEnsureBlankLineAfter === $lastIndex));
9289
}
9390
}
9491
}
92+
93+
/**
94+
* @param Tokens $tokens
95+
* @param int $index
96+
*
97+
* @return int
98+
*/
99+
private function getIndexToEnsureBlankLineAfter(Tokens $tokens, $index)
100+
{
101+
$indexToEnsureBlankLine = $index;
102+
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);
103+
104+
while (null !== $nextIndex) {
105+
$token = $tokens[$nextIndex];
106+
107+
if ($token->isWhitespace()) {
108+
if (1 === Preg::match('/\R/', $token->getContent())) {
109+
break;
110+
}
111+
$nextNextIndex = $tokens->getNonEmptySibling($nextIndex, 1);
112+
113+
if (!$tokens[$nextNextIndex]->isComment()) {
114+
break;
115+
}
116+
}
117+
118+
if (!$token->isWhitespace() && !$token->isComment()) {
119+
break;
120+
}
121+
122+
$indexToEnsureBlankLine = $nextIndex;
123+
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);
124+
}
125+
126+
return $indexToEnsureBlankLine;
127+
}
128+
129+
/**
130+
* @param string $currentContent
131+
* @param bool $isLastIndex
132+
*
133+
* @return Token
134+
*/
135+
private function getTokenToInsert($currentContent, $isLastIndex)
136+
{
137+
$ending = $this->whitespacesConfig->getLineEnding();
138+
139+
$emptyLines = $isLastIndex ? $ending : $ending.$ending;
140+
$indent = 1 === Preg::match('/^.*\R( *)$/s', $currentContent, $matches) ? $matches[1] : '';
141+
142+
return new Token([T_WHITESPACE, $emptyLines.$indent]);
143+
}
95144
}

src/Fixer/Operator/NewWithBracesFixer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
120120
}
121121

122122
// entrance into array index syntax - need to look for exit
123-
while ($nextToken->equals('[')) {
124-
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
123+
while ($nextToken->equals('[') || $nextToken->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
124+
$nextIndex = $tokens->findBlockEnd($tokens->detectBlockType($nextToken)['type'], $nextIndex) + 1;
125125
$nextToken = $tokens[$nextIndex];
126126
}
127127

@@ -132,7 +132,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
132132
}
133133

134134
// new statement with () - nothing to do
135-
if ($nextToken->equals('(')) {
135+
if ($nextToken->equals('(') || $nextToken->isGivenKind(T_OBJECT_OPERATOR)) {
136136
continue;
137137
}
138138

src/Fixer/PhpTag/BlankLineAfterOpeningTagFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
8585
$tokens[0] = new Token([$token->getId(), rtrim($token->getContent()).$lineEnding]);
8686
}
8787

88-
if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
89-
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
88+
if (false === strpos($tokens[1]->getContent(), "\n")) {
89+
if ($tokens[1]->isWhitespace()) {
90+
$tokens[1] = new Token([T_WHITESPACE, $lineEnding.$tokens[1]->getContent()]);
91+
} else {
92+
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
93+
}
9094
}
9195
}
9296
}

src/RuleSet.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final class RuleSet implements RuleSetInterface
6969
'cast_spaces' => true,
7070
'class_attributes_separation' => ['elements' => ['method']],
7171
'class_definition' => ['single_line' => true],
72-
'concat_space' => ['spacing' => 'none'],
72+
'concat_space' => true,
7373
'declare_equal_normalize' => true,
7474
'function_typehint_space' => true,
7575
'include' => true,
@@ -97,7 +97,7 @@ final class RuleSet implements RuleSetInterface
9797
]],
9898
'no_leading_import_slash' => true,
9999
'no_leading_namespace_whitespace' => true,
100-
'no_mixed_echo_print' => ['use' => 'echo'],
100+
'no_mixed_echo_print' => true,
101101
'no_multiline_whitespace_around_double_arrow' => true,
102102
'no_short_bool_cast' => true,
103103
'no_singleline_whitespace_before_semicolons' => true,
@@ -195,9 +195,7 @@ final class RuleSet implements RuleSetInterface
195195
],
196196
'no_alias_functions' => true,
197197
'no_homoglyph_names' => true,
198-
'non_printable_character' => [
199-
'use_escape_sequences_in_strings' => false,
200-
],
198+
'non_printable_character' => true,
201199
'php_unit_construct' => true,
202200
'php_unit_mock_short_will_return' => true,
203201
'psr4' => true,
@@ -347,7 +345,7 @@ final class RuleSet implements RuleSetInterface
347345
],
348346
'@PHPUnit50Migration:risky' => [
349347
'@PHPUnit48Migration:risky' => true,
350-
'php_unit_dedicate_assert' => ['target' => PhpUnitTargetVersion::VERSION_5_0],
348+
'php_unit_dedicate_assert' => true,
351349
],
352350
'@PHPUnit52Migration:risky' => [
353351
'@PHPUnit50Migration:risky' => true,

0 commit comments

Comments
 (0)