Skip to content

Commit

Permalink
Merge branch '2.12' into 2.14
Browse files Browse the repository at this point in the history
* 2.12:
  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
  • Loading branch information
SpacePossum committed Aug 2, 2019
2 parents db00b77 + d3f1bd8 commit 0fac8c1
Show file tree
Hide file tree
Showing 27 changed files with 473 additions and 177 deletions.
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ NOTE: the output for the following formats are generated in accordance with XML
* ``checkstyle`` follows the common `"checkstyle" xml schema </doc/checkstyle.xsd>`_


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

The ``--rules`` option limits the rules to apply on the
The ``--rules`` option limits the rules to apply to the
project:

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

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

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

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

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

Configuration options:

Expand All @@ -1593,7 +1593,7 @@ Choose from the list of available rules:

Class names should match the file name.

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/Console/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static function getHelpCopy()
* ``checkstyle`` follows the common `"checkstyle" xml schema </doc/checkstyle.xsd>`_
The <comment>--verbose</comment> option will show the applied rules. When using the ``txt`` format it will also displays progress notifications.
The <comment>--verbose</comment> option will show the applied rules. When using the ``txt`` format it will also display progress notifications.
The <comment>--rules</comment> option limits the rules to apply on the
The <comment>--rules</comment> option limits the rules to apply to the
project:
<info>$ php %command.full_name% /path/to/project --rules=@PSR2</info>
Expand Down Expand Up @@ -109,7 +109,7 @@ public static function getHelpCopy()
* <comment>sbd</comment>: Sebastianbergmann/diff format (default when using `--diff` without specifying `diff-format`).
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.
Risky rule is a rule, which could change code behaviour. By default no risky rules are run.
A rule is considered risky if it could change code behaviour. By default no risky rules are run.
The <comment>--stop-on-violation</comment> flag stops the execution upon first file that needs to be fixed.
Expand Down
26 changes: 18 additions & 8 deletions src/Fixer/Alias/MbStrFunctionsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
'substr_count' => ['alternativeName' => 'mb_substr_count', 'argumentCount' => [2, 3, 4]],
];

/**
* @var array<string, array>
*/
private $functions;

public function __construct()
{
parent::__construct();

$this->functions = array_filter(
self::$functionsMap,
static function (array $mapping) {
return \function_exists($mapping['alternativeName']);
}
);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -86,15 +103,8 @@ public function isCandidate(Tokens $tokens)
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$functions = array_filter(
self::$functionsMap,
static function ($mapping) {
return \function_exists($mapping['alternativeName']);
}
);

$argumentsAnalyzer = new ArgumentsAnalyzer();
foreach ($functions as $functionIdentity => $functionReplacement) {
foreach ($this->functions as $functionIdentity => $functionReplacement) {
$currIndex = 0;
while (null !== $currIndex) {
// try getting function reference and translate boundaries for humans
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Basic/Psr0Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class InvalidName {}
),
],
null,
'This fixer may change your class name, which will break the code that is depended on old name.'
'This fixer may change your class name, which will break the code that depends on the old name.'
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Basic/Psr4Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class InvalidName {}
),
],
null,
'This fixer may change your class name, which will break the code that is depended on old name.'
'This fixer may change your class name, which will break the code that depends on the old name.'
);
}

Expand Down
25 changes: 23 additions & 2 deletions src/Fixer/Import/NoLeadingImportSlashFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;

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

if ($nextToken->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearAt($nextTokenIdx);
$this->removeLeadingImportSlash($tokens, $nextTokenIdx);
} elseif ($nextToken->isGivenKind([CT::T_FUNCTION_IMPORT, CT::T_CONST_IMPORT])) {
$nextTokenIdx = $tokens->getNextMeaningfulToken($nextTokenIdx);
if ($tokens[$nextTokenIdx]->isGivenKind(T_NS_SEPARATOR)) {
$tokens->clearAt($nextTokenIdx);
$this->removeLeadingImportSlash($tokens, $nextTokenIdx);
}
}
}
}

/**
* @param Tokens $tokens
* @param int $index
*/
private function removeLeadingImportSlash(Tokens $tokens, $index)
{
$previousIndex = $tokens->getPrevNonWhitespace($index);

if (
$previousIndex < $index - 1
|| $tokens[$previousIndex]->isComment()
) {
$tokens->clearAt($index);

return;
}

$tokens[$index] = new Token([T_WHITESPACE, ' ']);
}
}
69 changes: 59 additions & 10 deletions src/Fixer/NamespaceNotation/BlankLineAfterNamespaceFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -62,7 +63,6 @@ public function isCandidate(Tokens $tokens)
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$ending = $this->whitespacesConfig->getLineEnding();
$lastIndex = $tokens->count() - 1;

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

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

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

