- Notifications
You must be signed in to change notification settings - Fork 542
Fix get_class()
on HasMethodType #2350
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
Open
staabm wants to merge 4 commits into phpstan:1.10.x Choose a base branch from staabm:bug4890
base: 1.10.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -11,13 +11,13 @@ | |
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\Enum\EnumCaseObjectType; | ||
use PHPStan\Type\Generic\GenericClassStringType; | ||
use PHPStan\Type\Generic\TemplateType; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ObjectShapeType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\ObjectWithoutClassType; | ||
use PHPStan\Type\StaticType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeTraverser; | ||
use PHPStan\Type\TypeUtils; | ||
use PHPStan\Type\UnionType; | ||
| @@ -64,27 +64,26 @@ static function (Type $type, callable $traverse): Type { | |
return new GenericClassStringType(new ObjectType($type->getClassName())); | ||
} | ||
| ||
$objectClassNames = $type->getObjectClassNames(); | ||
if ($type instanceof TemplateType && $objectClassNames === []) { | ||
if ($type instanceof ObjectWithoutClassType) { | ||
return new GenericClassStringType($type); | ||
$isObject = $type->isObject(); | ||
if ($isObject->yes() || $isObject->maybe()) { | ||
if ($type instanceof ObjectShapeType) { | ||
return new ClassStringType(); | ||
} | ||
| ||
$objectType = TypeCombinator::intersect($type, new ObjectWithoutClassType()); | ||
if ($objectType instanceof StaticType) { | ||
$objectType = $objectType->getStaticObjectType(); | ||
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. Why are you checking this after an intersection? | ||
} | ||
$classStringType = new GenericClassStringType($objectType); | ||
| ||
if ($isObject->yes()) { | ||
return $classStringType; | ||
} | ||
| ||
return new UnionType([ | ||
new GenericClassStringType($type), | ||
new ConstantBooleanType(false), | ||
]); | ||
} elseif ($type instanceof MixedType) { | ||
return new UnionType([ | ||
new ClassStringType(), | ||
$classStringType, | ||
new ConstantBooleanType(false), | ||
]); | ||
} elseif ($type instanceof StaticType) { | ||
return new GenericClassStringType($type->getStaticObjectType()); | ||
} elseif ($objectClassNames !== []) { | ||
return new GenericClassStringType($type); | ||
} elseif ($type instanceof ObjectWithoutClassType) { | ||
return new ClassStringType(); | ||
} | ||
| ||
return new ConstantBooleanType(false); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
| ||
namespace Bug4890; | ||
| ||
use function PHPStan\Testing\assertType; | ||
| ||
interface Proxy {} | ||
| ||
class HelloWorld | ||
| ||
{ | ||
public function update(object $entity): void | ||
{ | ||
assertType('class-string<object>', get_class($entity)); | ||
assert(method_exists($entity, 'getId')); | ||
assertType('class-string<object&hasMethod(getId)>', get_class($entity)); | ||
| ||
if ($entity instanceof Proxy) { | ||
assertType('class-string<Bug4890\Proxy&hasMethod(getId)>', get_class($entity)); | ||
} | ||
| ||
$class = $entity instanceof Proxy | ||
? get_parent_class($entity) | ||
: get_class($entity); | ||
assert(is_string($class)); | ||
| ||
} | ||
| ||
public function updateProp(object $entity): void | ||
{ | ||
assertType('class-string<object>', get_class($entity)); | ||
assert(property_exists($entity, 'myProp')); | ||
assertType('class-string<object&hasProperty(myProp)>', get_class($entity)); | ||
| ||
if ($entity instanceof Proxy) { | ||
assertType('class-string<Bug4890\Proxy&hasProperty(myProp)>', get_class($entity)); | ||
} | ||
| ||
$class = $entity instanceof Proxy | ||
? get_parent_class($entity) | ||
: get_class($entity); | ||
assert(is_string($class)); | ||
} | ||
| ||
/** | ||
* @param object{foo: self, bar: int, baz?: string} $entity | ||
*/ | ||
public function updateObjectShape($entity): void | ||
{ | ||
assertType('class-string', get_class($entity)); | ||
assert(property_exists($entity, 'foo')); | ||
assertType('class-string', get_class($entity)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
| ||
namespace Bug4890b; | ||
| ||
interface Proxy {} | ||
| ||
class HelloWorld | ||
{ | ||
public function update(object $entity): void | ||
{ | ||
assert(method_exists($entity, 'getId')); | ||
| ||
$class = $entity instanceof Proxy | ||
? get_parent_class($entity) | ||
: get_class($entity); | ||
assert(is_string($class)); | ||
} | ||
} | ||
| ||
|
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
For which type here it's going to be a
maybe
?