dereuromark/cakephp-ide-helper

PHPStorm Meta File generation fails: "Call to undefined method ReflectionUnionType::getName()"

passchn opened this issue · 4 comments

When trying to create a Meta File for PHPStorm, I run into this exception:

➜   bin/cake phpstorm generate   
Exception: Call to undefined method ReflectionUnionType::getName()
In [/Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php, line 211]

2021-12-22 08:59:10 error: [Error] Call to undefined method ReflectionUnionType::getName() in /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php on line 211
Stack Trace:
- /Users/pascalschneider/Sites/xxxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php:170
- /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php:96
- /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php:73
- /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/Task/TableFinderTask.php:51
- /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/TaskCollection.php:149
- /Users/pascalschneider/Sites/xxx/vendor/dereuromark/cakephp-ide-helper/src/Generator/PhpstormGenerator.php:33
- ...

getName() does not exist on ReflectionUnionType:
https://www.php.net/manual/en/class.reflectiontype.php#125396

From my composer.json:

    "require": {
        "php": ">=8.0",
        "cakephp/cakephp": "^4.3",
        [...]
    },
    "require-dev": {
        "cakedc/cakephp-phpstan": "^2.0",
        "cakephp/cakephp-codesniffer": "^4.5.1",
        "dereuromark/cakephp-ide-helper": "^1.17.0",
        "phpstan/extension-installer": "^1.1",
        "phpstan/phpstan": "^1.0.0",
        [...]
    },

I did not find any way to fix the error from the documentation and also have not found any issues on GitHub or Slack about this exception. As this seems to be a php8-related issue, I do not see any way to fix this on my side. My aim is to reduce error messages from PHPStorm when using the fetchTable() method from CakePHP4.3.

When debugging in TableFinderTask.php before line 211, I found out which part of my code raised the issue:

/** @var \ReflectionNamedType|null $parameterType */
$parameterType = $parameter->getType();

/**
 * Debug
 */
if (!method_exists($parameterType, 'getName')) {
    debug($parameterType);
    debug($parameters);
    debug($method);
    debug($className);
    die();
} 

if (!$parameterType || $parameterType->getName() !== Query::class) {
    return $result;
}

I had a union typed parameter (int|string) in a table method which lead to the error. I could not find out why, as other functions with union typed params work just fine.. However, removing the type fixed the issue for now.

Do you have an idea for a fix here as PR?

This works as it doesn't try to call getType on ReflectionUnionType:

if (!$parameterType || !method_exists($parameterType, 'getName') || $parameterType->getName() !== Query::class) {
    return $result;
}

I don't know if there are any side effects though.. :D

Also, I could not find out why it worked with other union typed parameters. If the exception makes sense somehow it would be nice to inform the user what method is causing the problem, like so:

if (!method_exists($parameterType, 'getName')) {
    throw new \Exception("Bad parameter type for $method in $className");
}

Feel free to make a PR with it. Sounds like it is a good way to solve this issue.