-
Notifications
You must be signed in to change notification settings - Fork 542
Enabling constructor check for class-string variables #3661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e5d182f
Fix PHPStan issue by enabling constructor check for class-string vari…
sayuprc a59fc5d
Add tests
sayuprc f4cfd00
Fixed behavior
sayuprc 615d5e6
Add tests
sayuprc 298e55e
Simplify
sayuprc 1072835
Update src/Rules/Classes/InstantiationRule.php
ondrejmirtes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,13 @@ | |
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function array_filter; | ||
use function array_map; | ||
use function array_merge; | ||
use function count; | ||
use function sprintf; | ||
use function str_starts_with; | ||
use function strtolower; | ||
|
||
/** | ||
|
@@ -245,6 +248,18 @@ private function getClassNames(Node $node, Scope $scope): array | |
|
||
$type = $scope->getType($node->class); | ||
|
||
if (str_starts_with($type->describe(VerbosityLevel::precise()), 'class-string')) { | ||
|
||
$classStringObjectType = $type->getClassStringObjectType(); | ||
|
||
return array_map( | ||
static fn (string $name): array => [$name, true], | ||
array_filter($classStringObjectType->getObjectClassNames(), function (string $name): bool { | ||
$reflectionClass = $this->reflectionProvider->getClass($name); | ||
return !$reflectionClass->isAbstract() && !$reflectionClass->isInterface(); | ||
}), | ||
); | ||
|
||
} | ||
|
||
return array_merge( | ||
array_map( | ||
static fn (ConstantStringType $type): array => [$type->getValue(), true], | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php // lint >= 8.0 | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace ClassString; | ||
|
||
class A | ||
{ | ||
public function __construct(public int $i) | ||
{ | ||
} | ||
} | ||
|
||
abstract class B | ||
{ | ||
public function __construct(public int $i) | ||
{ | ||
} | ||
} | ||
|
||
class C extends B | ||
{ | ||
} | ||
|
||
interface D | ||
{ | ||
} | ||
|
||
class Foo | ||
{ | ||
/** | ||
* @return class-string<A> | ||
*/ | ||
public static function returnClassStringA(): string | ||
{ | ||
return A::class; | ||
} | ||
|
||
/** | ||
* @return class-string<B> | ||
*/ | ||
public static function returnClassStringB(): string | ||
{ | ||
return B::class; | ||
} | ||
|
||
/** | ||
* @return class-string<C> | ||
*/ | ||
public static function returnClassStringC(): string | ||
{ | ||
return C::class; | ||
} | ||
|
||
/** | ||
* @return class-string<D> | ||
*/ | ||
public static function returnClassStringD(): string | ||
{ | ||
return D::class; | ||
} | ||
} | ||
|
||
$classString = Foo::returnClassStringA(); | ||
$error = new (Foo::returnClassStringA())('O_O'); | ||
$error = new ($classString)('O_O'); | ||
$error = new $classString('O_O'); | ||
|
||
$classString = Foo::returnClassStringB(); | ||
$ok = new (Foo::returnClassStringB())('O_O'); | ||
$ok = new ($classString)('O_O'); | ||
$ok = new $classString('O_O'); | ||
|
||
$classString = Foo::returnClassStringC(); | ||
$error = new (Foo::returnClassStringC())('O_O'); | ||
$error = new ($classString)('O_O'); | ||
$error = new $classString('O_O'); | ||
|
||
$classString = Foo::returnClassStringD(); | ||
$ok = new (Foo::returnClassStringD())('O_O'); | ||
$ok = new ($classString)('O_O'); | ||
$ok = new $classString('O_O'); | ||
|
||
$className = A::class; | ||
$error = new ($className)('O_O'); | ||
$error = new $className('O_O'); | ||
$error = new A('O_O'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, rule logic can't rely on string type descriptions.