Skip to content

[DependencyInjection] More bullet-proof expression evaluation #59976

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

Open
wants to merge 4 commits into
base: 6.4
Choose a base branch
from

Conversation

Wirone
Copy link
Contributor

@Wirone Wirone commented Mar 14, 2025

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues n/a
License MIT

As asked here, we faced weird issue recently. I was able to reproduce it with minimal DI setup in CheckTypeDeclarationsPassTest.

@carsonbot

This comment was marked as outdated.

@Wirone Wirone force-pushed the codito/full-bulletproof-for-evaluating-expression branch from 4266c4a to 1a7bf06 Compare March 14, 2025 14:21
@Wirone Wirone marked this pull request as ready for review March 14, 2025 14:36
@carsonbot carsonbot added this to the 7.2 milestone Mar 14, 2025
@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.2" but it seems your PR description refers to branch "6.4, 7.2".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@Wirone Wirone force-pushed the codito/full-bulletproof-for-evaluating-expression branch from f22ebf9 to 0dcb2e6 Compare March 14, 2025 14:38
Wirone added 2 commits March 14, 2025 15:51
In some scenarios evaluating expression can lead to TypeError (unresolved DI params being strings, not expected integers etc).
@Wirone Wirone force-pushed the codito/full-bulletproof-for-evaluating-expression branch from 0dcb2e6 to 247f0f9 Compare March 14, 2025 14:52
@Wirone Wirone changed the base branch from 7.2 to 6.4 March 14, 2025 14:52
@OskarStark OskarStark modified the milestones: 7.2, 6.4 Mar 14, 2025
Wirone added 2 commits March 17, 2025 07:51
`composer.json`'s requirement for PHP is `PHP >= 8.1`, so even though GH Actions does not contain PHP 8.1 in the matrix (which is wrong IMHO), let's stick with `readonly` on property level.
@Wirone
Copy link
Contributor Author

Wirone commented Mar 17, 2025

I've additionally fixed PHP 8.1 compatibility (readonly on fixture class level, I have this as a default in IDE and just today saw the red underline in files 😉). What's interesting is why previous CI run did not run tests on PHP 8.1, and latest one did 😅.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

So, the error you describe on Twitter happens before env vars are unresolved, while running the expression.
I feel like the solution that's proposed at the moment is way to much. What if the error is a parse error or anything else that'd better be caught at linting time?
Sorry I don't have a proposal, that's just the question that comes up at the moment :)

@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);
Copy link
Member

Choose a reason for hiding this comment

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

to be replaced by the licence header (same below)

Copy link
Member

Choose a reason for hiding this comment

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

also in fixture file?
if so, we could run the Fixer to apply this for other fixture files

Copy link
Member

Choose a reason for hiding this comment

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

the thing is we'd like the licence header in fixtures (because why not), but then, no need to enforce other rules I'd say (at least we'll get many false-positives if we do)

Copy link
Member

Choose a reason for hiding this comment

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

and PHP-CS-Fixer only allows to exclude files entirely, not to override the config being applied to some files to apply only some of the rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we definitely need to introduce skipping rules like ECS has, I like it a lot.

But you can also have a separate ruleset for fixtures and run Fixer twice, pointing to the config file.

@Wirone
Copy link
Contributor Author

Wirone commented Mar 20, 2025

I feel like the solution that's proposed at the moment is way to much. What if the error is a parse error or anything else that'd better be caught at linting time?

@nicolas-grekas I've been thinking about your question last 2 days and my initial opinion did not change - lint:container is IMHO not a place for spotting syntax errors. These should be caught in a separate job (php -l or parallel lint), the last moment for it could be cache:clear. I think lint:container should focus only on DI definition and skip all the errors related to initialising services. Ideally it shouldn't initialise them in the first place, but I get that in order to match signature's expected type with other service's type or expression' return type it has to be done. Anyway, I think my fix is valid, as it aligns with the comment that is right there:

If a service from the expression cannot be fetched from the container, we skip the validation.

In our case it's runtime issue, but for other scenarios it can be actually language-level issue - none of them should break the DI linting (maybe flag for this would be a good idea, so you could run lint:container --stop-on-error or something like that).

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 20, 2025

What about checking the error message and ignoring only the ones we know we want to ignore?
I don't agree with you about linting not being the place to spot parse errors: any opportunity to spot mistakes as early as possible is a win. False-positives are what is bad, and I would care about them one by one.

@nicolas-grekas
Copy link
Member

Up to follow my last suggestion @Wirone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants