-
Notifications
You must be signed in to change notification settings - Fork 8
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
Updates for PHPStan 2.0 #48
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
use PhpParser\Node\NullableType; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\UnionType; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PHPStan\Node\Printer\Printer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan has removed the |
||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ExtendedMethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
|
@@ -35,11 +35,9 @@ | |
|
||
final readonly class CollectorMetadataPrinter | ||
{ | ||
private Standard $printerStandard; | ||
|
||
public function __construct() | ||
{ | ||
$this->printerStandard = new Standard(); | ||
public function __construct( | ||
private Printer $printer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan complains if we try to instantiate this directly, it needs to be passed through dependency injection There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to create |
||
) { | ||
} | ||
|
||
public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodReflection $extendedMethodReflection, Scope $scope): string | ||
|
@@ -56,6 +54,7 @@ public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodRefl | |
return ResolvedTypes::UNKNOWN_TYPES; | ||
} | ||
|
||
// @phpstan-ignore phpstanApi.instanceofType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case there is no good alternative to checking that it is an instance of IntersectionType (at least I was not able to find one), so I have added a ignore comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better place it in |
||
if ($argType instanceof IntersectionType) { | ||
return ResolvedTypes::UNKNOWN_TYPES; | ||
} | ||
|
@@ -100,7 +99,7 @@ public function printParamTypesToString(ClassMethod $classMethod, ?string $class | |
$paramType = $this->resolveSortedTypes($paramType, $className); | ||
} | ||
|
||
$printedParamType = $this->printerStandard->prettyPrint([$paramType]); | ||
$printedParamType = $this->printer->prettyPrint([$paramType]); | ||
$printedParamType = str_replace('\Closure', 'callable', $printedParamType); | ||
$printedParamType = ltrim($printedParamType, '\\'); | ||
$printedParamType = str_replace('|\\', '|', $printedParamType); | ||
|
@@ -160,15 +159,15 @@ private function resolveSortedTypes(UnionType|NodeIntersectionType $paramType, ? | |
|
||
private function printTypeToString(Type $type): string | ||
{ | ||
if ($type instanceof ClassStringType) { | ||
if ($type->isClassString()->yes()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan recommends using these alternatives |
||
return 'string'; | ||
} | ||
|
||
if ($type instanceof ArrayType) { | ||
if ($type->isArray()->yes()) { | ||
return 'array'; | ||
} | ||
|
||
if ($type instanceof BooleanType) { | ||
if ($type->isBoolean()->yes()) { | ||
return 'bool'; | ||
} | ||
|
||
|
@@ -180,8 +179,8 @@ private function printTypeToString(Type $type): string | |
return 'callable'; | ||
} | ||
|
||
if ($type instanceof EnumCaseObjectType) { | ||
return $type->getClassName(); | ||
if (count($type->getEnumCases()) === 1) { | ||
return $type->getEnumCases()[0]->getClassName(); | ||
} | ||
|
||
return $type->describe(VerbosityLevel::typeOnly()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use PhpParser\NodeVisitor\NameResolver; | ||
use PhpParser\Parser; | ||
use PhpParser\ParserFactory; | ||
use PhpParser\PhpVersion; | ||
use PHPStan\Reflection\ClassReflection; | ||
use Throwable; | ||
|
||
|
@@ -26,7 +27,7 @@ final class ReflectionParser | |
public function __construct() | ||
{ | ||
$parserFactory = new ParserFactory(); | ||
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7); | ||
$this->parser = $parserFactory->createForVersion(PhpVersion::fromString('7.4')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPParser 5 has removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public function parseClassReflection(ClassReflection $classReflection): ?ClassLike | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,10 +110,6 @@ private function validateArgVsParamTypes(array $args, MethodCall $methodCall, Sc | |
continue; | ||
} | ||
|
||
if (! $arg instanceof Arg) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $arg will always be an Arg, this check is not needed |
||
continue; | ||
} | ||
|
||
$paramRuleError = $this->validateParam($param, $position, $arg->value, $scope); | ||
if (! $paramRuleError instanceof RuleError) { | ||
continue; | ||
|
@@ -149,6 +145,7 @@ private function validateParam(Param $param, int $position, Expr $expr, Scope $s | |
return null; | ||
} | ||
|
||
// @phpstan-ignore phpstanApi.instanceofType | ||
if ($argType instanceof IntersectionType) { | ||
return null; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ public function getNodeType(): string | |
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$varType = $scope->getType($node->var); | ||
// @phpstan-ignore phpstanApi.instanceofType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, there is no good alternative or at least I was not able to find one |
||
if (! $varType instanceof ObjectType) { | ||
return []; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ public function processNode(Node $node, Scope $scope): array | |
} | ||
|
||
$parentClassMethodReflection = $this->methodNodeAnalyser->matchFirstParentClassMethod($scope, $classMethodName); | ||
// @phpstan-ignore phpstanApi.instanceofAssumption | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not covered by PHPStan's BC guarantee, for now we ignore it |
||
if (! $parentClassMethodReflection instanceof PhpMethodReflection) { | ||
return []; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
final class NarrowPrivateClassMethodParamTypeRuleTest extends RuleTestCase | ||
{ | ||
/** | ||
* @param mixed[] $expectedErrorsWithLines | ||
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrorsWithLines | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PHPStan asks us to add a more detailed type hint |
||
*/ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorsWithLines): void | ||
|
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.
See https://phpstan.org/blog/why-is-instanceof-type-wrong-and-getting-deprecated