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

ReflectionClass::newInstanceArgs() accepts associative arrays #9077

Closed
BenMorel opened this issue Jan 7, 2023 · 3 comments · Fixed by #9085
Closed

ReflectionClass::newInstanceArgs() accepts associative arrays #9077

BenMorel opened this issue Jan 7, 2023 · 3 comments · Fixed by #9085

Comments

@BenMorel
Copy link
Contributor

BenMorel commented Jan 7, 2023

Since PHP 8, ReflectionClass::newInstanceArgs() accepts an associative array of parameter names.

This code works (https://3v4l.org/L0kj4):

class X {
    public function __construct(
        string $a,
        string $b,
    ) {
        echo "\$a = $a\n";
        echo "\$b = $b\n";
    }
}

$r = new ReflectionClass(X::class);
$r->newInstanceArgs([
    'b' => 1,
    'a' => 2,
]);

But Psalm fails with:

ERROR: InvalidArgument - 14:21 - Argument 1 of ReflectionClass::newInstanceArgs expects array<int, mixed>, but array{a: 2, b: 1} provided

https://psalm.dev/r/835445aea1

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/835445aea1
<?php

class X {
    public function __construct(
        public readonly string $a,
        public readonly string $b,
    ) {
    }
}

$r = new ReflectionClass(X::class);
$r->newInstanceArgs([
    'b' => 1,
    'a' => 2,
]);
Psalm output (using commit 1350992):

ERROR: InvalidArgument - 12:21 - Argument 1 of ReflectionClass::newInstanceArgs expects array<int, mixed>, but array{a: 2, b: 1} provided

@BenMorel
Copy link
Contributor Author

BenMorel commented Jan 7, 2023

I tried to fix it, but I fail to understand the hierarchy of CallMaps that leads Psalm to fail on this example on PHP 8:

  • CallMap.php has types that should work:
    'ReflectionClass::newInstanceArgs' => ['object', 'args='=>'array<array-key, mixed>'],
    'ReflectionObject::newInstanceArgs' => ['object', 'args='=>'array'],
  • CallMap_historical.php has the old (i.e. pre- PHP 8) type, and a permissive type for ReflectionObject:
    'ReflectionClass::newInstanceArgs' => ['object', 'args='=>'list<mixed>'],
    'ReflectionObject::newInstanceArgs' => ['object', 'args='=>'array'],
  • CallMap_80_delta.php has both the old and the new types correct:
    'ReflectionClass::newInstanceArgs' => [
      'old' => ['object', 'args='=>'list<mixed>'],
      'new' => ['object', 'args='=>'array<array-key, mixed>'],
    ],

I think a sane validation would be (for both ReflectionClass and ReflectionObject):

  • PHP 7: list<mixed>
  • PHP 8: list<mixed>|array<string, mixed>

(even though newInstanceArgs() also accepts arrays with out-of-order int keys, but ignores keys in this case).

@orklah
Copy link
Collaborator

orklah commented Jan 7, 2023

@BenMorel new stubs were introduced for Reflection classes: https://github.com/vimeo/psalm/blob/master/stubs/Reflection.phpstub

They take precedence over the callmaps, your fix should belong here

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

Successfully merging a pull request may close this issue.

2 participants