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

Mutator: AssignCoalesce. Upgrade PHPParser to 4.2.1 #641

Merged
merged 3 commits into from Feb 18, 2019
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
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.1",
"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
106 changes: 106 additions & 0 deletions tests/Mutator/Operator/AssignCoalesceTest.php
@@ -0,0 +1,106 @@
<?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
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
{
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
];

yield 'Does not mutate coalesce binary operator' => [
<<<'PHP'
<?php

$a['value'] = $foo ?? $bar;
PHP
,
];
}
}