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

ExpressionRepeat mutator #1215

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"PowEqual": { "$ref": "#/definitions/default-mutator-config" },
"ShiftLeft": { "$ref": "#/definitions/default-mutator-config" },
"ShiftRight": { "$ref": "#/definitions/default-mutator-config" },
"ExpressionRepeat": { "$ref": "#/definitions/default-mutator-config" },
"RoundingFamily": { "$ref": "#/definitions/default-mutator-config" },
"ArrayItem": { "$ref": "#/definitions/default-mutator-config" },
"EqualIdentical": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
80 changes: 80 additions & 0 deletions src/Mutator/Augmentation/ExpressionRepeat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, 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\Augmentation;

use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\PhpParser\Visitor\ImpureExpressionVisitor;
use PhpParser\Node;

/**
* @internal
*/
final class ExpressionRepeat implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
'Duplicates an expression statement, provided it can have side-effects.',
MutatorCategory::SEMANTIC_ADDITION,
null
);
}

/**
* @param Node\Stmt\Expression $node
*
* @return iterable<array<Node\Stmt\Expression>>
*/
public function mutate(Node $node): iterable
{
yield [$node, $node];
}

public function canMutate(Node $node): bool
{
if (!$node instanceof Node\Stmt\Expression) {
return false;
}

// Skip statements with almost no side effects.
return $node->getAttribute(ImpureExpressionVisitor::HAS_NODES_WITH_SIDE_EFFECTS_KEY);
}
}
9 changes: 9 additions & 0 deletions src/Mutator/ProfileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final class ProfileList

public const ALL_PROFILES = [
'@arithmetic' => self::ARITHMETIC_PROFILE,
'@augmentation' => self::AUGMENTATION_PROFILE,
'@boolean' => self::BOOLEAN_PROFILE,
'@cast' => self::CAST_PROFILE,
'@conditional_boundary' => self::CONDITIONAL_BOUNDARY_PROFILE,
Expand Down Expand Up @@ -93,6 +94,10 @@ final class ProfileList
Mutator\Arithmetic\ShiftRight::class,
];

public const AUGMENTATION_PROFILE = [
Mutator\Augmentation\ExpressionRepeat::class,
];

public const BOOLEAN_PROFILE = [
Mutator\Boolean\ArrayItem::class,
// EqualIdentical disabled from the default boolean profile
Expand Down Expand Up @@ -269,6 +274,7 @@ final class ProfileList

public const DEFAULT_PROFILE = [
'@arithmetic',
'@augmentation',
'@boolean',
'@cast',
'@conditional_boundary',
Expand Down Expand Up @@ -311,6 +317,9 @@ final class ProfileList
'ShiftLeft' => Mutator\Arithmetic\ShiftLeft::class,
'ShiftRight' => Mutator\Arithmetic\ShiftRight::class,

// Augmentation
'ExpressionRepeat' => Mutator\Augmentation\ExpressionRepeat::class,

// Boolean
'ArrayItem' => Mutator\Boolean\ArrayItem::class,
'EqualIdentical' => Mutator\Boolean\EqualIdentical::class,
Expand Down
2 changes: 2 additions & 0 deletions src/PhpParser/NodeTraverserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Infection\PhpParser\Visitor\IgnoreNode\AbstractMethodIgnorer;
use Infection\PhpParser\Visitor\IgnoreNode\InterfaceIgnorer;
use Infection\PhpParser\Visitor\IgnoreNode\NodeIgnorer;
use Infection\PhpParser\Visitor\ImpureExpressionVisitor;
use Infection\PhpParser\Visitor\NonMutableNodesIgnorerVisitor;
use Infection\PhpParser\Visitor\ParentConnectorVisitor;
use Infection\PhpParser\Visitor\ReflectionVisitor;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function create(NodeVisitor $mutationVisitor, array $nodeIgnorers): NodeT
$traverser->addVisitor(new ParentConnectorVisitor());
$traverser->addVisitor(new FullyQualifiedClassNameVisitor());
$traverser->addVisitor(new ReflectionVisitor());
$traverser->addVisitor(new ImpureExpressionVisitor());
$traverser->addVisitor($mutationVisitor);

return $traverser;
Expand Down
135 changes: 135 additions & 0 deletions src/PhpParser/Visitor/ImpureExpressionVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, 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\PhpParser\Visitor;

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;

/**
* @internal
*/
final class ImpureExpressionVisitor extends NodeVisitorAbstract
{
public const HAS_NODES_WITH_SIDE_EFFECTS_KEY = 'withSideEffects';

private const INITIAL_DEPTH = 0;

/**
* @var bool[]
*/
private $seenMethodCall = [];

/**
* @var bool[]
*/
private $seenNonMethodCall = [];

/**
* @var int
*/
private $depth = self::INITIAL_DEPTH;

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Stmt\Expression) {
++$this->depth;
$this->seenMethodCall[$this->depth] = false;
$this->seenNonMethodCall[$this->depth] = false;

return null;
}

if ($this->depth === self::INITIAL_DEPTH) {
// Not inside a statement.
return null;
}

if ($this->seenMethodCall[$this->depth] === false) {
$this->seenMethodCall[$this->depth] = self::nodeIsDynamicCall($node);
}

if ($this->seenNonMethodCall[$this->depth] === false) {
$this->seenNonMethodCall[$this->depth] = self::nodeIsNonMethodCall($node);
}

return null;
}

public function leaveNode(Node $node): ?Node
{
if ($node instanceof Node\Stmt\Expression) {
--$this->depth;

$node->setAttribute(
self::HAS_NODES_WITH_SIDE_EFFECTS_KEY,
array_pop($this->seenMethodCall) && !array_pop($this->seenNonMethodCall)
);
}

return null;
}

private static function nodeIsDynamicCall(Node $node): bool
{
if ($node instanceof Node\Expr\MethodCall) {
return true;
}

if ($node instanceof Node\Expr\FuncCall && !$node->name instanceof Node\Name) {
return true;
}

return false;
}

private static function nodeIsNonMethodCall(Node $node): bool
{
if ($node instanceof Node\Expr\StaticCall) {
return true;
}

if ($node instanceof Node\Expr\New_) {
return true;
}

// Only named function calls are accounted here.
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) {
return true;
}

return false;
}
}