Steps to reproduce the issue
function repro(\Joomla\CMS\MVC\Factory\MVCFactoryInterface $factory): void
{
$factory->createModel('Articles');
}
libraries/vendor/bin/phpstan analyse
5.4
The analysis completes. Since $prefix is an optional parameter (createModel($name, $prefix = '', array $config = [])), calling the method with a single argument is valid code and
must not break static analysis.
The whole PHPStan run aborts:
Internal error: Call to a member function getAttribute() on null while analysing file .../repro.php
In CI this fails the phpstan job for any PR touching such a file, with an error message that points to the analysed file instead of the actual culprit, making it very hard for
contributors to understand what is wrong.
The custom dynamic return type extension build/phpstan/src/DynamicReturnType/MVCFactoryInterfaceInterface.php guards only against calls without any arguments, but then
unconditionally reads the second argument:
if (\count($methodCall->getArgs()) === 0) {
return null;
}
$name = str_replace("'", '', $methodCall->getArgs()[0]->value->getAttribute('rawValue'));
$prefix = str_replace("'", '', $methodCall->getArgs()[1]->value->getAttribute('rawValue'));
For a one-argument call, getArgs()[1] is null; reading ->value on it yields null (warning only), and the subsequent getAttribute() call is the fatal error reported by PHPStan.
###Suggested fix
Bail out whenever fewer than the two required arguments are present, since the extension cannot resolve a class name without the prefix anyway:
if (\count($methodCall->getArgs()) < 2) {
return null;
}
Aditionally a custom rule for phpstan could be considered to generate a warning if one param is used. As far as I see the usage of just one param is deprecated for all subclasses of MVCFactoryInterface but the LegacyFactory
Problem applies also to createView and createTable methods
| Labels |
Added:
No Code Attached Yet
bug
|
||
@Hackwar would appreciate your opinion on this.