Skip to content

Support ReflectionMethod + getParameters #14726

Closed

Description

@momala454

Bug report

When an array is created using ReflectionMethod and getParameters, phpstan does not know the possible values.

Code snippet that reproduces the problem

https://phpstan.org/r/b37689ff-7c03-4ca8-bd3d-f873defcebe4

Expected output

No error on the line echo $properties['world']; because we know that world is a parameter to the constructor.

Current error returned : Offset 'world' might not exist on array<non-empty-string, mixed>.

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

No response

Activity

  1. changed the title [-]Support ReflectionMethod[/-] [+]Support ReflectionMethod + getParameters[/+] on May 29, 2026
  2. staabm commented on May 30, 2026

    @staabm
    Contributor

    I am not sure the suggestion is useful.

    If you write code which relys on reflection behavior, you should guard it against possible changing data.
    IMO In case you write code which expects a certain structure, you could even already hardcode against the real class you are reflecting against…

    The whole point of reflection is that you don’t know/assume the structure beforehand, therefore you need to be defensive

    wdyt?

  3. momala454 commented on Jun 1, 2026

    @momala454
    Author

    The use case is to convert an object from a parent class to a child class. The parent class contains a lot of arguments in the constructor, and also contains some properties that are not in the constructor.

    The child class add a few new parameters in the constructor.

    I prefer not to write all the parameters of the constructor manually (in the method "fromHelloWorld" in the example) as there are a lot, so I used ReflectionMethod.

    Example: https://phpstan.org/r/1dd57bc2-8d19-4f48-b095-6162dd2d435e

  4. added a commit that references this issue on Jun 4, 2026
    090bb24
  5. staabm commented on Jun 4, 2026

    @staabm
    Contributor

    I had a talk with ondrej about this and he shares my opinion.

    as soon as you are using reflection you are crossing a line in which you cannot/should not expect static analysis to support your code anymore (by design)

  6. github-actions commented on Jul 6, 2026

    @github-actions

    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.

  7. locked as resolved and limited conversation to collaborators on Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions