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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"rules",
"src"
],
"Rector\\Utils\\": "utils"
"Rector\\Utils\\": "utils",
"Rector\\Utils\\PHPStan\\": "utils/phpstan/src"
},
"files": [
"src/functions/node_helper.php"
Expand All @@ -86,6 +87,7 @@
"tests"
],
"Rector\\Utils\\Tests\\": "utils-tests",
"Rector\\Utils\\PHPStan\\Tests\\": "utils/phpstan/tests",
"E2e\\Parallel\\Reflection\\Resolver\\": [
"e2e/parallel-reflection-resolver/src/",
"e2e/no-parallel-reflection-resolver/src"
Expand Down
20 changes: 20 additions & 0 deletions config/set/php-polyfills.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
use Rector\Php73\Rector\FuncCall\ArrayKeyFirstLastRector;
use Rector\Php80\Rector\Identical\StrEndsWithRector;
use Rector\Php80\Rector\Identical\StrStartsWithRector;
use Rector\Php80\Rector\NotIdentical\MbStrContainsRector;
use Rector\Php80\Rector\NotIdentical\StrContainsRector;
use Rector\Php80\Rector\Ternary\GetDebugTypeRector;
use Rector\Php83\Rector\BooleanAnd\JsonValidateRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector;
use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector;
use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector;
use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector;

// @note longer rule registration must be used here, to separate from withRules() from root rector.php

Expand All @@ -22,5 +30,17 @@
StrStartsWithRector::class,
StrEndsWithRector::class,
StrContainsRector::class,
MbStrContainsRector::class,

// PHP 8.3
JsonValidateRector::class,
AddOverrideAttributeToOverriddenMethodsRector::class,

// PHP 8.4
ForeachToArrayAllRector::class,
ForeachToArrayAnyRector::class,
ForeachToArrayFindRector::class,
ForeachToArrayFindKeyRector::class,
DeprecatedAnnotationToDeprecatedAttributeRector::class,
]);
};
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ includes:
- vendor/symplify/phpstan-rules/config/symplify-rules.neon
- vendor/symplify/phpstan-rules/config/rector-rules.neon

services:
-
class: Rector\Utils\PHPStan\Rule\RegisterRelatedPolyfillRectorRule
arguments:
polyfillConfigFilePath: %currentWorkingDirectory%/config/set/php-polyfills.php
tags:
- phpstan.rules.rule

parameters:
level: 8

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<directory>tests</directory>
<directory>rules-tests</directory>
<directory>utils-tests</directory>
<directory>utils/phpstan/tests</directory>
<exclude>rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Source/SomeAbstractTest.php</exclude>
</testsuite>
</testsuites>
Expand Down
12 changes: 4 additions & 8 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,10 @@ private function doesMethodExistInTrait(ClassMethod $classMethod, string $classM

$traits = $this->astResolver->parseClassReflectionTraits($classReflection);
$className = $classReflection->getName();

foreach ($traits as $trait) {
if ($this->isUsedByTrait($trait, $classMethodName, $className)) {
return true;
}
}

return false;
return array_any(
$traits,
fn (Trait_ $trait): bool => $this->isUsedByTrait($trait, $classMethodName, $className)
);
}

