Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement UnwrapArrayReplace mutator #536

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ abstract class AbstractUnwrapMutator extends Mutator
*/
final public function mutate(Node $node)
{
return $node->args[$this->getParameterIndex()];
foreach ($this->getParameterIndex($node) as $index) {
yield $node->args[$index];
}
}

abstract protected function getFunctionName(): string;

abstract protected function getParameterIndex(): int;
abstract protected function getParameterIndex(Node $node): \Generator;

final protected function mutatesNode(Node $node): bool
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}

if (!array_key_exists($this->getParameterIndex(), $node->args)) {
return false;
foreach ($this->getParameterIndex($node) as $index) {
if (!array_key_exists($index, $node->args)) {
return false;
}
}

return $node->name->toLowerString() === strtolower($this->getFunctionName());
Expand Down
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_filter';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayFlip.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_flip';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_map';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 1;
yield 1;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayReduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_reduce';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 2;
yield 2;
}
}
54 changes: 54 additions & 0 deletions src/Mutator/Unwrap/UnwrapArrayReplace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017-2018, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
final class UnwrapArrayReplace extends AbstractUnwrapMutator
{
protected function getFunctionName(): string
{
return 'array_replace';
}

protected function getParameterIndex(Node $node): \Generator
{
yield from array_keys($node->args);
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayReverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_reverse';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapStrRepeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'str_repeat';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
2 changes: 2 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ final class MutatorProfile
Mutator\Unwrap\UnwrapArrayFlip::class,
Mutator\Unwrap\UnwrapArrayMap::class,
Mutator\Unwrap\UnwrapArrayReduce::class,
Mutator\Unwrap\UnwrapArrayReplace::class,
Mutator\Unwrap\UnwrapArrayReverse::class,
];

Expand Down Expand Up @@ -319,6 +320,7 @@ final class MutatorProfile
'UnwrapArrayFlip' => Mutator\Unwrap\UnwrapArrayFlip::class,
'UnwrapArrayMap' => Mutator\Unwrap\UnwrapArrayMap::class,
'UnwrapArrayReduce' => Mutator\Unwrap\UnwrapArrayReduce::class,
'UnwrapArrayReplace' => Mutator\Unwrap\UnwrapArrayReplace::class,
'UnwrapArrayReverse' => Mutator\Unwrap\UnwrapArrayReverse::class,
'UnwrapStrRepeat' => Mutator\Unwrap\UnwrapStrRepeat::class,
];
Expand Down