|
@@ -15,6 +15,10 @@ declare(strict_types=1);
|
|
|
namespace PhpCsFixer\Fixer\Operator;
|
|
|
|
|
|
use PhpCsFixer\AbstractFixer;
|
|
|
+use PhpCsFixer\Fixer\ConfigurableFixerInterface;
|
|
|
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
|
|
|
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
|
|
|
+use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
|
|
|
use PhpCsFixer\FixerDefinition\CodeSample;
|
|
|
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
|
|
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
|
@@ -22,17 +26,30 @@ use PhpCsFixer\Tokenizer\Tokens;
|
|
|
use PhpCsFixer\Tokenizer\TokensAnalyzer;
|
|
|
|
|
|
/**
|
|
|
- * Fixer for rules defined in PSR12 ¶6.1.
|
|
|
- *
|
|
|
* @author Gregor Harlan <gharlan@web.de>
|
|
|
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
|
|
*/
|
|
|
-final class UnaryOperatorSpacesFixer extends AbstractFixer
|
|
|
+final class UnaryOperatorSpacesFixer extends AbstractFixer implements ConfigurableFixerInterface
|
|
|
{
|
|
|
public function getDefinition(): FixerDefinitionInterface
|
|
|
{
|
|
|
return new FixerDefinition(
|
|
|
'Unary operators should be placed adjacent to their operands.',
|
|
|
- [new CodeSample("<?php\n\$sample ++;\n-- \$sample;\n\$sample = ! ! \$a;\n\$sample = ~ \$c;\nfunction & foo(){}\n")]
|
|
|
+ [
|
|
|
+ new CodeSample("<?php\n\$sample ++;\n-- \$sample;\n\$sample = ! ! \$a;\n\$sample = ~ \$c;\nfunction & foo(){}\n"),
|
|
|
+ new CodeSample(
|
|
|
+ '<?php
|
|
|
+function foo($a, ... $b) { return (-- $a) * ($b ++);}
|
|
|
+',
|
|
|
+ ['only_dec_inc' => false]
|
|
|
+ ),
|
|
|
+ new CodeSample(
|
|
|
+ '<?php
|
|
|
+function foo($a, ... $b) { return (-- $a) * ($b ++);}
|
|
|
+',
|
|
|
+ ['only_dec_inc' => true]
|
|
|
+ ),
|
|
|
+ ]
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -51,11 +68,25 @@ final class UnaryOperatorSpacesFixer extends AbstractFixer
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
|
|
|
+ {
|
|
|
+ return new FixerConfigurationResolver([
|
|
|
+ (new FixerOptionBuilder('only_dec_inc', 'Limit to increment and decrement operators.'))
|
|
|
+ ->setAllowedTypes(['bool'])
|
|
|
+ ->setDefault(false)
|
|
|
+ ->getOption(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
|
|
{
|
|
|
$tokensAnalyzer = new TokensAnalyzer($tokens);
|
|
|
|
|
|
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
|
|
|
+ if (true === $this->configuration['only_dec_inc'] && !$tokens[$index]->isGivenKind([T_DEC, T_INC])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
if ($tokensAnalyzer->isUnarySuccessorOperator($index)) {
|
|
|
if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isComment()) {
|
|
|
$tokens->removeLeadingWhitespace($index);
|