cphalcon
cphalcon copied to clipboard
[BUG]: PHPStan static analysis ends up with internal errors
Describe the bug We've upgraded to phalcon 5.0.0-RC3 and PHP8.1 on our projects and we have troubles with running static analysis on it. Currently found problems:
- Files using
Phalcon\Events\Event
fails with
Uncaught PHPStan\BetterReflection\SourceLocator\Ast\Exception\ParseToAstFailure: AST failed to parse in located source (first 20 characters: <?php
namespace Pha)`
- Files using
Phalcon\Mvc\Model
ends up withChild process error (exit code 139): Segmentation fault (core dumped)
Unfortunately I couldn't get anything more specific than this message. I wrote an issue about the first one to PHPStan and I'm waiting for a response.
To Reproduce We have not found a problem with Phalcon other than the static analysis file
- We use dockerfile with
- PHP version = 8.1
- Zephir parser version = 1.5.0
- Zephir version = 0.16.0
- Phalcon version = 5.0.0RC3
- Operating system = Debian Bullseye
- server = Apache2
- In composer.json we have
- phalcon/ide-stubs version = 5.0.0-RC1
- phpstan/phpstan = 1.8.1
all you need is just mention the class in the analysed class and then when you run vendor/bin/phpstan analyse ./sourcepath
it fails
Expected behavior PHPStan analysis ends up with zero internal errors caused by using Phalcon
After several tests with different versions of PHP, Phalcon and PHPStan. It seems that error is not in Phalcon, but in PHPStan, since version 1.7.0
(https://github.com/phpstan/phpstan/releases/tag/1.7.0).
According to this blog post - https://phpstan.org/blog/zero-config-analysis-with-static-reflection, since version 1.7.0
, since there it uses AST tree reflection, which might have some internal problems while reading external package (Phalcon).
Related https://github.com/phpstan/phpstan/issues/7590
My personal guess is that Phalcon uses some method name like throw
- something that wouldn't be possible to parse and write in userland, but is possible to declare in an extension.
I've added a bit more debugging capability to PHPStan so it should tell us what's the problem.
The root issue here is that Phalcon\Events\Event
has some weird corrupted info in reflection.
The getSource()
method reports return type ((void*)0)
(ReflectionNamedType::getName()
) which isn't valid. Please fix that in the extension. Thanks.
Please fix that in the extension.
Thank you! I'll check it.
$stubber = new \Roave\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber();
$data = $stubber->generateClassStub(\Phalcon\Events\Event::class);
echo $data->getStub();
Now it generates correct stubs:
<?php
namespace Phalcon\Events;
class Event implements \Phalcon\Events\EventInterface
{
protected $cancelable = null;
protected $data = null;
protected $source = null;
protected $stopped = false;
protected $type = null;
public function getData()
{
}
public function getType() : string
{
}
public function __construct(string $type, $source, $data, bool $cancelable)
{
}
public function isCancelable() : bool
{
}
public function isStopped() : bool
{
}
public function getSource() : ?object
{
}
public function setData($data) : \Phalcon\Events\EventInterface
{
}
public function setType(string $type) : \Phalcon\Events\EventInterface
{
}
public function stop() : \Phalcon\Events\EventInterface
{
}
}
Resolved in https://github.com/phalcon/cphalcon/pull/16066
Released in v5.0.0