Skip to content
Permalink
Browse files

OperatorLinebreakFixer - fix for nullable type and return type

  • Loading branch information...
kubawerlos committed Oct 12, 2019
1 parent 6ff9dc1 commit e2e2643c3b9a0296458d7415cccf87cc971255d8
@@ -8,7 +8,7 @@

[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
![Tests](https://img.shields.io/badge/tests-1153-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1197-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)

@@ -21,7 +21,7 @@ final class OperatorLinebreakFixer extends AbstractFixer implements Configuratio
/** @var string */
private $position = 'beginning';
/** @var array<string, int> */
/** @var Token[] */
private $operators = [];
public function getDefinition(): FixerDefinitionInterface
@@ -53,9 +53,63 @@ public function getConfigurationDefinition(): FixerConfigurationResolver
public function configure(?array $configuration = null): void
{
$this->operators = \array_flip(['&&', '||', 'and', 'or', 'xor']);
$this->operators = [
new Token([T_LOGICAL_AND, 'and']),
new Token([T_LOGICAL_OR, 'or']),
new Token([T_LOGICAL_XOR, 'xor']),
new Token('&&'),
new Token('||'),
];
if (!isset($configuration['only_booleans']) || $configuration['only_booleans'] === false) {
$this->operators += \array_flip(['=', '.', '*', '/', '%', '<', '>', '|', '^', '+', '-', '&', '&=', '.=', '/=', '=>', '==', '>=', '===', '!=', '<>', '!==', '<=', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^=', '**', '**=', '<=>', '??', '?', ':']);
$this->operators = \array_merge(
$this->operators,
[
new Token('+'),
new Token('-'),
new Token('*'),
new Token('/'),
new Token('%'),
new Token([T_POW, '**']),
new Token([T_PLUS_EQUAL, '+=']),
new Token([T_MINUS_EQUAL, '-=']),
new Token([T_MUL_EQUAL, '*=']),
new Token([T_DIV_EQUAL, '/=']),
new Token([T_MOD_EQUAL, '%=']),
new Token([T_POW_EQUAL, '**=']),
new Token('='),
new Token('&'),
new Token('|'),
new Token('^'),
new Token([T_SL, '<<']),
new Token([T_SR, '>>']),
new Token([T_AND_EQUAL, '&=']),
new Token([T_OR_EQUAL, '|=']),
new Token([T_XOR_EQUAL, '^=']),
new Token([T_SL_EQUAL, '<<=']),
new Token([T_SR_EQUAL, '>>=']),
new Token([T_IS_EQUAL, '==']),
new Token([T_IS_IDENTICAL, '===']),
new Token([T_IS_NOT_EQUAL, '!=']),
new Token([T_IS_NOT_EQUAL, '<>']),
new Token([T_IS_NOT_IDENTICAL, '!==']),
new Token('<'),
new Token('>'),
new Token([T_IS_SMALLER_OR_EQUAL, '<=']),
new Token([T_IS_GREATER_OR_EQUAL, '>=']),
new Token([T_SPACESHIP, '<=>']),
new Token('and'),
new Token('or'),
new Token('xor'),
new Token([T_BOOLEAN_AND, '&&']),
new Token([T_BOOLEAN_OR, '||']),
new Token('.'),
new Token([T_CONCAT_EQUAL, '.=']),
new Token([T_COALESCE, '??']),
new Token([T_DOUBLE_ARROW, '=>']),
new Token('?'),
new Token(':'),
]
);
}
$this->position = $configuration['position'] ?? $this->position;
@@ -89,7 +143,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
while ($index > 1) {
$index--;
if (!isset($this->operators[\strtolower($tokens[$index]->getContent())])) {
if (!$tokens[$index]->equalsAny($this->operators, false)) {
continue;
}
@@ -117,6 +117,8 @@ abstract class AbstractFixerTestCase extends TestCase
$this->fixer->fix($this->createMock(\SplFileInfo::class), $tokens);
static::assertSame($expected, $tokens->generateCode());
static::assertFalse($tokens->isChanged());
}
}
@@ -400,5 +400,58 @@ function () {
false;
',
];
yield 'nullable type' => [
'<?php
function foo(
?int $x,
?int $y,
?int $z
) {};',
null,
['position' => 'end'],
];
yield 'return type' => [
'<?php
function foo()
:
bool
{};',
];
foreach ([
'+', '-', '*', '/', '%', '**', // Arithmetic
'+=', '-=', '*=', '/=', '%=', '**=', // Arithmetic assignment
'=', // Assignment
'&', '|', '^', '<<', '>>', // Bitwise
'&=', '|=', '^=', '<<=', '>>=', // Bitwise assignment
'==', '===', '!=', '<>', '!==', '<', '>', '<=', '>=', '<=>', // Comparison
'and', 'or', 'xor', '&&', '||', // Logical
'.', '.=', // String
'??', // Null Coalescing
] as $operator) {
yield \sprintf('handle %s operator', $operator) => [
\sprintf('<?php
$foo
%s $bar;
', $operator),
\sprintf('<?php
$foo %s
$bar;
', $operator),
];
}
yield 'handle => operator' => [
'<?php
[$foo
=> $bar];
',
'<?php
[$foo =>
$bar];
',
];
}
}

0 comments on commit e2e2643

Please sign in to comment.
You can’t perform that action at this time.