private function isUsedByTrait(Trait_ $trait, string $classMethodName, string $className): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,6 @@ private function isInPropertyPromotedParams(Class_ $class, PropertyFetch|StaticP
/** @var string $propertyName */
$propertyName = $this->getName($propertyFetch);
$params = $this->promotedPropertyResolver->resolveFromClass($class);

foreach ($params as $param) {
if ($this->isName($param, $propertyName)) {
return true;
}
}

return false;
return array_any($params, fn (Node $param): bool => $this->isName($param, $propertyName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php55\Rector\String_;

use Deprecated;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
Expand All @@ -23,9 +24,7 @@
*/
final class StringClassNameToClassConstantRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
{
/**
* @deprecated since 2.2.12. Default behavior now.
*/
#[Deprecated(message: 'since 2.2.12. Default behavior now.')]
public const string SHOULD_KEEP_PRE_SLASH = 'should_keep_pre_slash';

/**
Expand Down
11 changes: 4 additions & 7 deletions rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ public function hasPhpAttributes(
Property | ClassLike | ClassMethod | Function_ | Param $node,
array $attributeClasses
): bool {
foreach ($attributeClasses as $attributeClass) {
if ($this->hasPhpAttribute($node, $attributeClass)) {
return true;
}
}

return false;
return array_any(
$attributeClasses,
fn (string $attributeClass): bool => $this->hasPhpAttribute($node, $attributeClass)
);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions rules/Php80/NodeResolver/SwitchExprsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ private function isValidCase(Case_ $case): bool

private function areCasesValid(Switch_ $newSwitch): bool
{
foreach ($newSwitch->cases as $case) {
if (! $this->isValidCase($case)) {
return false;
}
}

return true;
return array_all($newSwitch->cases, fn (Case_ $case): bool => $this->isValidCase($case));
}
}
9 changes: 8 additions & 1 deletion rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAllRector\ForeachToArrayAllRectorTest
*/
final class ForeachToArrayAllRector extends AbstractRector implements MinPhpVersionInterface
final class ForeachToArrayAllRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
Expand Down Expand Up @@ -102,6 +104,11 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_ALL;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_84;
}

/**
* @param StmtsAware $stmtsAware
*/
Expand Down
9 changes: 8 additions & 1 deletion rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\ForeachToArrayAnyRectorTest
*/
final class ForeachToArrayAnyRector extends AbstractRector implements MinPhpVersionInterface
final class ForeachToArrayAnyRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
Expand Down Expand Up @@ -101,6 +103,11 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_ANY;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_84;
}

/**
* @param StmtsAware $stmtsAware
*/
Expand Down
9 changes: 8 additions & 1 deletion rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector\ForeachToArrayFindKeyRectorTest
*/
final class ForeachToArrayFindKeyRector extends AbstractRector implements MinPhpVersionInterface
final class ForeachToArrayFindKeyRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
Expand Down Expand Up @@ -165,6 +167,11 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_FIND_KEY;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_84;
}

private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool
{
if (
Expand Down
9 changes: 8 additions & 1 deletion rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayFindRector\ForeachToArrayFindRectorTest
*/
final class ForeachToArrayFindRector extends AbstractRector implements MinPhpVersionInterface
final class ForeachToArrayFindRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
Expand Down Expand Up @@ -160,6 +162,11 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_FIND;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_84;
}

private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool
{
if (count($foreach->stmts) !== 1) {
Expand Down
8 changes: 1 addition & 7 deletions rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ public function __construct(

public function hasNeverFuncCall(ClassMethod | Closure | Function_ $functionLike): bool
{
foreach ((array) $functionLike->stmts as $stmt) {
if ($this->isWithNeverTypeExpr($stmt)) {
return true;
}
}

return false;
return array_any((array) $functionLike->stmts, fn (Stmt $stmt): bool => $this->isWithNeverTypeExpr($stmt));
}

public function isWithNeverTypeExpr(Stmt $stmt, bool $withNativeNeverType = true): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public function isFileStrictTypeSafe(FileNode $fileNode): bool
}

$assigns = $this->betterNodeFinder->findInstanceOf($fileNode->stmts, Assign::class);
foreach ($assigns as $assign) {
if (! $this->isPropertyAssignSafe($assign)) {
return false;
}
}

return true;
return array_all($assigns, fn (Assign $assign): bool => $this->isPropertyAssignSafe($assign));
}

private function isCallLikeSafe(CallLike $callLike): bool
Expand Down
16 changes: 2 additions & 14 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,15 @@ public function hasByType(string $type): bool
*/
public function hasByTypes(array $types): bool
{
foreach ($types as $type) {
if ($this->hasByType($type)) {
return true;
}
}

return false;
return array_any($types, fn (string $type): bool => $this->hasByType($type));
}

/**
* @param string[] $names
*/
public function hasByNames(array $names): bool
{
foreach ($names as $name) {
if ($this->hasByName($name)) {
return true;
}
}

return false;
return array_any($names, fn (string $name): bool => $this->hasByName($name));
}

public function hasByName(string $name): bool
Expand Down
12 changes: 5 additions & 7 deletions src/Configuration/VendorMissAnalyseGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public function isVendorAnalyzed(array $filePaths): bool

private function hasDowngradeSets(): bool
{
/** @var string[] $registeredRectorSets */
$registeredRectorSets = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_SETS);

foreach ($registeredRectorSets as $registeredRectorSet) {
if (str_contains((string) $registeredRectorSet, 'downgrade-')) {
return true;
}
}

return false;
return array_any(
$registeredRectorSets,
fn (string $registeredRectorSet): bool => str_contains($registeredRectorSet, 'downgrade-')
);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/CustomRules/SimpleNodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ public static function dump(array|Node $node, bool $rootNode = true): string
*/
private static function isStringList(array $items): bool
{
foreach ($items as $item) {
if (! is_string($item)) {
return false;
}
}

return true;
return array_all($items, fn (mixed $item): bool => is_string($item));
}

private static function dumpFlags(mixed $flags): string
Expand Down
5 changes: 4 additions & 1 deletion src/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function hasAbstractParentClassMethod(ClassReflection $classReflection, s
return false;
}

return array_any($parentClassMethods, fn ($parentClassMethod) => $parentClassMethod->isAbstract());
return array_any(
$parentClassMethods,
fn (PhpMethodReflection $phpMethodReflection): bool => $phpMethodReflection->isAbstract()
);
}

/**
Expand Down
Loading
Loading