Skip to content

feat: add api to get ObjectType from ClassReflection and EnumCaseReflection #4099

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 1 commit into from
Jul 17, 2025

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented Jul 14, 2025

Hello!

This PR adds the following two methods to the api:

  • ClassReflection::getObjectType(): ObjectType
  • EnumCaseReflection:getEnumCaseObjectType(): EnumCaseObjectType

Motivation

It's very easy to get all the ClassReflections from a Type with $type->getObjectClassReflections(). However, there have been several occasions where I've needed to go the other direction (get an ObjectType from a ClassReflection) and have not been able to do so easily.

I was digging around in the code, and I discovered a private static method ObjectType::createFromReflection() that already did just this. So I opted to move the logic in ObjectType::createFromReflection() to ClassReflection::getObjectType() instead of simply making the private method public as I didn't think allowing child classes to statically call this method would be a good idea (e.g., TemplateObjectType::createFromReflection())

Thanks!

@calebdw
Copy link
Contributor Author

calebdw commented Jul 14, 2025

@ondrejmirtes,

In the original logic, you have the class reflection instance, but you don't pass it into the ObjectType and instead opt to leave it null so that the class reflection is assigned the next time getClassReflection() is called on the type.

What is the reasoning for this? I would think that it would be better to pass along the class reflection instance since you already have it, but maybe I'm missing something...

private static function createFromReflection(ClassReflection $reflection): self
{
if (!$reflection->isGeneric()) {
return new ObjectType($reflection->getName());
}
return new GenericObjectType(
$reflection->getName(),
$reflection->typeMapToList($reflection->getActiveTemplateTypeMap()),
variances: $reflection->varianceMapToList($reflection->getCallSiteVarianceMap()),
);
}

@calebdw calebdw force-pushed the calebdw/push-poykyvwuvkyn branch from 366e72a to e7d3e6b Compare July 14, 2025 19:23
@ondrejmirtes ondrejmirtes merged commit 9cf5370 into phpstan:2.1.x Jul 17, 2025
833 of 839 checks passed
@ondrejmirtes
Copy link
Member

Thank you, this is okay. Omitting of passing class reflection to ObjectType in the original code might change some behaviour for the better or worse as there will be slight differences in some scenario, we'll see.

@calebdw calebdw deleted the calebdw/push-poykyvwuvkyn branch July 17, 2025 12:32
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.

2 participants