mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 16:02:23 +02:00
[Bugfix] IsCountableRector
& IsIterableRector
should first check method availability
This commit is contained in:
parent
1dd69eedd6
commit
03b4bdd1b9
@ -61,10 +61,10 @@ CODE_SAMPLE
|
||||
|
||||
private function shouldSkip(): bool
|
||||
{
|
||||
if ($this->isAtLeastPhpVersion('7.3')) {
|
||||
if (function_exists('is_countable')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! function_exists('is_countable');
|
||||
return $this->isAtLeastPhpVersion('7.3');
|
||||
}
|
||||
}
|
||||
|
@ -61,10 +61,10 @@ CODE_SAMPLE
|
||||
|
||||
private function shouldSkip(): bool
|
||||
{
|
||||
if ($this->isAtLeastPhpVersion('7.1')) {
|
||||
if (function_exists('is_iterable')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! function_exists('is_iterable');
|
||||
return $this->isAtLeastPhpVersion('7.1');
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
function isCountable()
|
||||
{
|
||||
is_array($foo) || $foo instanceof Countable;
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Php\Tests\Rector\BinaryOp\IsCountable\Fixture;
|
||||
|
||||
use Countable;
|
||||
|
||||
class PolyfillFunction
|
||||
{
|
||||
public function run($foo)
|
||||
{
|
||||
return is_array($foo) || $foo instanceof Countable;
|
||||
}
|
||||
}
|
||||
|
||||
// dummy
|
||||
if (! function_exists('is_countable')) {
|
||||
function is_countable($args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Php\Tests\Rector\BinaryOp\IsCountable\Fixture;
|
||||
|
||||
use Countable;
|
||||
|
||||
class PolyfillFunction
|
||||
{
|
||||
public function run($foo)
|
||||
{
|
||||
return is_countable($foo);
|
||||
}
|
||||
}
|
||||
|
||||
// dummy
|
||||
if (! function_exists('is_countable')) {
|
||||
function is_countable($args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -9,7 +9,13 @@ final class IsCountableRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
// That method can be provided by `symfony/polyfill-php73` package,
|
||||
// or not exists as it's run on PHP < 7.3
|
||||
if (! function_exists('is_countable')) {
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture71.php.inc']);
|
||||
} else {
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture73.php.inc']);
|
||||
}
|
||||
}
|
||||
|
||||
public function getRectorClass(): string
|
||||
|
@ -0,0 +1,48 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Php\Tests\Rector\BinaryOp\IsCountableRector;
|
||||
|
||||
use Rector\Configuration\Option;
|
||||
use Rector\Php\Rector\BinaryOp\IsCountableRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
|
||||
final class PolyfillRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $originalPhpVersionFeaturesParameter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->parameterProvider = self::$container->get(ParameterProvider::class);
|
||||
$this->originalPhpVersionFeaturesParameter = $this->parameterProvider->provideParameter(
|
||||
Option::PHP_VERSION_FEATURES
|
||||
);
|
||||
|
||||
$this->parameterProvider->changeParameter(Option::PHP_VERSION_FEATURES, '7.2');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$this->parameterProvider->changeParameter(
|
||||
Option::PHP_VERSION_FEATURES,
|
||||
$this->originalPhpVersionFeaturesParameter
|
||||
);
|
||||
}
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/polyfill_function.php.inc']);
|
||||
}
|
||||
|
||||
public function getRectorClass(): string
|
||||
{
|
||||
return IsCountableRector::class;
|
||||
}
|
||||
}
|
@ -38,7 +38,7 @@ final class PolyfillRectorTest extends AbstractRectorTestCase
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/polyfil_function.php.inc']);
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/polyfill_function.php.inc']);
|
||||
}
|
||||
|
||||
public function getRectorClass(): string
|
||||
|
Loading…
x
Reference in New Issue
Block a user