Skip to content
Permalink
Browse files

Infection - mutating arrays

  • Loading branch information...
kubawerlos committed Oct 19, 2019
1 parent e335de4 commit c9af0c0e309b309492a8fa7621050a7ca44a67ac
@@ -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-1199-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1242-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)

@@ -18,8 +18,12 @@
"mutators": {
"@default": true,
"ArrayItemRemoval": {
"settings": {
"remove": "all"
},
"ignore": [
"PhpCsFixerCustomFixers\\Fixer\\*::getConfigurationDefinition",
"PhpCsFixerCustomFixers\\Fixer\\*::getDefinition",
"PhpCsFixerCustomFixers\\Fixer\\*::isCandidate"
]
},
@@ -88,7 +88,7 @@ private function fixNames(Tokens $tokens, int $startIndex, int $endIndex): void
$functionIndex = $tokens->getTokenNotOfKindSibling(
$index,
1,
[[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC]]
[[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]
);
if (!$tokens[$functionIndex]->isGivenKind(T_FUNCTION)) {
continue;
@@ -129,7 +129,7 @@ private function getDataProviderNames(Tokens $tokens, int $startIndex, int $endI
$functionIndex = $tokens->getTokenNotOfKindSibling(
$index,
1,
[[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC]]
[[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]
);
if (!$tokens[$functionIndex]->isGivenKind(T_FUNCTION)) {
@@ -64,7 +64,7 @@ private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool
continue;
}
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_FUNCTION, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
if ($tokens[$prevIndex]->isGivenKind([T_AS, T_CLASS, T_CONST, T_DOUBLE_COLON, T_OBJECT_OPERATOR, CT::T_USE_TRAIT])) {
continue;
}
@@ -49,7 +49,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}
$nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT]]);
$nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_DOC_COMMENT], [T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE]]);
if ($tokens[$prevIndex]->isGivenKind(T_NEW) || $tokens[$nextIndex]->isGivenKind(T_DOUBLE_COLON)) {
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
@@ -63,7 +63,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$nextIndex = $tokens->getTokenNotOfKindSibling(
$index,
1,
[[T_WHITESPACE], [T_COMMENT], [T_DOC_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC], [T_VAR]]
[[T_WHITESPACE], [T_COMMENT], [T_ABSTRACT], [T_FINAL], [T_PUBLIC], [T_PROTECTED], [T_PRIVATE], [T_STATIC]]
);
if ($nextIndex === null) {
return;
@@ -53,59 +53,52 @@ public function getConfigurationDefinition(): FixerConfigurationResolver
public function configure(?array $configuration = null): void
{
$this->operators = [
[T_LOGICAL_AND, 'and'],
[T_LOGICAL_OR, 'or'],
[T_LOGICAL_XOR, 'xor'],
[T_BOOLEAN_AND, '&&'],
[T_BOOLEAN_OR, '||'],
];
$this->operators = [[T_BOOLEAN_AND], [T_BOOLEAN_OR], [T_LOGICAL_AND], [T_LOGICAL_OR], [T_LOGICAL_XOR]];
if (!isset($configuration['only_booleans']) || $configuration['only_booleans'] === false) {
$this->operators = \array_merge(
$this->operators,
[
'%',
'&',
'*',
'+',
'-',
'*',
'.',
'/',
'%',
[T_POW, '**'],
[T_PLUS_EQUAL, '+='],
[T_MINUS_EQUAL, '-='],
[T_MUL_EQUAL, '*='],
[T_DIV_EQUAL, '/='],
[T_MOD_EQUAL, '%='],
[T_POW_EQUAL, '**='],
'=',
'&',
'|',
'^',
[T_SL, '<<'],
[T_SR, '>>'],
[T_AND_EQUAL, '&='],
[T_OR_EQUAL, '|='],
[T_XOR_EQUAL, '^='],
[T_SL_EQUAL, '<<='],
[T_SR_EQUAL, '>>='],
[T_IS_EQUAL, '=='],
[T_IS_IDENTICAL, '==='],
[T_IS_NOT_EQUAL, '!='],
[T_IS_NOT_EQUAL, '<>'],
[T_IS_NOT_IDENTICAL, '!=='],
':',
'<',
'=',
'>',
[T_IS_SMALLER_OR_EQUAL, '<='],
[T_IS_GREATER_OR_EQUAL, '>='],
[T_SPACESHIP, '<=>'],
'.',
[T_CONCAT_EQUAL, '.='],
[T_COALESCE, '??'],
[T_DOUBLE_ARROW, '=>'],
[T_OBJECT_OPERATOR, '->'],
[T_PAAMAYIM_NEKUDOTAYIM, '::'],
'?',
':',
'^',
'|',
[T_AND_EQUAL],
[T_COALESCE],
[T_CONCAT_EQUAL],
[T_DIV_EQUAL],
[T_DOUBLE_ARROW],
[T_IS_EQUAL],
[T_IS_GREATER_OR_EQUAL],
[T_IS_IDENTICAL],
[T_IS_NOT_EQUAL],
[T_IS_NOT_IDENTICAL],
[T_IS_SMALLER_OR_EQUAL],
[T_MINUS_EQUAL],
[T_MOD_EQUAL],
[T_MUL_EQUAL],
[T_OBJECT_OPERATOR],
[T_OR_EQUAL],
[T_PAAMAYIM_NEKUDOTAYIM],
[T_PLUS_EQUAL],
[T_POW],
[T_POW_EQUAL],
[T_SL],
[T_SL_EQUAL],
[T_SPACESHIP],
[T_SR],
[T_SR_EQUAL],
[T_XOR_EQUAL],
]
);
}
@@ -83,9 +83,9 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
/** @var int $referenceIndex */
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
if (!($tokens[$operatorIndex]->equals([T_OBJECT_OPERATOR, '->']) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STRING, 'self']))
&& !($tokens[$operatorIndex]->equals([T_DOUBLE_COLON, '::']) && $tokens[$referenceIndex]->equals([T_STATIC, 'static']))
if (!($tokens[$operatorIndex]->isGivenKind(T_OBJECT_OPERATOR) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this'], false))
&& !($tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->equals([T_STRING, 'self'], false))
&& !($tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->isGivenKind(T_STATIC))
) {
continue;
}
@@ -40,14 +40,19 @@ public function isRisky(): bool
return false;
}
public function getPriority(): int
{
return 6;
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 0; $index < $tokens->count(); $index++) {
if (!$tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$functionIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]);
$functionIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]);
if ($functionIndex === null || !$tokens[$functionIndex]->isGivenKind(T_FUNCTION)) {
continue;
}
@@ -68,11 +73,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
}
}
public function getPriority(): int
{
return 6;
}
/**
* @return string[]
*/
@@ -48,7 +48,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}
$functionIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_VARIABLE], [T_WHITESPACE], '=']);
$functionIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_ABSTRACT], [T_COMMENT], [T_FINAL], [T_PRIVATE], [T_PROTECTED], [T_PUBLIC], [T_STATIC], [T_WHITESPACE]]);
if ($functionIndex === null || !$tokens[$functionIndex]->isGivenKind(T_FUNCTION)) {
continue;
}
@@ -97,6 +97,11 @@ public function provideFixCases(): iterable
'<?php $a; /**foo*/',
];
yield [
'<?php $a; /** foo */',
'<?php $a; /** foo*/',
];
yield [
'<?php $a; /**** foo ****/',
'<?php $a; /****foo****/',
@@ -221,5 +221,26 @@ public function testSomething() {}
public function someDataProvider() {}
}',
];
foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
yield \sprintf('test function with %s modifier', $modifier) => [
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() {}
public function provideFooCases() {}
}', $modifier),
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider fooDataProvider
*/
%s function testFoo() {}
public function fooDataProvider() {}
}', $modifier),
];
}
}
}
@@ -150,5 +150,28 @@ public function someFunction() {}
public function someFunction2() {}
}',
];
foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
yield \sprintf('test function with %s modifier', $modifier) => [
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() {}
public function provideFooCases(): iterable {}
}
', $modifier),
\sprintf('<?php
class FooTest extends TestCase {
/**
* @dataProvider provideFooCases
*/
%s function testFoo() {}
public function provideFooCases() {}
}
', $modifier),
];
}
}
}
@@ -42,7 +42,9 @@ public function provideFixCases(): iterable
yield ['<?php namespace Foo; new STDClass();'];
yield ['<?php namespace STDClass; new Foo();'];
yield ['<?php $foo->STDCLASS();'];
yield ['<?php $foo->STDCLASS;'];
yield ['<?php Foo::STDCLASS();'];
yield ['<?php Foo::STDCLASS;'];
yield ['<?php function STDCLASS() { return 42; }; '];
yield ['<?php STDCLASS();'];
yield ['<?php \STDCLASS();'];
@@ -38,42 +38,57 @@ public function provideFixCases(): iterable
{
yield [
'<?php $foo = new Bar();',
'<?php $foo = new \Bar();',
'<?php $foo = new \\Bar();',
];
yield [
'<?php $foo = new Bar\Baz();',
'<?php $foo = new \Bar\Baz();',
'<?php $foo = new Bar\\Baz();',
'<?php $foo = new \\Bar\\Baz();',
];
yield [
'<?php $foo = Bar::NAME;',
'<?php $foo = \Bar::NAME;',
'<?php $foo = \\Bar::NAME;',
];
yield [
'<?php $foo = Bar\Baz::NAME;',
'<?php $foo = \Bar\Baz::NAME;',
'<?php $foo = Bar\\Baz::NAME;',
'<?php $foo = \\Bar\\Baz::NAME;',
];
yield [
'<?php $x = new Foo(); namespace Bar; $y = new \Baz();',
'<?php $x = new \Foo(); namespace Bar; $y = new \Baz();',
'<?php $x = new Foo(); namespace Bar; $y = new \\Baz();',
'<?php $x = new \\Foo(); namespace Bar; $y = new \\Baz();',
];
yield [
'<?php $x = \getcwd();',
'<?php $x = \\getcwd();',
];
yield [
'<?php
$a = new Foo\Bar();
$a = new Foo\\Bar();
$b = new Baz();
',
'<?php
$a = new \Foo\Bar();
$b = new \Baz();
$a = new \\Foo\\Bar();
$b = new \\Baz();
',
];
yield [
'<?php $foo = Bar::value();',
'<?php $foo = \\ Bar::value();',
];
yield [
'<?php $foo = /* comment */Bar::value();',
'<?php $foo = \\/* comment */Bar::value();',
];
yield [
'<?php $foo = /** comment */Bar::value();',
'<?php $foo = \\/** comment */Bar::value();',
];
}
}

0 comments on commit c9af0c0

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