Skip to content

Forbid direct access to static members on traits #4829

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

Closed
wants to merge 3 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Oct 15, 2019

This forbids directly accessing static properties and static methods on traits. Trait members in general can only be used through the class in which the trait was used, but due to an implementation oversight access to static members was still allowed. This change remedies this.

@nikic
Copy link
Member Author

nikic commented Oct 15, 2019

Usages found in open-source projects:

/home/nikic/package-analysis/sources/drupal/core/modules/simpletest/src/WebTestBase.php:706
    DeprecationListenerTrait::getSkippedDeprecations()
/home/nikic/package-analysis/sources/drupal/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevPub.php:63
    EntityPublishedTrait::publishedBaseFieldDefinitions($entity_type)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/AST/ContextNode.php:2434
    ConditionVisitorUtil::getFunctionName($node)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Language/Element/FunctionTrait.php:1083
    FunctionTrait::addParamsToScopeOfFunctionOrMethod($this->getContext(), $code_base, $this, $comment)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Analysis/ConditionVisitor/NotHasTypeCondition.php:48
    ConditionVisitorUtil::excludeMatchingTypes($code_base, $variable->getUnionType(), $this->type)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Analysis/PreOrderAnalysisVisitor.php:169
    Analyzable::ensureDidAnnotate($node)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Analysis/PreOrderAnalysisVisitor.php:272
    Analyzable::ensureDidAnnotate($node)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Analysis/PreOrderAnalysisVisitor.php:404
    Analyzable::ensureDidAnnotate($node)
/home/nikic/package-analysis/sources/phan/phan/src/Phan/Analysis/PreOrderAnalysisVisitor.php:543
    Analyzable::ensureDidAnnotate($node)
/home/nikic/package-analysis/sources/google/gax/src/Testing/MockStubTrait.php:145
    MockStubTrait::stripStatusFromResponses($this->responses)
/home/nikic/package-analysis/sources/google/gax/src/Testing/MockStubTrait.php:174
    MockStubTrait::stripStatusFromResponses($this->responses)
/home/nikic/package-analysis/sources/drush/drush/examples/Commands/SyncViaHttpCommands.php:64
    ExecTrait::programExists('wget')
/home/nikic/package-analysis/sources/encore/laravel-admin/src/Grid/Tools/QuickSearch.php:22
    HasQuickSearch::$searchKey
/home/nikic/package-analysis/sources/encore/laravel-admin/src/Grid/Tools/QuickSearch.php:26
    HasQuickSearch::$searchKey
/home/nikic/package-analysis/sources/encore/laravel-admin/src/Grid/Tools/QuickSearch.php:27
    HasQuickSearch::$searchKey
/home/nikic/package-analysis/sources/elasticsearch/elasticsearch/src/Elasticsearch/Namespaces/IndicesNamespace.php:50
    BooleanRequestWrapper::performRequest($endpoint, $this->transport)
/home/nikic/package-analysis/sources/elasticsearch/elasticsearch/src/Elasticsearch/Namespaces/IndicesNamespace.php:480
    BooleanRequestWrapper::performRequest($endpoint, $this->transport)
/home/nikic/package-analysis/sources/elasticsearch/elasticsearch/src/Elasticsearch/Namespaces/IndicesNamespace.php:685
    BooleanRequestWrapper::performRequest($endpoint, $this->transport)
/home/nikic/package-analysis/sources/elasticsearch/elasticsearch/src/Elasticsearch/Namespaces/IndicesNamespace.php:960
    BooleanRequestWrapper::performRequest($endpoint, $this->transport)
/home/nikic/package-analysis/sources/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:561
    BooleanRequestWrapper::performRequest($endpoint, $this->transport)

@nikic
Copy link
Member Author

nikic commented Oct 15, 2019

Heads up @z-song on the use of the trait member HasQuickSearch::$searchKey in https://github.com/z-song/laravel-admin/blob/335655b30632858826bea896cca4056194d03879/src/Grid/Tools/QuickSearch.php#L37-L51. Is it possible to avoid accessing this directly on a trait?

Comment on lines +18 to +19
T::$foo = 42;
var_dump(T::$foo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 lines should be in their own try/catch block, otherwise the case covered by the 2nd line would not be tested.

Copy link
Contributor

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks sensible - not sure whether there are real-world necessary use-cases for it, but they can be replaced via abstract class using the trait, I suppose.

@dstogov
Copy link
Member

dstogov commented Oct 15, 2019

I think, it's better to pass this change through RFC process.
It's definitely going to affect some projects, but fixes are going to be trivial.

@krakjoe
Copy link
Member

krakjoe commented Oct 16, 2019

I think an RFC for 8 makes sense

@kocsismate
Copy link
Member

@nikic Do you still want to pursue this RFC for PHP 8.0? Or is it not that important to have in PHP 8.0?

@nikic
Copy link
Member Author

nikic commented Jul 9, 2021

A deprecation for this is part of https://wiki.php.net/rfc/deprecations_php_8_1. Closing this outright removal.

@nikic nikic closed this Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants