Skip to content

Commit

Permalink
AssignCoalesce mutator. Upgrade PHPParser to 4.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-rafalko committed Feb 17, 2019
1 parent 3b5b4b5 commit a6d73e6
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -43,7 +43,7 @@
"ext-libxml": "*",
"composer/xdebug-handler": "^1.3",
"justinrainbow/json-schema": "^5.2",
"nikic/php-parser": "^4.1",
"nikic/php-parser": "^4.2",
"ocramius/package-versions": "^1.2",
"padraic/phar-updater": "^1.0.4",
"pimple/pimple": "^3.2",
Expand Down
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions src/Mutator/Operator/AssignCoalesce.php
@@ -0,0 +1,64 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017-2019, 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\Operator;

use Infection\Mutator\Util\Mutator;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp\Coalesce;

/**
* @internal
*/
final class AssignCoalesce extends Mutator
{
/**
* Replaces "$array['a'] ??= 'otherValue';" with "$array['a'] = 'otherValue'"
*
* @param Coalesce $node
*
* @return Assign
*/
public function mutate(Node $node)
{
return new Assign($node->var, $node->expr, $node->getAttributes());
}

protected function mutatesNode(Node $node): bool
{
return $node instanceof Coalesce;
}
}
2 changes: 2 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Expand Up @@ -144,6 +144,7 @@ final class MutatorProfile
];

public const OPERATOR = [
Mutator\Operator\AssignCoalesce::class,
Mutator\Operator\Break_::class,
Mutator\Operator\Coalesce::class,
Mutator\Operator\Continue_::class,
Expand Down Expand Up @@ -310,6 +311,7 @@ final class MutatorProfile
'OneZeroFloat' => Mutator\Number\OneZeroFloat::class,

//Operator
'AssignCoalesce' => Mutator\Operator\AssignCoalesce::class,
'Break_' => Mutator\Operator\Break_::class,
'Continue_' => Mutator\Operator\Continue_::class,
'Throw_' => Mutator\Operator\Throw_::class,
Expand Down
97 changes: 97 additions & 0 deletions tests/Mutator/Operator/AssignCoalesceTest.php
@@ -0,0 +1,97 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017-2019, 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\Tests\Mutator\Operator;

use Infection\Tests\Mutator\AbstractMutatorTestCase;

/**
* @internal
*/
final class AssignCoalesceTest extends AbstractMutatorTestCase
{
/**
* @dataProvider provideMutationCases
*/
public function test_mutator($input, $expected = null): void
{
$this->doTest($input, $expected);
}

public function provideMutationCases(): \Generator
{
yield 'Mutate coalesce when right part is a scalar value' => [
<<<'PHP'
<?php
$a['value'] ??= 'otherValue';
PHP
,
<<<'PHP'
<?php
$a['value'] = 'otherValue';
PHP
];

yield 'Mutate coalesce when right part is an expression' => [
<<<'PHP'
<?php
$a['value'] ??= 'other' . ' Value';
PHP
,
<<<'PHP'
<?php
$a['value'] = 'other' . ' Value';
PHP
];

yield 'Mutate coalesce when right part is a variable' => [
<<<'PHP'
<?php
$a['value'] ??= $var;
PHP
,
<<<'PHP'
<?php
$a['value'] = $var;
PHP
];
}
}

0 comments on commit a6d73e6

Please sign in to comment.