Skip to content
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

implement isClassStringType() on Type #1970

Merged
merged 5 commits into from Nov 6, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Nov 5, 2022

No description provided.

@staabm
Copy link
Contributor Author

staabm commented Nov 5, 2022

in 11ca353 I added a test from phpstan/phpstan#1267 (comment) which was not covered by #1969

@@ -392,6 +392,14 @@ parameters:
count: 1
path: src/Testing/PHPStanTestCase.php

-
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was able to drop all uses of isClassString and instanceof ClassStringType with 48f8635 but these 2 occurences. These seemed harder to solve, therefore I put them into the baseline.

Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing something, but it looks like the internals of isClassString() should be moved into isClassStringType() and wrapped in a trinary. At least you don't have to use the deprecated method in the new one then. You could even turn it around and let the deprecated one use the new one internally?

Not sure what the problem with the other usage was 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after a couple of hours sleep I was able to resolve the remining stuff :)

@staabm staabm marked this pull request as ready for review November 5, 2022 16:08
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the type-class-string branch 3 times, most recently from 419daca to 3747223 Compare November 6, 2022 07:39
{
if ($this->subtractedType !== null) {
$stringType = new StringType();
if ($this->subtractedType->isSuperTypeOf($stringType)->yes()) {
Copy link
Member

Choose a reason for hiding this comment

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

Alright, so mixed~string is definitely no. mixed~class-string should also be no.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, and tests added.

src/Type/CallableType.php Outdated Show resolved Hide resolved
@@ -27,7 +27,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
}

if ($type instanceof ConstantStringType) {
return TrinaryLogic::createFromBoolean($type->isClassString());
return TrinaryLogic::createFromBoolean($type->isClassStringType()->yes());
Copy link
Member

Choose a reason for hiding this comment

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

I don't get it, why degrade maybe to no?

You can do just return $type->isClassStringType(); and it's gonna be even better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made a 1:1 translation of pre-existing logic. didn't realize that returning the trinary from isClassStringType would be even more precise.

fixed

src/Type/Constant/ConstantStringType.php Outdated Show resolved Hide resolved
@@ -44,7 +44,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof ConstantStringType) {
return TrinaryLogic::createFromBoolean($type->isClassString());
return TrinaryLogic::createFromBoolean($type->isClassStringType()->yes());
Copy link
Contributor

@herndlm herndlm Nov 6, 2022

Choose a reason for hiding this comment

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

You can simplify this and return the original trinary here and above maybe?

Copy link
Contributor Author

@staabm staabm Nov 6, 2022

Choose a reason for hiding this comment

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

I thought so too, but I think the alternative would be $type->isClassStringType()->and(Trinary::createYes()) which is not that readable either. just returning doesn't work, because of Maybe.

Copy link
Contributor Author

@staabm staabm Nov 6, 2022

Choose a reason for hiding this comment

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

@ondrejmirtes
Copy link
Member

BTW :) b78ff97 Similarly it should be possible to simplify complicated GenericClassStringType::accepts() and isSuperTypeOf(), but that's gonna need more methods on Type.

@ondrejmirtes ondrejmirtes merged commit 06ce1e8 into phpstan:1.9.x Nov 6, 2022
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the type-class-string branch November 6, 2022 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants