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

Deprecate TypeUtils::getDirectClassNames() #1924

Merged

Conversation

herndlm
Copy link
Contributor

@herndlm herndlm commented Oct 26, 2022

Just a simple migration over to Type::, as requested in #1922 (comment)

Are you fine with that name? E.g. we also have Type::getReferencedClasses() which is somehow related, right?

I'd try to get rid of more instanceof TypeWithClassName in a follow-up if that's fine

@herndlm
Copy link
Contributor Author

herndlm commented Oct 26, 2022

Looks likes a deprecated method rule in the deprecation rules extension is causing a deprecation now that needs fixing then 🙃

@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from b9dac7f to 39f948e Compare October 30, 2022 17:57
@herndlm
Copy link
Contributor Author

herndlm commented Oct 30, 2022

The new failing tests here are from the deprecation extension which needs fixing afterwards. But it would be ready

@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from 39f948e to 5f71f70 Compare November 2, 2022 08:34
@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from 5f71f70 to c0fa22b Compare November 8, 2022 12:18
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Yeah, the name is OK :) Additionally, we could get rid of instanceof TypeWithClassName in this PR. Thank you.

@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from c0fa22b to 978958f Compare November 18, 2022 20:52
@herndlm herndlm marked this pull request as draft November 18, 2022 20:56
@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from 978958f to b91b0af Compare December 14, 2022 08:18
@herndlm
Copy link
Contributor Author

herndlm commented Dec 14, 2022

getting rid of all instanceof TypeWithClassName here can get ugly really fast. currently there are 40 of those and with many I'm not sure if we should really use the new method, introduce something else, or just leave it as it is.

e.g. some examples

calls where we explicitly want to know the class for whatever reasons

$classType = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use (&$uncertainty): Type {
	if ($type instanceof UnionType || $type instanceof IntersectionType) {
		return $traverse($type);
	}
	if ($type instanceof TypeWithClassName) {
		$uncertainty = true;
		return $type;
	}
...
if (!$returnType instanceof TypeWithClassName) {
	return new MixedType();
}

calls where we want to know explicitly the one className

$thisType instanceof TypeWithClassName ? $thisType->getClassName() : null,
$isThrowable = $originalCatchType instanceof TypeWithClassName && strtolower($originalCatchType->getClassName()) === 'throwable';
 elseif (
	$argValueType instanceof GenericClassStringType
	&& $argValueType->getGenericType() instanceof TypeWithClassName
) {
	$scopeClass = $argValueType->getGenericType()->getClassName();
	$thisType = $argValueType->getGenericType();
}

I'm not entirely sure how to get rid of them yet, e.g. in some cases I'd need to check that getObjectClassNames() is not empty, in some I'd need to check that one entry is returned, in some I can maybe traverse them and adapt the surrounding logic (but many places seem to be using GenericTypeVariableResolver::getType() which expects a TypeWithClassName) and in some maybe I want to check that only one entry is returned and use that. any ideas? or should we do this in more PRs maybe? I want to avoiding messing this one up too much :)

@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from b91b0af to 874a1e7 Compare January 3, 2023 08:33
@herndlm herndlm requested a review from ondrejmirtes January 3, 2023 08:33
@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch from 874a1e7 to 0bc0ac5 Compare January 3, 2023 08:39
@herndlm herndlm marked this pull request as ready for review January 3, 2023 08:48
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes
Copy link
Member

It shouldn't be hard to come up with examples where those instanceof TypeWithClassName are wrong - for example having an intersection with another type (but the condition where we're asking we'd still want it to be true).

Also - UnionType::getObjectClassNames() doesn't match how it should behave. If we for example have Foo|null, the returned array should be empty. Look at getArrays() for an example how to do it.

@herndlm herndlm marked this pull request as draft January 3, 2023 14:45
@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch 2 times, most recently from d751dcb to 1aa58ed Compare January 4, 2023 09:26
@ondrejmirtes
Copy link
Member

You can now just call pickTypes in UnionType :) 480626e

@herndlm
Copy link
Contributor Author

herndlm commented Jan 4, 2023

You can now just call pickTypes in UnionType :) 480626e

that's great! I just realised that you type-hinted T of Type but here T would be string :/
can I just remove it or should I make it a T of Type|string or do you have some other idea?

@ondrejmirtes
Copy link
Member

@herndlm Nothing might break if we remove the of Type part?

@herndlm
Copy link
Contributor Author

herndlm commented Jan 4, 2023

@herndlm Nothing might break if we remove the of Type part?

no, but it starts to get weird naming-wise. pickTypes(), $getTypes, $innerTypes and $types are all kind of wrong then

@ondrejmirtes
Copy link
Member

Yeah, you can rename that to something else :) Also maybe pickFromTypes instead of pickTypes.

@herndlm herndlm force-pushed the deprecate-type-utils-get-direct-class-names branch 3 times, most recently from 5a091bc to 569cfb0 Compare January 4, 2023 13:29
@herndlm herndlm marked this pull request as ready for review January 16, 2023 17:17
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@ondrejmirtes ondrejmirtes force-pushed the deprecate-type-utils-get-direct-class-names branch from 66e59f9 to b778089 Compare January 16, 2023 21:13
@ondrejmirtes ondrejmirtes merged commit 082ed46 into phpstan:1.10.x Jan 16, 2023
@ondrejmirtes
Copy link
Member

Thank you!

@staabm
Copy link
Contributor

staabm commented Jan 16, 2023

Great job @herndlm 🙏

@herndlm herndlm deleted the deprecate-type-utils-get-direct-class-names branch January 17, 2023 06:56
@ondrejmirtes
Copy link
Member

An idea - Type::getStringClassNames() for these cases:

  • ConstantStringType that's also a class string
  • GenericClassStringType - a shortcut to $type->getGenericType()->getObjectClassNames()

There are some cases of instanceof GenericClassStringType that we'd be able to get rid of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants