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
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class BothIntegers
{
public function run(int $value)
{
return in_array($value, [1, 2, 3]);
}
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class BothIntegers
{
public function run(int $value)
{
return in_array($value, [1, 2, 3], true);
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class BothStrings
{
public function run(string $value)
{
return in_array($value, ['one', 'two', 'three']);
}
}
?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class BothStrings
{
public function run(string $value)
{
return in_array($value, ['one', 'two', 'three'], true);
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class SkipBothMixedTypes
{
/**
* @param mixed[] $items
*/
public function run(mixed $value, array $items)
{
return in_array($value, $items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class SkipDifferentTypes
{
public function run()
{
$items = ['10', '20', '30'];
return in_array(10, $items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class SkipDocblockArray
{
/**
* @param string[] $items
*/
public function run(string $value, array $items)
{
return in_array($value, $items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\Fixture;

class SkipMixedType
{
public function run(mixed $value)
{
$items = ['10', '20', '30'];
return in_array($value, $items);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class StrictInArrayRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\FuncCall\StrictInArrayRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([StrictInArrayRector::class]);
94 changes: 94 additions & 0 deletions rules/CodingStyle/Rector/FuncCall/StrictInArrayRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\StrictInArrayRector\StrictInArrayRectorTest
*/
final class StrictInArrayRector extends AbstractRector
{
public function __construct(
private readonly TypeComparator $typeComparator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Set in_array strict to true when defined on similar type', [
new CodeSample(
<<<'CODE_SAMPLE'
class BothStrings
{
public function run(string $value)
{
return in_array($value, ['one', 'two', 'three']);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class BothStrings
{
public function run(string $value)
{
return in_array($value, ['one', 'two', 'three'], true);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'in_array')) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
if (count($args) !== 2) {
return null;
}

$firstArgType = $this->nodeTypeResolver->getNativeType($args[0]->value);
$secondArgType = $this->nodeTypeResolver->getNativeType($args[1]->value);

if (! $secondArgType->isArray()->yes()) {
return null;
}

if ($this->typeComparator->isSubtype($secondArgType->getIterableValueType(), $firstArgType)) {
$node->args[] = new Arg($this->nodeFactory->createTrue());
return $node;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function refactor(Node $node): ?Node

if (in_array(
$constantFilterName,
['FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOL']
['FILTER_VALIDATE_BOOLEAN', 'FILTER_VALIDATE_BOOL'],
true
) && $valueType->isBoolean()
->yes()) {
return $firstArgValue;
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Level/CodingStyleLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
use Rector\CodingStyle\Rector\FuncCall\StrictInArrayRector;
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\Property\SplitGroupedPropertiesRector;
Expand Down Expand Up @@ -75,6 +76,7 @@ final class CodingStyleLevel
CallUserFuncToMethodCallRector::class,
FuncGetArgsToVariadicParamRector::class,
StrictArraySearchRector::class,
StrictInArrayRector::class,
UseClassKeywordForClassNameResolutionRector::class,
SplitGroupedPropertiesRector::class,
SplitGroupedClassConstantsRector::class,
Expand Down