phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

`*ParameterClosureTypeExtension` for functions which accept a array of callables

Open staabm opened this issue 1 year ago • 4 comments

Feature request

using *ParameterClosureTypeExtension for preg_replace_callback in https://github.com/phpstan/phpstan-src/pull/3281 worked like a charm. I was able to easily wire togehter the RegexArrayShapeMatcher with the closure arg.

I try to do a similar thing for preg_replace_callback_array but since this function has a array-of-callables as a first arg my *ParameterClosureTypeExtension is not invoked.

example code

preg_replace_callback_array(
	[
		'~A([a]+)~i' => function ($match) {
		var_dump($match);
			\PHPStan\Testing\assertType('list<string>', $match);
		},
		'~B([b]+)?~i' => function ($match) {
			//var_dump($match);
		},
		'/(foo)?(bar)?(baz)?/' => function ($match) {
			//var_dump($match);
		},
	],
	$subject
);

I think we either need a way to handle array-of-callables, or we need to hardcode preg_replace_callback_array directly into NodeScopeResolver - or maybe there is another path I did not yet think of.

lets use this issue to figure out how this can/should be done :-)

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

No response

staabm avatar Aug 02 '24 10:08 staabm

//cc @canvural

staabm avatar Aug 02 '24 10:08 staabm

Oh, I'm not sure what to do about this, it's such a weird design...

ondrejmirtes avatar Aug 02 '24 10:08 ondrejmirtes

yeah, its weird.

one more datapoint: the github search leads only 8k occurences.. might be not worth supporting in the end

staabm avatar Aug 02 '24 10:08 staabm

We definitely don't need to force ourselves into a corner with a bad design, it's better to wait and maybe something emerges in the future.

ondrejmirtes avatar Aug 02 '24 10:08 ondrejmirtes