Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[EventDispatcher] Add types to private properties#41924

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:6.0fromderrabus:types/ed-properties
Jul 3, 2021

Conversation

@derrabus
Copy link
Member

@derrabusderrabus commentedJul 1, 2021
edited
Loading

QA
Branch?6.0
Bug fix?no
New feature?no
Deprecations?no
TicketsN/A
LicenseMIT
Doc PRN/A

In Symfony 6, we would be able to use typed properties. This is why I gave it a try and migrated all private properties of my favorite component to evaluate how the path might look like and what we have to watch out for.

Why only private properties? Well because adding types here is mostly safe. If I add a type to a protected/public property, I might break existing code that overrides that property.

One important thing we have to watch out for is the uninitialized state that a typed property has until we assign a value to it for the first time. As long as that property is in that state, reading it will trigger an error. However, callingisset() is safe and access to those properties can be safeguarded with?? and??=.

An untyped property would implicitly be initialized withnull and our existing code often relies on that behavior.

Another pitfall is thecallable pseudo-type: PHP does not allow us to assign it to a property. I've usedstring|array|object instead, but I'm open for better ideas.

I used PhpStorm for most of the work, but I needed to adjust the code afterwards. I've searched the code (except theTests folder) for forprivate $, opened each file and let PhpStorm infer all property types. Findings:

  • If PhpStorm inferred a type, it was the correct one.
  • PhpStorm is bad at detecting the nullability of a property.
  • PhpStorm won't help you deal with uninitialized properties and it won't add a= null initializer.

The manual work was mainly look at all the places where a property is accessed and check if an implicitnull initialization was expected or ifnull is assigned directly to the property. My goal here was to avoid making a property nullable if possible.

I sometimes moved an initialization with a constant value from the constructor to the property declaration to make the initialization more obvious. I think we can backport that if we want to keep the diff small.

What do you think? Shall we go with types properties?

@carsonbotcarsonbot added Status: Needs Review EventDispatcher RFCRFC = Request For Comments (proposals about features that you want to be discussed) labelsJul 1, 2021
@carsonbotcarsonbot added this to the6.0 milestoneJul 1, 2021
@carsonbotcarsonbot changed the title[RFC][EventDispatcher] Add types to private properties[EventDispatcher] Add types to private propertiesJul 1, 2021
@derrabus
Copy link
MemberAuthor

Note: One thing I intentionally kept out of this PR was constructor property promotion. Introducing property types would technically enable us to use CPP.

However, this is mainly syntactic sugar that makes merges from lower branches for existing classes difficult and would bloat the diff for a PR that introduces property types. If we ever feel like migrating to CPP, that step can be automated well, once we have private properties.

@stof
Copy link
Member

stof commentedJul 1, 2021

Another pitfall is thecallable pseudo-type: PHP does not allow us to assign it to a property. I've usedstring|array|object instead, but I'm open for better ideas.

I'd rather leaving it untyped that adding a wrong type (string|array|object does not enforce it being callable)

@derrabus
Copy link
MemberAuthor

string|array|object does not enforce it being callable

No, but it does make sure that the property is not null for instance. As I said, I'm not super happy either, but I tried to narrow down that type as much as possible.

@stof
Copy link
Member

stof commentedJul 1, 2021

@derrabus I'd still keep it untyped, relying on phpdoc (and the static analysis).

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

I'm fine with the type for listener personally
as a rule of thumb, we'd better store closures than callables anyway
here we can't, but we don't call it either

@derrabus
Copy link
MemberAuthor

as a rule of thumb, we'd better store closures than callables anyway

All right, so for future PRs I'll check if I can turn a callable into a closure.

nicolas-grekas reacted with thumbs up emoji

@derrabus
Copy link
MemberAuthor

Next attempt: The DI component:#41928

Signed-off-by: Alexander M. Turek <me@derrabus.de>
@fabpotfabpotforce-pushed thetypes/ed-properties branch frombd5556f to3852feeCompareJuly 3, 2021 05:04
@fabpot
Copy link
Member

Thank you@derrabus.

@fabpotfabpot merged commite98f0d5 intosymfony:6.0Jul 3, 2021
@derrabusderrabus deleted the types/ed-properties branchJuly 3, 2021 10:03
nicolas-grekas added a commit that referenced this pull requestJul 3, 2021
…errabus)This PR was merged into the 6.0 branch.Discussion----------[DependencyInjection] Add types to private properties| Q             | A| ------------- | ---| Branch?       | 6.0| Bug fix?      | no| New feature?  | no| Deprecations? | no| Tickets       | N/A| License       | MIT| Doc PR        | N/ASame procedure as#41924, but on a more complex component.Commits-------069da20 [DependencyInjection] Add types to private properties
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof approved these changes

Assignees

No one assigned

Labels

EventDispatcherRFCRFC = Request For Comments (proposals about features that you want to be discussed)Status: Reviewed

Projects

None yet

Milestone

6.0

Development

Successfully merging this pull request may close these issues.

5 participants

@derrabus@stof@fabpot@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp