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
2 changes: 1 addition & 1 deletion src/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
}

$methodName = $node->name->toString();
if (! in_array($methodName, ['getRepository', 'find', 'findAll', 'findBy', 'findOneBy'])) {
if (! in_array($methodName, ['getRepository', 'find', 'findAll', 'findBy', 'findOneBy'], true)) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Explicit/ExplicitClassPrefixSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function str_ends_with;
use Symplify\PHPStanRules\Enum\RuleIdentifier;
use function str_ends_with;

/**
* @implements Rule<ClassLike>
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Explicit/NoProtectedClassStmtRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function shouldSkipClassMethod(ClassMethod|ClassConst|Property $classStm
}

// PHPUnit test methods
if (in_array($classStmt->name->toString(), [MethodName::SET_UP, MethodName::TEAR_DOWN])) {
if (in_array($classStmt->name->toString(), [MethodName::SET_UP, MethodName::TEAR_DOWN], true)) {
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Rules/NoDynamicNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\UnionType;
use Symplify\PHPStanRules\Enum\RuleIdentifier;
use Symplify\PHPStanRules\TypeAnalyzer\CallableTypeAnalyzer;

Expand All @@ -36,7 +37,7 @@ public function __construct(
public function getNodeType(): string
{
// trick to allow multiple node types
return Node::class;
return Expr::class;
Copy link
Copy Markdown
Contributor Author

@staabm staabm May 21, 2026

Choose a reason for hiding this comment

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

use a more specific parent type, so the rule is called less often

}

public function processNode(Node $node, Scope $scope): array
Expand All @@ -54,6 +55,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$classType = $scope->getType($node->class);
if ($classType instanceof UnionType && $classType->getObjectClassReflections() !== []) {
return [];
}

$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(RuleIdentifier::NO_DYNAMIC_NAME)
->build();
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Rector/NoIntegerRefactorReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function findUsedNodeVisitorConstantNames(ClassMethod $classMethod): arr
return null;
}

if (! in_array($subNode->class->toString(), [NodeVisitor::class, NodeTraverser::class])) {
if (! in_array($subNode->class->toString(), [NodeVisitor::class, NodeTraverser::class], true)) {
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Rules/StringFileAbsolutePathExistsRule.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Rules;

use PhpParser\Node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function processNode(Node $node, Scope $scope): array
}

// 2. special case for string known values
if ($referenceExpr instanceof String_ && in_array($referenceExpr->value, self::NAMED_AUTOWIRED_TYPES)) {
if ($referenceExpr instanceof String_ && in_array($referenceExpr->value, self::NAMED_AUTOWIRED_TYPES, true)) {
$ruleError = RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $referenceExpr->value))
->identifier(SymfonyRuleIdentifier::NO_DUPLICATE_ARG_AUTOWIRE_BY_TYPE)
->line($referenceFuncCall->getStartLine())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public static function isReferenceCall(Expr $expr): bool

$functionName = $expr->name->toString();

return in_array($functionName, [
SymfonyFunctionName::REF,
SymfonyFunctionName::SERVICE,
]);
return in_array(
$functionName,
[
SymfonyFunctionName::REF,
SymfonyFunctionName::SERVICE,
],
true
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Fixture;

use Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Source\SomeClassConstantA;
use Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Source\SomeClassConstantB;

final class SkipConstantLookupOnKnownType
{
public function run()
{
$objectOfKnownType = $this->getObject();
if ($objectOfKnownType::MY_CONSTANT === 3) {
echo "a";
}
}

private function getObject():SomeClassConstantA|SomeClassConstantB {
return rand(0,1) ? new SomeClassConstantA() : new SomeClassConstantB();
}
}
6 changes: 5 additions & 1 deletion tests/Rules/NoDynamicNameRule/NoDynamicNameRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\Rules\NoDynamicNameRule;

/**
* @extends RuleTestCase<NoDynamicNameRule>
*/
final class NoDynamicNameRuleTest extends RuleTestCase
{
/**
* @param array<int, array<string|int>> $expectedErrorMessagesWithLines
* @param list<array{string, int, 2?: string|null}> $expectedErrorMessagesWithLines
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

drive-by phpstan error cleanup

*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void
Expand Down Expand Up @@ -42,6 +45,7 @@ public static function provideData(): Iterator
yield [__DIR__ . '/Fixture/SkipMagicGet.php', []];
yield [__DIR__ . '/Fixture/SkipCallableUnion.php', []];
yield [__DIR__ . '/Fixture/SkipNullableClosure.php', []];
yield [__DIR__ . '/Fixture/SkipConstantLookupOnKnownType.php', []];
yield [__DIR__ . '/Fixture/SkipImmediatelyInvokedFunctionExpression.php', []];
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Rules/NoDynamicNameRule/Source/SomeClassConstantA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Source;

final class SomeClassConstantA
{
const MY_CONSTANT = 1;
}
10 changes: 10 additions & 0 deletions tests/Rules/NoDynamicNameRule/Source/SomeClassConstantB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Source;

final class SomeClassConstantB
{
const MY_CONSTANT = 2;
}
Loading