[CodeQualityStrict] Skip MoveVariableDeclarationNearReferenceRector on pass impure function (#6090)

This commit is contained in:
Abdul Malik Ikhsan 2021-04-11 00:45:46 +07:00 committed by GitHub
parent f82a8dc995
commit 723bd2b1b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 5 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
final class SkipPassBcdiv
{
public function run($data, $options)
{
$a = bcscale(3);
echo bcdiv('105', '6.55957');
echo $a;
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
final class SkipPassJsonLastError
{
public function run($data, $options)
{
$result = json_encode($data, $options, 512);
if (json_last_error()) {
throw new \Exception();
}
echo $result;
}
}

View File

@ -9,7 +9,7 @@ class StaticCallNextException
function myMethod()
{
$var = do_something();
if (rand(0, 1)) {
if (mktime() === false) {
throw MyException::notFound();
}
echo $var;
@ -28,7 +28,7 @@ class StaticCallNextException
{
function myMethod()
{
if (rand(0, 1)) {
if (mktime() === false) {
throw MyException::notFound();
}
$var = do_something();

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Rector\CodeQualityStrict\Rector\Variable;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
@ -28,6 +27,7 @@ use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\While_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\SideEffect\PureFunctionDetector;
use Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -43,9 +43,15 @@ final class MoveVariableDeclarationNearReferenceRector extends AbstractRector
*/
private $scopeAwareNodeFinder;
public function __construct(ScopeAwareNodeFinder $scopeAwareNodeFinder)
/**
* @var PureFunctionDetector
*/
private $pureFunctionDetector;
public function __construct(ScopeAwareNodeFinder $scopeAwareNodeFinder, PureFunctionDetector $pureFunctionDetector)
{
$this->scopeAwareNodeFinder = $scopeAwareNodeFinder;
$this->pureFunctionDetector = $pureFunctionDetector;
}
public function getRuleDefinition(): RuleDefinition
@ -297,7 +303,7 @@ CODE_SAMPLE
return false;
}
return Strings::startsWith($funcName, 'ob_');
return ! $this->pureFunctionDetector->detect($n);
});
}

View File

@ -100,6 +100,12 @@ final class PureFunctionDetector
// ftp
'ftp_close',
// bcmath
'bcscale', 'bcdiv',
// json
'json_encode', 'json_decode', 'json_last_error',
];
/**