Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class DualCheckToAble
final class IsArrayAndDualCheckToAble
{
/**
* @var NameResolver
Expand Down
23 changes: 16 additions & 7 deletions packages/Php/src/Rector/BinaryOp/IsCountableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Rector\Php\DualCheckToAble;
use Rector\Php\IsArrayAndDualCheckToAble;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class IsCountableRector extends AbstractRector
{
/**
* @var DualCheckToAble
* @var IsArrayAndDualCheckToAble
*/
private $dualCheckToAble;
private $isArrayAndDualCheckToAble;

public function __construct(DualCheckToAble $dualCheckToAble)
public function __construct(IsArrayAndDualCheckToAble $isArrayAndDualCheckToAble)
{
$this->dualCheckToAble = $dualCheckToAble;
$this->isArrayAndDualCheckToAble = $isArrayAndDualCheckToAble;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -52,10 +52,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isAtLeastPhpVersion('7.3')) {
if ($this->shouldSkip()) {
return null;
}

return $this->dualCheckToAble->processBooleanOr($node, 'Countable', 'is_countable') ?: $node;
return $this->isArrayAndDualCheckToAble->processBooleanOr($node, 'Countable', 'is_countable') ?: $node;
}

private function shouldSkip(): bool
{
if ($this->isAtLeastPhpVersion('7.3')) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't those check be opposite?

You can have polyfill with that method on even PHP 7.0, so first it should check for polyfill/method availability, then check PHP version, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right of course!

Care for you PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return false;
}

return ! function_exists('is_countable');
}
}
25 changes: 19 additions & 6 deletions packages/Php/src/Rector/BinaryOp/IsIterableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use Rector\Php\DualCheckToAble;
use Rector\Php\IsArrayAndDualCheckToAble;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class IsIterableRector extends AbstractRector
{
/**
* @var DualCheckToAble
* @var IsArrayAndDualCheckToAble
*/
private $dualCheckToAble;
private $isArrayAndDualCheckToAble;

public function __construct(DualCheckToAble $dualCheckToAble)
public function __construct(IsArrayAndDualCheckToAble $isArrayAndDualCheckToAble)
{
$this->dualCheckToAble = $dualCheckToAble;
$this->isArrayAndDualCheckToAble = $isArrayAndDualCheckToAble;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -52,6 +52,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->dualCheckToAble->processBooleanOr($node, 'Traversable', 'is_iterable') ?: $node;
if ($this->shouldSkip()) {
return null;
}

return $this->isArrayAndDualCheckToAble->processBooleanOr($node, 'Traversable', 'is_iterable') ?: $node;
}

private function shouldSkip(): bool
{
if ($this->isAtLeastPhpVersion('7.1')) {
return false;
}

return ! function_exists('is_iterable');
}
}
37 changes: 30 additions & 7 deletions packages/Php/src/Rector/FuncCall/ArrayKeyFirstLastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
*/
final class ArrayKeyFirstLastRector extends AbstractRector
{
/**
* @var string
*/
private const ARRAY_KEY_FIRST = 'array_key_first';

/**
* @var string
*/
private const ARRAY_KEY_LAST = 'array_key_last';

/**
* @var string[]
*/
private $previousToNewFunctions = [
'reset' => 'array_key_first',
'end' => 'array_key_last',
'reset' => self::ARRAY_KEY_FIRST,
'end' => self::ARRAY_KEY_LAST,
];

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -70,11 +80,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isAtLeastPhpVersion('7.3')) {
return null;
}

if (! $this->isNames($node, ['reset', 'end'])) {
if ($this->shouldSkip($node)) {
return null;
}

Expand Down Expand Up @@ -122,4 +128,21 @@ private function getNextExpression(Node $node): ?Node

return $currentExpression->getAttribute(AttributeKey::NEXT_NODE);
}

private function shouldSkip(FuncCall $funcCall): bool
{
if ($this->isAtLeastPhpVersion('7.3')) {
return false;
}

if (function_exists(self::ARRAY_KEY_FIRST) && function_exists(self::ARRAY_KEY_LAST)) {
return false;
}

if ($this->isNames($funcCall, ['reset', 'end'])) {
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Php\Tests\Rector\BinaryOp\IsIterableRector\Fixture;

use Traversable;

class PolyfillFunction
{
public function run($foo)
{
return is_array($foo) || $foo instanceof Traversable;
}
}

// dummy
if (! function_exists('is_iterable')) {
function is_iterable($args)
{
return true;
}
}

?>
-----
<?php

namespace Rector\Php\Tests\Rector\BinaryOp\IsIterableRector\Fixture;

use Traversable;

class PolyfillFunction
{
public function run($foo)
{
return is_iterable($foo);
}
}

// dummy
if (! function_exists('is_iterable')) {
function is_iterable($args)
{
return true;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Rector\Php\Tests\Rector\BinaryOp\IsIterableRector;

use Rector\Configuration\Option;
use Rector\Php\Rector\BinaryOp\IsIterableRector;
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.0');
}

protected function tearDown(): void
{
parent::tearDown();

$this->parameterProvider->changeParameter(
Option::PHP_VERSION_FEATURES,
$this->originalPhpVersionFeaturesParameter
);
}

public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/polyfil_function.php.inc']);
}

public function getRectorClass(): string
{
return IsIterableRector::class;
}
}
5 changes: 5 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ final class Option
*/
public const KERNEL_CLASS_PARAMETER = 'kernel_class';

/**
* @var string
*/
public const PHP_VERSION_FEATURES = 'php_version_features';

/**
* @var string
*/
Expand Down
17 changes: 11 additions & 6 deletions src/Php/PhpVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

namespace Rector\Php;

use Rector\Configuration\Option;
use Symplify\PackageBuilder\Parameter\ParameterProvider;

final class PhpVersionProvider
{
/**
* @var string|null
* @var ParameterProvider
*/
private $phpVersionFeatures;
private $parameterProvider;

public function __construct(?string $phpVersionFeatures)
public function __construct(ParameterProvider $parameterProvider)
{
$this->phpVersionFeatures = $phpVersionFeatures;
$this->parameterProvider = $parameterProvider;
}

public function provide(): string
{
if ($this->phpVersionFeatures) {
return $this->phpVersionFeatures;
/** @var string|null $phpVersionFeatures */
$phpVersionFeatures = $this->parameterProvider->provideParameter(Option::PHP_VERSION_FEATURES);
if ($phpVersionFeatures !== null) {
return $phpVersionFeatures;
}

// for tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected function setUp(): void
{
parent::setUp();

/** @var ParameterProvider $parameterProvider */
$parameterProvider = self::$container->get(ParameterProvider::class);
$parameterProvider->changeParameter(Option::KERNEL_CLASS_PARAMETER, SomeKernelClass::class);
}
Expand Down