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

An int constant used in parametersSchema isn't expanded to int during validation #10223

Closed
spaze opened this issue Nov 29, 2023 · 7 comments
Closed

Comments

@spaze
Copy link
Contributor

spaze commented Nov 29, 2023

Bug report

Hi,

I have an extension that uses the following config:

parameters:
	disallowedFunctionCalls:
		-
			allowParamFlagsAnywhere:
				2: ::ENT_QUOTES

ENT_QUOTES's value is int 3.

My parametersSchema contains this (simplified):

	disallowedFunctionCalls: listOf(
		structure([
			[...]
			?allowParamFlagsAnywhere: arrayOf(anyOf(int(), structure(...)), anyOf(int(), string())),
		])
	)

Meaning allowParamFlagsAnywhere can be an array of ints (or array of structures, irrelevant at this point).

But PHPStan complains that it sees a string '::ENT_QUOTES', not an int:

Invalid configuration:
The item 'parameters › disallowedFunctionCalls › 45 › allowParamFlagsAnywhere › 2' expects to be int|array, '::ENT_QUOTES' given.

I can easily change my config and add a comment, but I was wondering whether it's something that can be changed in PHPStan, or whether it would be too something (e.g. complicated, dangerous, ...). As far as I remember, the constant expansion is a feature of nette/di, and the constant is expanded/replaced to its value during the analysis, but not in the validation phase.

Code snippet that reproduces the problem

No response

Expected output

Configuration is validated fine.

Did PHPStan help you today? Did it make you happy in any way?

Yes, you deserve a cold one 🍺

Copy link

mergeable bot commented Nov 29, 2023

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

I just tried this and it works:

parameters:
	parallel:
		jobSize: ::constant(ENT_QUOTES)

This is a bit of a pain. I persuaded David to do nette/di#291 and it broke nette/di for many people for many versions - see the number of releases since then 😂

PHPStan validates parameters here https://github.com/phpstan/phpstan-src/blob/a49f7d43140a952695009561c99d999769281d63/src/DependencyInjection/ContainerFactory.php#L339 after container creation. It used to be in its own ParametersSchemaExtension but that broke because of the dynamic parameters.

So hopefully this isn't gonna break any time soon :)

@spaze
Copy link
Contributor Author

spaze commented Nov 29, 2023

Thanks, I just remembered I've seen the ::constant syntax earlier 😅 But when trying it, it works in the parameters scheme validation phase, but then the value of the parameter is:

object(Nette\Neon\Entity)#2876 (2) {
  ["value"]=>
  string(10) "::constant"
  ["attributes"]=>
  array(1) {
    [0]=>
    string(10) "ENT_QUOTES"
  }
}

instead of just 3.

Should this be handled on my side, or is that something PHPStan should do? I don't know, just asking :-)

@spaze
Copy link
Contributor Author

spaze commented Nov 29, 2023

Scratch that, this is my test not handling the ::constant(...) syntax, not anything else. Thanks!

@ondrejmirtes
Copy link
Member

Why are you doing this in so complicated way? :) You should put the test .neon file into getAdditionalConfigFiles: https://phpstan.org/developing-extensions/testing

That way you don't need to reimplement what PHPStan already does :)

getRule should probably just do return self::getContainer()->getByType(FunctionCalls::class).

@spaze
Copy link
Contributor Author

spaze commented Nov 29, 2023

Ah, nice, thanks. It was introduced after I wrote my first test and I missed it when it was introduced (or forgot) :-)

Also thanks for the getRule hint, that nicely simplifies the test, cool!

spaze added a commit to spaze/phpstan-disallowed-calls that referenced this issue Nov 29, 2023
…arametersSchema

The getRule method could be then simplified, too.

See phpstan/phpstan#10223
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants