Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
818e26c
commit 2658c28
Showing
3 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* Dariusz Rumiński <dariusz.ruminski@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\Fixer\Operator; | ||
|
||
use PhpCsFixer\AbstractFixer; | ||
use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface; | ||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver; | ||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder; | ||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* @author Kuba Werłos <werlos@gmail.com> | ||
*/ | ||
final class OperatorLinebreakFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
const BOOLEAN_OPERATORS = [ | ||
'&&', | ||
'||', | ||
'and', | ||
'or', | ||
'xor', | ||
]; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
const NON_BOOLEAN_OPERATORS = [ | ||
'=', | ||
'*', | ||
'/', | ||
'%', | ||
'<', | ||
'>', | ||
'|', | ||
'^', | ||
'+', | ||
'-', | ||
'&', | ||
'&=', | ||
'.=', | ||
'/=', | ||
'=>', | ||
'==', | ||
'>=', | ||
'===', | ||
'!=', | ||
'<>', | ||
'!==', | ||
'<=', | ||
'-=', | ||
'%=', | ||
'*=', | ||
'|=', | ||
'+=', | ||
'<<', | ||
'<<=', | ||
'>>', | ||
'>>=', | ||
'^=', | ||
'**', | ||
'**=', | ||
'<=>', | ||
'??', | ||
]; | ||
|
||
/** @var string[] */ | ||
private $operators; | ||
|
||
/** @var string */ | ||
private $position = 'beginning'; | ||
|
||
public function __construct() | ||
{ | ||
$this->operators = \array_merge(self::BOOLEAN_OPERATORS, self::NON_BOOLEAN_OPERATORS); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return FixerDefinition | ||
*/ | ||
public function getDefinition() | ||
{ | ||
return new FixerDefinition( | ||
'Binary operators must always be at the beginning or at the end of the line.', | ||
[new CodeSample('<?php | ||
function foo() { | ||
return $bar || | ||
$baz; | ||
} | ||
')] | ||
); | ||
} | ||
|
||
/** | ||
* @return FixerConfigurationResolver | ||
*/ | ||
public function getConfigurationDefinition() | ||
{ | ||
return new FixerConfigurationResolver([ | ||
(new FixerOptionBuilder('only_booleans', 'whether to limit operators to only boolean ones')) | ||
->setDefault(false) | ||
->setAllowedTypes(['bool']) | ||
->getOption(), | ||
(new FixerOptionBuilder('position', 'whether to place operators at the beginning or at the end of the line')) | ||
->setDefault($this->position) | ||
->setAllowedValues(['beginning', 'end']) | ||
->getOption(), | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(array $configuration = null) | ||
{ | ||
parent::configure($configuration); | ||
|
||
if (isset($configuration['only_booleans']) && $configuration['only_booleans']) { | ||
$this->operators = self::BOOLEAN_OPERATORS; | ||
} | ||
$this->position = isset($configuration['position']) ? $configuration['position'] : $this->position; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCandidate(Tokens $tokens) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isRisky() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPriority() | ||
{ | ||
return 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function applyFix(\SplFileInfo $file, Tokens $tokens) | ||
{ | ||
if ('beginning' === $this->position) { | ||
$this->fixMoveToTheBeginning($tokens); | ||
} else { | ||
$this->fixMoveToTheEnd($tokens); | ||
} | ||
} | ||
|
||
private function fixMoveToTheBeginning(Tokens $tokens) | ||
{ | ||
for ($index = 0; $index < $tokens->count(); ++$index) { | ||
$tokenContent = \strtolower($tokens[$index]->getContent()); | ||
|
||
if (!\in_array($tokenContent, $this->operators, true)) { | ||
continue; | ||
} | ||
|
||
$nextIndex = $tokens->getNextMeaningfulToken($index); | ||
for ($i = $nextIndex - 1; $i > $index; --$i) { | ||
if ($tokens[$i]->isWhitespace() && 1 === Preg::match('/\R/u', $tokens[$i]->getContent())) { | ||
$operator = clone $tokens[$index]; | ||
$tokens->clearAt($index); | ||
if ($tokens[$index - 1]->isWhitespace()) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($index - 1); | ||
} | ||
$tokens->insertAt($nextIndex, [$operator, new Token([T_WHITESPACE, ' '])]); | ||
|
||
break; | ||
} | ||
} | ||
$index = $nextIndex; | ||
} | ||
} | ||
|
||
private function fixMoveToTheEnd(Tokens $tokens) | ||
{ | ||
for ($index = $tokens->count() - 1; $index > 0; --$index) { | ||
$tokenContent = \strtolower($tokens[$index]->getContent()); | ||
|
||
if (!\in_array($tokenContent, $this->operators, true)) { | ||
continue; | ||
} | ||
|
||
$prevIndex = $tokens->getPrevMeaningfulToken($index); | ||
for ($i = $prevIndex + 1; $i < $index; ++$i) { | ||
if ($tokens[$i]->isWhitespace() && 1 === Preg::match('/\R/u', $tokens[$i]->getContent())) { | ||
$operator = clone $tokens[$index]; | ||
$tokens->clearAt($index); | ||
if ($tokens[$index + 1]->isWhitespace()) { | ||
$tokens->clearTokenAndMergeSurroundingWhitespace($index + 1); | ||
} | ||
$tokens->insertAt($prevIndex + 1, [new Token([T_WHITESPACE, ' ']), $operator]); | ||
|
||
break; | ||
} | ||
} | ||
$index = $prevIndex; | ||
} | ||
} | ||
} |
Oops, something went wrong.