phpstan-src
phpstan-src copied to clipboard
Difference between Instance and Static properties
Closes https://github.com/phpstan/phpstan-src/pull/12775
I encounter some issues/questions for you @ondrejmirtes.
First, HasPropertyType is tricky
- since it's used for both static and non static properties.
- and
property_existsreturns true either it's a static or non static properties
So we have
class Foo
{
public $foo;
public static $staticFoo;
}
var_dump(property_exists(new Foo(), 'foo')); // TRUE
var_dump(property_exists(new Foo(), 'staticFoo')); // TRUE
Foo::$foo; // CRASH
(new Foo())->foo; // Works
Foo::$staticFoo; // Works
(new Foo())->staticFoo; // CRASH
Second, and kinda related, how should be considered properties from ObjectShapeType ? Are they only instance properties ?
Also if you already have some comment on the existing work.
Yeah, these kinds of questions are great and generally are a positive effect of deprecating old methods - it forces us to do stuff correctly and fix existing problems along the way.
My thoughts:
- HasPropertyType is used alongside property_exists so representing both instance and static properties is fine. Not sure what kind of problem you have with that now. If you don't know what to do about this we can leave it for later (it's fine to leave some failing tests here for now).
- ObjectShapeType - should work only for instance properties.
In the new list of todo to see with you @ondrejmirtes I have:
PHPClassReflectionExtension::createPropertyto maybe split into createInstanceProperty and createStaticProperty (?) ; I'm not familiar with the implementation yet.HasPropertyType::isSuperTypeOf=> remplace usage ofhasPropertyHasProperty::isSubTypeOf=> remplace usage ofhasProperty- Having or not a difference between
HasProperty::hasInstancePropertyandHasProperty::hasStaticPropertyand updating thenew HasProperty()eventually. - Deciding if the LevelsIntegrationTest change is ok. (It's UnionType related, when one of the class has not the property and the other has the static property of the same name)
HasPropertyType is used alongside property_exists so representing both instance and static properties is fine. Not sure what kind of problem you have with that now. If you don't know what to do about this we can leave it for later (it's fine to leave some failing tests here for now).
Currently the TypeSpecifier does
if (
$var instanceof PropertyFetch
&& $var->name instanceof Node\Identifier
) {
$types = $types->unionWith(
$this->create($var->var, new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($var->name->toString()),
]), TypeSpecifierContext::createTruthy(), $scope)->setRootExpr($expr),
);
} elseif (
$var instanceof StaticPropertyFetch
&& $var->class instanceof Expr
&& $var->name instanceof Node\VarLikeIdentifier
) {
$types = $types->unionWith(
$this->create($var->class, new IntersectionType([
new ObjectWithoutClassType(),
new HasPropertyType($var->name->toString()),
]), TypeSpecifierContext::createTruthy(), $scope)->setRootExpr($expr),
);
}
Which I think, means that accessing to Foo::$bar makes PHPStan thinks that foo->bar exists. (and the opposite)
So should we add a param to the HasPropertyType construct which would be STATIC|INSTANCE|UNKNOWN
This pull request has been marked as ready for review.
Rebased and fix, it should be ready for a new review @ondrejmirtes :)
It's rebased and fixed I think.
I know it's not an easy PR to review @ondrejmirtes but it would be awesome if we succeed closing it :) Is there something I can do to help the review ?
It's rebased and fixed again.
This PR is really hard to maintain, are you (still ?) interested in it @ondrejmirtes ? Can I do something to help you to review it ?
Rebased and I had to introduce new fixes because of recent bugfixes.
Should be re-ready for review @ondrejmirtes
nit: PR description contains a wrong link
nit: PR description contains a wrong link
Thanks, updated to https://github.com/phpstan/phpstan/issues/12775
Hi, I'm taking over this PR and taking it over the finish line. Already have a local branch with some changes. Stay tuned!
Thank you!
Thank you too !