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

ProxyGenerator missing assert() for getDeclaringMethod() return value in getParameterType() #922

Closed
GameplayJDK opened this issue Jan 26, 2021 · 2 comments

Comments

@GameplayJDK
Copy link
Contributor

GameplayJDK commented Jan 26, 2021

Hello everyone,

while working on #917 i noticed a missing assert() in the method getParameterType() of the ProxyGenerator.

The ReflectionParameter::getDeclaringFunction() method returns a ReflectionFunctionAbstract, the type hint of ProxyGenerator::formatType(...) is ReflectionMethod, one of two possible implementations, the other is ReflectionFunction.

    /**
     * @return string|null
     */
    private function getParameterType(ReflectionParameter $parameter)
    {
        if (! $parameter->hasType()) {
            return null;
        }

        return $this->formatType($parameter->getType(), $parameter->getDeclaringFunction(), $parameter);
    }

I'd propose a change from the above code to the code below:

    /**
     * @return string|null
     */
    private function getParameterType(ReflectionParameter $parameter)
    {
        if (! $parameter->hasType()) {
            return null;
        }

        $declaringFunction = $parameter->getDeclaringFunction();

        assert($declaringFunction instanceof ReflectionMethod);

        return $this->formatType($parameter->getType(), $declaringFunction, $parameter);
    }

3v4l.org snippet

Of course the assertion will always be true, but this theoretical type mismatch was pointed out by my IDE, which confused me at first. In my opinion this would make the code more clean and prevent any future confusion about what type is expected.

@GameplayJDK GameplayJDK changed the title ProxyGenerator missing assert() for declaring ProxyGenerator missing assert() for getDeclaringMethod() return value in getParameterType() Jan 26, 2021
GameplayJDK added a commit to GameplayJDK/doctrine-common that referenced this issue Jan 26, 2021
@malarzm
Copy link
Member

malarzm commented Jan 26, 2021

@GameplayJDK it'd be better to just open the PR with the description from here :)

@GameplayJDK
Copy link
Contributor Author

I'll edit it accordingly, thanks 👍

greg0ire added a commit that referenced this issue Feb 6, 2021
…sing-assert

Fix #922, ProxyGenerator missing assert
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

No branches or pull requests

2 participants