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
42 changes: 25 additions & 17 deletions packages/Php/src/Rector/FuncCall/CountOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'count')) {
return null;
}

// check if it has some condition before already, if so, probably it's already handled
if ($node->getAttribute(self::ALREADY_CHANGED_ON_COUNT)) {
return null;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Ternary) {
if ($this->shouldSkip($node)) {
return null;
}

Expand All @@ -82,14 +72,16 @@ public function refactor(Node $node): ?Node

if ($this->isNullType($countedNode)) {
$identicalNode = new Identical($countedNode, $this->createNull());
$node->setAttribute(self::ALREADY_CHANGED_ON_COUNT, true);

$ternaryNode = new Ternary($identicalNode, new LNumber(0), $node);
} else {
$conditionNode = new BooleanOr(
$this->createFunction('is_array', [new Arg($countedNode)]),
new Instanceof_($countedNode, new FullyQualified('Countable'))
);
if ($this->isAtLeastPhpVersion('7.3')) {
$conditionNode = new FuncCall(new Node\Name('is_countable'), [new Arg($countedNode)]);
} else {
$conditionNode = new BooleanOr(
$this->createFunction('is_array', [new Arg($countedNode)]),
new Instanceof_($countedNode, new FullyQualified('Countable'))
);
}

$ternaryNode = new Ternary($conditionNode, $node, new LNumber(0));
}
Expand All @@ -99,4 +91,20 @@ public function refactor(Node $node): ?Node

return $ternaryNode;
}

private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isName($funcCall, 'count')) {
return true;
}

// check if it has some condition before already, if so, probably it's already handled
if ($funcCall->getAttribute(self::ALREADY_CHANGED_ON_COUNT)) {
return true;
}

$parentNode = $funcCall->getAttribute(AttributeKey::PARENT_NODE);

return $parentNode instanceof Ternary;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector;

use Rector\Php\Rector\FuncCall\CountOnNullRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @covers \Rector\Php\Rector\FuncCall\CountOnNullRector
*/
final class CountOnNullRectorTest extends AbstractRectorTestCase
{
public function test(): void
Expand All @@ -21,8 +23,8 @@ public function test(): void
]);
}

protected function getRectorClass(): string
protected function provideConfig(): string
{
return CountOnNullRector::class;
return __DIR__ . '/rector_with_php71.yaml';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @covers \Rector\Php\Rector\FuncCall\CountOnNullRector
*/
final class CountOnNullRectorWithPHP73Test extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/is_countable.php.inc']);
}

protected function provideConfig(): string
{
return __DIR__ . '/rector_with_php73.yaml';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector\Fixture;

class IsCountable
{
public function run($values)
{
$valueCount = count($values);
}
}

?>
-----
<?php

namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector\Fixture;

class IsCountable
{
public function run($values)
{
$valueCount = is_countable($values) ? count($values) : 0;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
php_version_features: '7.1'

services:
Rector\Php\Rector\FuncCall\CountOnNullRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
php_version_features: '7.3'

services:
Rector\Php\Rector\FuncCall\CountOnNullRector: ~
8 changes: 4 additions & 4 deletions src/Php/PhpVersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function __construct(?string $phpVersionFeatures)

public function provide(): string
{
if ($this->phpVersionFeatures) {
return $this->phpVersionFeatures;
}

// for tests
if (defined('PHPUNIT_COMPOSER_INSTALL') || defined('__PHPUNIT_PHAR__')) {
return '7.5';
}

if ($this->phpVersionFeatures) {
return $this->phpVersionFeatures;
}

return PHP_VERSION;
}

Expand Down