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

New mutator for bcmath #678

Merged
merged 7 commits into from Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -60,7 +60,8 @@
"symfony/process": "3.4.2"
},
"require-dev": {
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7.5",
"ext-bcmath": "*"
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
},
"config": {
"platform": {
Expand Down
6 changes: 4 additions & 2 deletions composer.lock

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

3 changes: 2 additions & 1 deletion devTools/Dockerfile-php71-xdebug
@@ -1,6 +1,7 @@
FROM php:7.1-cli

RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug && \
docker-php-ext-install -j$(nproc) bcmath
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
RUN apt-get update && apt-get install -y --no-install-recommends \
expect git zip \
&& rm -rf /var/lib/apt/lists/*
Expand Down
3 changes: 2 additions & 1 deletion devTools/Dockerfile-php72-xdebug
@@ -1,6 +1,7 @@
FROM php:7.2-cli

RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug && \
docker-php-ext-install -j$(nproc) bcmath
RUN apt-get update && apt-get install -y --no-install-recommends \
expect git zip \
&& rm -rf /var/lib/apt/lists/*
Expand Down
3 changes: 2 additions & 1 deletion devTools/Dockerfile-php73-xdebug
@@ -1,6 +1,7 @@
FROM php:7.3-rc-cli

RUN pecl install xdebug-beta && docker-php-ext-enable xdebug
RUN pecl install xdebug-beta && docker-php-ext-enable xdebug && \
docker-php-ext-install -j$(nproc) bcmath
RUN apt-get update && apt-get install -y --no-install-recommends \
expect git zip \
&& rm -rf /var/lib/apt/lists/*
Expand Down
48 changes: 48 additions & 0 deletions resources/schema.json
Expand Up @@ -144,6 +144,54 @@
}
]
}
},
"BCMath": {
"type": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"settings": {
"type": "object",
"additionalProperties": false,
"properties": {
"bcadd": {
"type": "boolean"
},
"bccomp": {
"type": "boolean"
},
"bcdiv": {
"type": "boolean"
},
"bcmod": {
"type": "boolean"
},
"bcmul": {
"type": "boolean"
},
"bcpow": {
"type": "boolean"
},
"bcsub": {
"type": "boolean"
},
"bcsqrt": {
"type": "boolean"
},
"bcpowmod": {
"type": "boolean"
}
}
}
}
}
]
}
}
}
},
Expand Down
163 changes: 163 additions & 0 deletions src/Mutator/Extensions/BCMath.php
@@ -0,0 +1,163 @@
<?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\Extensions;

use Generator;
use Infection\Mutator\Util\Mutator;
use Infection\Mutator\Util\MutatorConfig;
use PhpParser\Node;

/**
* @internal
*/
final class BCMath extends Mutator
{
private $converters;

public function __construct(MutatorConfig $config)
{
parent::__construct($config);

$settings = $this->getSettings();

$this->setupConverters($settings);
}

/**
* @param Node|Node\Expr\FuncCall $node
*
* @return Node|Node[]|Generator
*/
public function mutate(Node $node)
{
yield from $this->converters[$node->name->toLowerString()]($node);
}

protected function mutatesNode(Node $node): bool
{
if (!$node instanceof Node\Expr\FuncCall || !$node->name instanceof Node\Name) {
return false;
}

$functionName = $node->name->toLowerString();

return isset($this->converters[$functionName]) && \function_exists($functionName);
}

private function setupConverters(array $functionsMap): void
{
$converters = [
'bcadd' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Plus::class)
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
)),
'bcdiv' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Div::class)
)),
'bcmod' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Mod::class)
)),
'bcmul' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Mul::class)
)),
'bcpow' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Pow::class)
)),
'bcsub' => $this->mapCheckingMinArgs(2, $this->mapCastToString(
$this->mapBinaryOperator(Node\Expr\BinaryOp\Minus::class)
)),
'bcsqrt' => $this->mapCheckingMinArgs(1, $this->mapCastToString(
$this->mapSquareRoots()
)),
'bcpowmod' => $this->mapCheckingMinArgs(3, $this->mapCastToString(
$this->mapPowerModulo()
)),
'bccomp' => $this->mapCheckingMinArgs(2,
$this->mapBinaryOperator(Node\Expr\BinaryOp\Spaceship::class)
),
];

$functionsToRemove = \array_filter($functionsMap, static function ($isOn) {
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
return !$isOn;
});

$this->converters = \array_diff_key($converters, $functionsToRemove);
}

private function mapCheckingMinArgs(int $minimumArgsCount, callable $converter)
{
return static function (Node\Expr\FuncCall $node) use ($minimumArgsCount, $converter): Generator {
if (\count($node->args) >= $minimumArgsCount) {
yield from $converter($node);
}
};
}

private function mapCastToString(callable $converter): callable
{
return static function (Node\Expr\FuncCall $node) use ($converter): Generator {
foreach ($converter($node) as $newNode) {
yield new Node\Expr\Cast\String_($newNode);
}
};
}

private function mapBinaryOperator(string $operator): callable
{
return static function (Node\Expr\FuncCall $node) use ($operator): Generator {
yield new $operator($node->args[0]->value, $node->args[1]->value);
};
}

private function mapSquareRoots(): callable
{
return static function (Node\Expr\FuncCall $node): Generator {
yield new Node\Expr\FuncCall(new Node\Name('\sqrt'), [$node->args[0]]);
};
}

private function mapPowerModulo(): callable
{
return static function (Node\Expr\FuncCall $node): Generator {
yield new Node\Expr\BinaryOp\Mod(
new Node\Expr\FuncCall(
new Node\Name('\pow'),
[$node->args[0], $node->args[1]]
),
$node->args[2]->value
);
};
}
}
7 changes: 7 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Expand Up @@ -232,6 +232,10 @@ final class MutatorProfile
Mutator\Unwrap\UnwrapUcWords::class,
];

public const EXTENSIONS = [
Mutator\Extensions\BCMath::class,
];

public const DEFAULT = [
maks-rafalko marked this conversation as resolved.
Show resolved Hide resolved
'@arithmetic',
'@boolean',
Expand Down Expand Up @@ -395,5 +399,8 @@ final class MutatorProfile
'UnwrapTrim' => Mutator\Unwrap\UnwrapTrim::class,
'UnwrapUcFirst' => Mutator\Unwrap\UnwrapUcFirst::class,
'UnwrapUcWords' => Mutator\Unwrap\UnwrapUcWords::class,

// Extensions
'BCMath' => Mutator\Extensions\BCMath::class,
];
}