-
Hello, Working on a little plugin over here that introspects the parameters of callable chains in a pipe combinator function: https://github.com/azjezz/psl/blob/2.0.x/src/Psl/Fun/pipe.php $stages = pipe();
fn (string $x): int => 2,
fn (int $y): float => 1.2,
fn (float $z): int => 23
); It inspects that the output type of the first function matchtes the input of the second function etc. https://gist.github.com/veewee/db5b2ad4868d9739732acafbbf7ef2f5 There is a supported case in which no stages are provided to the function: $x = pipe()('hello');
> $x = string('hello')
$y = pipe()(123);
> $y = int(123) In that case, the function just returns its input: This is the input: use function Psl\Fun\pipe;
$stages = pipe();
$res = $stages('hello');
/** @psalm-trace $stages */
/** @psalm-trace $res */ Tried various options like: $templated = new Type\Union([
new Type\Atomic\TTemplateParam($name, new Type\Union([Type\Atomic::create('mixed')]), 'fn-'.$event->getFunctionId())
]);
return new Type\Union([
new Type\Atomic\TClosure(
value: Closure::class,
params: [
new FunctionLikeParameter(
'input',
false,
$templated,
is_optional: false,
is_nullable: false,
is_variadic: false,
)
],
return_type: $templated,
)
]); Output:
I would expect psalm to know it is a string in this case. An clue on how I can configure the plugin to make it work with templated parameters so that it knows the end result? Context:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
Played around with this a bit more and haven't really found a solution. It looks like an issue in which the Not sure if I need to / can set something in e.g. the parent context, codebase, ... object. |
Beta Was this translation helpful? Give feedback.
-
I never tried something like that. However, I noticed that $event->getFunctionId() does not return a lowercase-string and this code transform the function id in lowercase in the third arg: Could it be related somehow? |
Beta Was this translation helpful? Give feedback.
-
@orklah Small side question related to above plugin. $nodeTypeProvider = $event->getStatementsSource()->getNodeTypeProvider();
$args = $event->getCallArgs();
$type = $nodeTypeProvider->getType($args[0]->value);
Would it make sense to somehow enable the detection of the resulting union type from the |
Beta Was this translation helpful? Give feedback.
Played around with this a bit more and haven't really found a solution.
As far as I can tell, the returned template type mentioned above is correct.
It looks like an issue in which the
FuncCall
to$stages
isn't replacing theTTemplateParam
with aTemplateResult
if the template type is applied through a plugin.Not sure if I need to / can set something in e.g. the parent context, codebase, ... object.