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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"symfony/console": "^6.4.24",
"symfony/filesystem": "^7.4",
"symfony/finder": "^6.4",
"symfony/polyfill-php84": "^1.38",
"symfony/process": "^7.4",
"symplify/easy-parallel": "^11.2.2",
"symplify/rule-doc-generator-contracts": "^11.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector;

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

final class AddArrayAnyAllClosureParamTypeRectorTest 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,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function arrayAllClosure()
{
/** @var int[] $numbers */
$numbers = doSomething();

return array_all($numbers, function ($number): bool {
return $number > 0;
});
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function arrayAllClosure()
{
/** @var int[] $numbers */
$numbers = doSomething();

return array_all($numbers, function (int $number): bool {
return $number > 0;
});
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function arrayAnyArrowFunction()
{
/** @var string[] $items */
$items = doSomething();

return array_any($items, fn ($item): bool => $item !== '');
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function arrayAnyArrowFunction()
{
/** @var string[] $items */
$items = doSomething();

return array_any($items, fn (string $item): bool => $item !== '');
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function skipAlreadyTyped()
{
/** @var string[] $items */
$items = doSomething();

return array_any($items, fn (string $item): bool => $item !== '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector\Fixture;

function skipMixedArray($items)
{
return array_any($items, fn ($item): bool => $item !== '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddArrayAnyAllClosureParamTypeRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_84);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayAllClosure(): bool
{
/** @var int[] $numbers */
$numbers = [1, 2, 3];

return array_all($numbers, function (?int $number): bool {
return $number > 0;
});
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayAllClosure(): bool
{
/** @var int[] $numbers */
$numbers = [1, 2, 3];

return array_all($numbers, function (int $number): bool {
return $number > 0;
});
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayAnyArrowFunction(): bool
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_any($items, fn (?string $item): bool => $item !== '');
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayAnyArrowFunction(): bool
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_any($items, fn (string $item): bool => $item !== '');
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayFind(): ?string
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_find($items, fn (?string $item): bool => $item !== '');
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function arrayFind(): ?string
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_find($items, fn (string $item): bool => $item !== '');
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function skipAlreadyNonNullable(): bool
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_any($items, fn (string $item): bool => $item !== '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function skipArrayFilter(): array
{
/** @var string[] $items */
$items = ['a', 'b'];

return array_filter($items, fn (?string $item): bool => $item !== '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector\Fixture;

function skipNullableItemType(): bool
{
/** @var array<string|null> $items */
$items = ['a', null];

return array_any($items, fn (?string $item): bool => $item !== '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector;

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

final class NarrowArrayAnyAllNullableParamTypeRectorTest 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,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\FuncCall\NarrowArrayAnyAllNullableParamTypeRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(NarrowArrayAnyAllNullableParamTypeRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_84);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -233,25 +234,18 @@ private function hasParameter(New_ $new, string $parameterName): bool
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors(
$extendedMethodReflection->getVariants()
);

foreach ($extendedParametersAcceptor->getParameters() as $extendedParameterReflection) {
if ($extendedParameterReflection->getName() === $parameterName) {
return true;
}
}

return false;
return array_any(
$extendedParametersAcceptor->getParameters(),
fn (ExtendedParameterReflection $extendedParameterReflection): bool => $extendedParameterReflection->getName() === $parameterName
);
}

private function hasArgument(New_ $new, string $argumentName): bool
{
foreach ($new->getArgs() as $arg) {
if ($arg->name instanceof Identifier && $arg->name->toString() === $argumentName) {
return true;
}
}

return false;
return array_any(
$new->getArgs(),
fn (Arg $arg): bool => $arg->name instanceof Identifier && $arg->name->toString() === $argumentName
);
}

private function resolveExceptionArgumentPosition(Name $exceptionName): ?int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -100,12 +101,6 @@ public function refactor(Node $node): ?array

private function hasArraySpread(FuncCall $funcCall): bool
{
foreach ($funcCall->getArgs() as $arg) {
if ($arg->unpack) {
return true;
}
}

return false;
return array_any($funcCall->getArgs(), fn (Arg $arg): bool => $arg->unpack);
}
}
Loading
Loading