Skip to content
Permalink
Browse files

Merge pull request #149 from kubawerlos/cleanup

Cleanup
  • Loading branch information...
kubawerlos committed Oct 9, 2019
2 parents 3fac21b + 5693c1a commit 00c5a257a5f1a1b8a6f8585498694fa8ec88d066
@@ -48,6 +48,7 @@ return PhpCsFixer\Config::create()
],
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'final_class' => true,
'final_internal_class' => true,
'fully_qualified_strict_types' => true,
'function_to_constant' => [
@@ -20,7 +20,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class ReadmeCommand extends BaseCommand
final class ReadmeCommand extends BaseCommand
{
private const NAME = 'PHP CS Fixer: custom fixers';
@@ -16,15 +16,21 @@
final class OperatorLinebreakFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, DeprecatingFixerInterface
{
private const BOOLEAN_OPERATORS = [
'&&' => T_BOOLEAN_AND,
'||' => T_BOOLEAN_OR,
'and' => T_LOGICAL_AND,
'or' => T_LOGICAL_OR,
'xor' => T_LOGICAL_XOR,
private const CONFIG_ONLY_BOOLEANS = 'only_booleans';
private const CONFIG_POSITION = 'position';
private const CONFIG_POSITION_BEGINNING = 'beginning';
private const CONFIG_POSITION_END = 'end';
private const OPERATORS_BOOLEANS = [
'&&' => true,
'||' => true,
'and' => true,
'or' => true,
'xor' => true,
];
private const NON_BOOLEAN_OPERATORS = [
private const OPERATORS_NON_BOOLEANS = [
'=' => true,
'.' => true,
'*' => true,
@@ -66,16 +72,11 @@ final class OperatorLinebreakFixer extends AbstractFixer implements Configuratio
':' => true,
];
/** @var array<string, int|true> */
private $operators;
/** @var string */
private $position = 'beginning';
private $position = self::CONFIG_POSITION_BEGINNING;
public function __construct()
{
$this->operators = \array_merge(self::BOOLEAN_OPERATORS, self::NON_BOOLEAN_OPERATORS);
}
/** @var array<string, true> */
private $operators = [];
public function getDefinition(): FixerDefinitionInterface
{
@@ -93,24 +94,25 @@ function foo() {
public function getConfigurationDefinition(): FixerConfigurationResolver
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('only_booleans', 'whether to limit operators to only boolean ones'))
(new FixerOptionBuilder(self::CONFIG_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'))
(new FixerOptionBuilder(self::CONFIG_POSITION, 'whether to place operators at the beginning or at the end of the line'))
->setDefault($this->position)
->setAllowedValues(['beginning', 'end'])
->setAllowedValues([self::CONFIG_POSITION_BEGINNING, self::CONFIG_POSITION_END])
->getOption(),
]);
}
public function configure(?array $configuration = null): void
{
if (isset($configuration['only_booleans']) && $configuration['only_booleans'] === true) {
$this->operators = self::BOOLEAN_OPERATORS;
$this->operators = self::OPERATORS_BOOLEANS;
if (!isset($configuration[self::CONFIG_ONLY_BOOLEANS]) || $configuration[self::CONFIG_ONLY_BOOLEANS] !== true) {
$this->operators = \array_merge($this->operators, self::OPERATORS_NON_BOOLEANS);
}
$this->position = $configuration['position'] ?? $this->position;
$this->position = $configuration[self::CONFIG_POSITION] ?? $this->position;
}
public function isCandidate(Tokens $tokens): bool
@@ -125,7 +127,7 @@ public function isRisky(): bool
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
if ($this->position === 'beginning') {
if ($this->position === self::CONFIG_POSITION_BEGINNING) {
$this->fixMoveToTheBeginning($tokens);
} else {
$this->fixMoveToTheEnd($tokens);
@@ -4,6 +4,7 @@
namespace Tests\Fixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\DefinedFixerInterface;
use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tokenizer\Tokens;
@@ -70,6 +71,9 @@ abstract class AbstractFixerTestCase extends TestCase
final public function testCodeSampleIsChangedDuringFixing(): void
{
$codeSample = $this->fixer->getDefinition()->getCodeSamples()[0];
if ($this->fixer instanceof ConfigurableFixerInterface) {
$this->fixer->configure($codeSample->getConfiguration());
}
$tokens = Tokens::fromCode($codeSample->getCode());
@@ -45,13 +45,12 @@ public function testDeprecatingPullRequest(): void
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable
{
yield [
yield 'handle equal sign' => [
'<?php
$foo
= $bar;
@@ -62,7 +61,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle add operator' => [
'<?php
return $foo
+ $bar;
@@ -73,7 +72,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle uppercase operator' => [
'<?php
return $foo
AND $bar;
@@ -84,7 +83,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle concatenation operator' => [
'<?php
return $foo
.$bar;
@@ -95,7 +94,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'ignore add operator when only booleans enabled' => [
'<?php
return $foo +
$bar;
@@ -104,7 +103,7 @@ public function provideFixCases(): iterable
['only_booleans' => true],
];
yield [
yield 'handle ternary operator' => [
'<?php
return $foo
? $bar
@@ -117,7 +116,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle multiple operators' => [
'<?php
return $foo
|| $bar
@@ -130,7 +129,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle operator when on separate line' => [
'<?php
return $foo
|| $bar;
@@ -142,7 +141,7 @@ public function provideFixCases(): iterable
',
];
yield [
yield 'handle multiline operator when position is "end"' => [
'<?php
return $foo ||
$bar ||
@@ -156,7 +155,7 @@ public function provideFixCases(): iterable
['position' => 'end'],
];
yield [
yield 'handle operator when on its own line and position is "end"' => [
'<?php
return $foo ||
$bar;
@@ -169,7 +168,7 @@ public function provideFixCases(): iterable
['position' => 'end'],
];
yield [
yield 'handle operator when no whitespace is before' => [
'<?php
function foo() {
return $a
@@ -184,7 +183,7 @@ function foo() {
',
];
yield [
yield 'handle operator when no whitespace is after and position is "end"' => [
'<?php
function foo() {
return $a||
@@ -200,7 +199,7 @@ function foo() {
['position' => 'end'],
];
yield [
yield 'handle operator with one-line comments' => [
'<?php
function getNewCuyamaTotal() {
return 562 // Population
@@ -217,24 +216,24 @@ function getNewCuyamaTotal() {
',
];
yield [
yield 'handle operator with PHPDoc comments' => [
'<?php
function getNewCuyamaTotal() {
return 562 /* Population */
+ 2150 /* Ft. above sea level */
+ 1951; /* Established */
return 562 /** Population */
+ 2150 /** Ft. above sea level */
+ 1951; /** Established */
}
',
'<?php
function getNewCuyamaTotal() {
return 562 + /* Population */
2150 + /* Ft. above sea level */
1951; /* Established */
return 562 + /** Population */
2150 + /** Ft. above sea level */
1951; /** Established */
}
',
];
yield [
yield 'handle operator with multiple comments next to each other' => [
'<?php
function foo() {
return isThisTheRealLife() // First comment
@@ -253,7 +252,7 @@ function foo() {
',
];
yield [
yield 'handle nested operators' => [
'<?php
function foo() {
return $a
@@ -276,7 +275,7 @@ function foo() {
',
];
yield [
yield 'handle Elvis operator' => [
'<?php
return $foo
?: $bar;
@@ -287,7 +286,7 @@ function foo() {
',
];
yield [
yield 'handle Elvis operator when position is "end"' => [
'<?php
return $foo ?:
$bar;
@@ -299,7 +298,7 @@ function foo() {
['position' => 'end'],
];
yield [
yield 'handle Elvis operator with space inside' => [
'<?php
return $foo
?: $bar;
@@ -310,7 +309,7 @@ function foo() {
',
];
yield [
yield 'handle Elvis operator with comment inside' => [
'<?php
return $foo/* Lorem ipsum */
?: $bar;
@@ -321,7 +320,7 @@ function foo() {
',
];
yield [
yield 'ignore ternary operator inside of switch' => [
'<?php
switch ($foo) {
case 1:

0 comments on commit 00c5a25

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