if (!$nextToken->isWhitespace()) {
$tokens->insertAt($semicolonIndex + 1, new Token([T_WHITESPACE, $ending.$ending]));
if (null !== $indexToEnsureBlankLine && $tokens[$indexToEnsureBlankLine]->isWhitespace()) {
$tokens[$indexToEnsureBlankLine] = $this->getTokenToInsert($tokens[$indexToEnsureBlankLine]->getContent(), $indexToEnsureBlankLine === $lastIndex);
} else {
$tokens[$nextIndex] = new Token([
T_WHITESPACE,
($nextIndex === $lastIndex ? $ending : $ending.$ending).ltrim($nextToken->getContent()),
]);
$tokens->insertAt($indexToEnsureBlankLineAfter + 1, $this->getTokenToInsert('', $indexToEnsureBlankLineAfter === $lastIndex));
}
}
}

/**
* @param Tokens $tokens
* @param int $index
*
* @return int
*/
private function getIndexToEnsureBlankLineAfter(Tokens $tokens, $index)
{
$indexToEnsureBlankLine = $index;
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);

while (null !== $nextIndex) {
$token = $tokens[$nextIndex];

if ($token->isWhitespace()) {
if (1 === Preg::match('/\R/', $token->getContent())) {
break;
}
$nextNextIndex = $tokens->getNonEmptySibling($nextIndex, 1);

if (!$tokens[$nextNextIndex]->isComment()) {
break;
}
}

if (!$token->isWhitespace() && !$token->isComment()) {
break;
}

$indexToEnsureBlankLine = $nextIndex;
$nextIndex = $tokens->getNonEmptySibling($indexToEnsureBlankLine, 1);
}

return $indexToEnsureBlankLine;
}

/**
* @param string $currentContent
* @param bool $isLastIndex
*
* @return Token
*/
private function getTokenToInsert($currentContent, $isLastIndex)
{
$ending = $this->whitespacesConfig->getLineEnding();

$emptyLines = $isLastIndex ? $ending : $ending.$ending;
$indent = 1 === Preg::match('/^.*\R( *)$/s', $currentContent, $matches) ? $matches[1] : '';

return new Token([T_WHITESPACE, $emptyLines.$indent]);
}
}
6 changes: 3 additions & 3 deletions src/Fixer/Operator/NewWithBracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
}

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

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

// new statement with () - nothing to do
if ($nextToken->equals('(')) {
if ($nextToken->equals('(') || $nextToken->isGivenKind(T_OBJECT_OPERATOR)) {
continue;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Fixer/PhpTag/BlankLineAfterOpeningTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
$tokens[0] = new Token([$token->getId(), rtrim($token->getContent()).$lineEnding]);
}

if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
if (false === strpos($tokens[1]->getContent(), "\n")) {
if ($tokens[1]->isWhitespace()) {
$tokens[1] = new Token([T_WHITESPACE, $lineEnding.$tokens[1]->getContent()]);
} else {
$tokens->insertAt(1, new Token([T_WHITESPACE, $lineEnding]));
}
}
}
}
10 changes: 4 additions & 6 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ final class RuleSet implements RuleSetInterface
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method']],
'class_definition' => ['single_line' => true],
'concat_space' => ['spacing' => 'none'],
'concat_space' => true,
'declare_equal_normalize' => true,
'function_typehint_space' => true,
'include' => true,
Expand All @@ -96,7 +96,7 @@ final class RuleSet implements RuleSetInterface
]],
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_mixed_echo_print' => ['use' => 'echo'],
'no_mixed_echo_print' => true,
'no_multiline_whitespace_around_double_arrow' => true,
'no_short_bool_cast' => true,
'no_singleline_whitespace_before_semicolons' => true,
Expand Down Expand Up @@ -193,9 +193,7 @@ final class RuleSet implements RuleSetInterface
],
'no_alias_functions' => true,
'no_homoglyph_names' => true,
'non_printable_character' => [
'use_escape_sequences_in_strings' => false,
],
'non_printable_character' => true,
'php_unit_construct' => true,
'psr4' => true,
'self_accessor' => true,
Expand Down Expand Up @@ -339,7 +337,7 @@ final class RuleSet implements RuleSetInterface
],
'@PHPUnit50Migration:risky' => [
'@PHPUnit48Migration:risky' => true,
'php_unit_dedicate_assert' => ['target' => PhpUnitTargetVersion::VERSION_5_0],
'php_unit_dedicate_assert' => true,
],
'@PHPUnit52Migration:risky' => [
'@PHPUnit50Migration:risky' => true,
Expand Down
Loading

0 comments on commit 0fac8c1

Please sign in to comment.