-
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 all commits
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 |
---|---|---|
|
@@ -14,14 +14,11 @@ | |
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; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\ClassStringType; | ||
use PHPStan\Type\ClosureType; | ||
use PHPStan\Type\Enum\EnumCaseObjectType; | ||
use PHPStan\Type\IntegerRangeType; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\MixedType; | ||
|
@@ -35,11 +32,11 @@ | |
|
||
final readonly class CollectorMetadataPrinter | ||
{ | ||
private Standard $printerStandard; | ||
private Standard $printer; | ||
|
||
public function __construct() | ||
{ | ||
$this->printerStandard = new Standard(); | ||
public function __construct( | ||
) { | ||
$this->printer = new Standard(); | ||
} | ||
|
||
public function printArgTypesAsString(MethodCall $methodCall, ExtendedMethodReflection $extendedMethodReflection, Scope $scope): string | ||
|
@@ -100,7 +97,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 +157,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 +177,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 |
---|---|---|
|
@@ -26,7 +26,7 @@ final class ReflectionParser | |
public function __construct() | ||
{ | ||
$parserFactory = new ParserFactory(); | ||
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7); | ||
$this->parser = $parserFactory->createForNewestSupportedVersion(); | ||
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. it needs verify under php 7.x, since rector downgraded to php 7.2, will verify after PR on rector side finished currently still wip :) 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. That's why I said I wasn't sure what to do here and why I initially used BTW the new Rector probably needs to downgrade to PHP 7.4 since PHPStan dropped support for 7.2 and 7.3, but again not really sure 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 see, Rector require phpstan, so php 7.4 will be minimum then ;) 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 tested on rector side, with
updating to : createForVersion(PhpVersion::fromString('7.0')) make it working:
see rectorphp/rector-src#6431 (comment) this needs fallback to php 8.0 tho, I will keep updating ;) 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. Let me know if I can help in any way 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 got it, on rector side, using If parsing process doesn't involve phpstan, you may need to use try {
// parse with newest supported version
} catch (\PhpParser\Error) {
// parse with php 7 parser
} |
||
} | ||
|
||
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; | ||
|
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