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

add getProperties() and getConstants() to ClassLike #629

Merged
merged 2 commits into from
Aug 30, 2019
Merged

add getProperties() and getConstants() to ClassLike #629

merged 2 commits into from
Aug 30, 2019

Conversation

TomasVotruba
Copy link
Contributor

@TomasVotruba TomasVotruba commented Aug 29, 2019

As there is getMethods() at ClassLike, I often tend to use getProperties() and getConstants(), but there are none. I understood that's probably because only getMethods() is in ClassLike for all 3 sub-classes.

Yet the API is inconsistent and confusing. This would fix that.

foreach ($class->stmts as $stmt) {
    if (! $stmt instanceof Property) {
        continue;
    }

    // do something
}   

vs.

foreach ($class->getMethods() as $method) {
    // do something
}   

This keeps happening to me last 2 years so I finally do something about it

@nikic
Copy link
Owner

nikic commented Aug 29, 2019

I think these should just be on ClassLike. I know not everything is supported everywhere, but that just means you get an empty array (also, at the parser level they are supported anyway...)

@TomasVotruba
Copy link
Contributor Author

Even if constants are not allowed in traits and properties are not allowed in interfaces?

@TomasVotruba
Copy link
Contributor Author

CI fail is not related to this feature, but rather some bug in
https://travis-ci.org/nikic/PHP-Parser/jobs/578377375#L346

The command "if [ $TRAVIS_PHP_VERSION = '7.2' ]; then test_old/run-php-src.sh; fi" exited with 1.

@nikic
Copy link
Owner

nikic commented Aug 30, 2019

@TomasVotruba Fixed with 5644a91.

@nikic
Copy link
Owner

nikic commented Aug 30, 2019

Even if constants are not allowed in traits and properties are not allowed in interfaces?

I think so. For a couple of reasons:

  • I expect that at least properties in interfaces will be supported in the future.
  • While this is forbidden by the language, the parser still accepts both constants in traits and properties in interfaces.
  • For the user, it is easier to just treat this generically -- you can write code that calls $classLike->getProperties() and just get back an empty array for interfaces, but don't have to do anything to explicitly distinguish those cases.

@TomasVotruba
Copy link
Contributor Author

TomasVotruba commented Aug 30, 2019

Ok, I'd prefer it too, less code to maintain and more ClassLikely :)

Will re-do. Thanks for reasoning 👍

@nikic nikic merged commit 005bb1d into nikic:master Aug 30, 2019
@TomasVotruba TomasVotruba deleted the get-properties branch August 30, 2019 18:52
@TomasVotruba
Copy link
Contributor Author

Thank you 👍

@TomasVotruba TomasVotruba changed the title add getProperties() to Class_/Trait_ and getConstants() to Class_/Interface_ add getProperties() and getConstants() to ClassLike Aug 31, 2019
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