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

Extending ArrayObject with private properties produces false positives #7983

Closed
discordier opened this issue May 18, 2022 · 14 comments
Closed
Labels

Comments

@discordier
Copy link
Contributor

When defining a class that extends ArrayObject, psalm seems a little confused, as it detects private properties to have a different access level than the very same property in the very same class.

ERROR: [OverriddenPropertyAccess](https://psalm.dev/159) - 5:20 - Property Foo::$name has different access level than Foo::$name

It even reports some type variance for the private property - despite the fact that ArrayObject does not declare any properties.

ERROR: [NonInvariantPropertyType](https://psalm.dev/265) - 5:20 - Property Foo::$name has type string, not invariant with ArrayObject::$name of type <empty>

I fail to see how to tackle this, as I do not know how I could pin point the culprit.

https://psalm.dev/r/8c3b56d382

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8c3b56d382
<?php

/** @extends ArrayObject<string, mixed> */
class Foo extends \ArrayObject {
    private string $name = '';
}
Psalm output (using commit f960d71):

ERROR: OverriddenPropertyAccess - 5:20 - Property Foo::$name has different access level than Foo::$name

ERROR: NonInvariantPropertyType - 5:20 - Property Foo::$name has type string, not invariant with ArrayObject::$name of type <empty>

@AndrolGenhald
Copy link
Collaborator

For some reason ArrayObject has 'name' => 'string' in the PropertyMap. Would love to know why that's there in the first place before removing it, but it does seem like the property does not actually exist.

@discordier
Copy link
Contributor Author

Where does it come from?
It is not declared in the stub.

I found this:

'name' => 'string',

which accordingly to comment is "stolen" from phan/phan/src/Phan/Language/Internal/PropertyMap.php.
Sadly neither tells where it originates from.
It appears to be wrong non-the-less.

@orklah
Copy link
Collaborator

orklah commented May 18, 2022

It seems to be generated from php.net somehow: https://github.com/phan/phan/blob/93c1c287d646d08249b4f5539bfe147550b163d7/src/Phan/Language/Internal/PropertyMap.php#L56

but it seems wrong to me too. @TysonAndre Do you remember the reason for this? It seems that a lot of iterators seems to have a name property but I don't think it's true. Maybe old documentation artifacts?

@discordier
Copy link
Contributor Author

Traced it down to: https://svn.php.net/repository/phpdoc/en/trunk/reference/spl/arrayobject.xml

It has (from line 69 on):

<!-- }}} -->
  |  
  | <!-- {{{ If the property is documented below (xml:id=arrayobject.props) use this
  | <classsynopsisinfo role="comment">&Properties;</classsynopsisinfo>
  | <fieldsynopsis>
  | <modifier>public</modifier>
  | <type>string</type>
  | <varname linkend="arrayobject.props.name">name</varname>
  | </fieldsynopsis>
  | }}} -->

This is commented XML code, which is simply not handled by the bash one liner embedded in the code.

@orklah
Copy link
Collaborator

orklah commented May 18, 2022

wow congrats, this is a pretty old bug found in both tools (possibly in phpstan too?)!

I think there's a lot of name properties we could drop in there

@discordier
Copy link
Contributor Author

@orklah
Copy link
Collaborator

orklah commented May 19, 2022

Are you up for a PR to fix this? :)

@discordier
Copy link
Contributor Author

Working on it.
I'll check the other classes aswell, so please allow some time.

@discordier
Copy link
Contributor Author

While working on this, I am a little confused as some classes are denoted with - separation and some have the full namespace as foo\bar.
Is it relevant or shall I normalize to \\?

PS: I wrote a small XML parser script (using \SimpleXML), which handles the extraction, I'd put this in bin with the PR, is this ok?

@AndrolGenhald
Copy link
Collaborator

While working on this, I am a little confused as some classes are denoted with - separation

Can you give an example?

PS: I wrote a small XML parser script (using \SimpleXML), which handles the extraction, I'd put this in bin with the PR, is this ok?

Absolutely! My goal is to remove extensions from the callmap over time, but it will definitely still be useful for built in types, and with a bit of work it could provide a very useful automatic comparison between the PHP documentation and reflected extension types.

@orklah
Copy link
Collaborator

orklah commented May 19, 2022

the - seems weird yeah, it's not like that in phan either. I'd say normalize to \\, let's see if it still pass tests.

Yeah, please do for the parser

discordier added a commit to discordier/psalm that referenced this issue May 23, 2022
@discordier
Copy link
Contributor Author

PR submitted as #8000 (nice number 😄).

discordier added a commit to discordier/psalm that referenced this issue May 23, 2022
discordier added a commit to discordier/psalm that referenced this issue May 24, 2022
discordier added a commit to discordier/psalm that referenced this issue Jun 29, 2022
discordier added a commit to discordier/psalm that referenced this issue Jun 29, 2022
@orklah
Copy link
Collaborator

orklah commented Jul 29, 2022

this was fixed :)

@orklah orklah closed this as completed Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants