cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: PHPStan static analysis ends up with internal errors

Open kristytoman opened this issue 2 years ago • 5 comments

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 with Child 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

  1. 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
  2. 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

kristytoman avatar Jul 14 '22 14:07 kristytoman

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).

image

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).

Jeckerson avatar Jul 14 '22 18:07 Jeckerson

Related https://github.com/phpstan/phpstan/issues/7590

Jeckerson avatar Jul 15 '22 09:07 Jeckerson

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.

ondrejmirtes avatar Jul 15 '22 09:07 ondrejmirtes

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.

ondrejmirtes avatar Jul 17 '22 06:07 ondrejmirtes

Please fix that in the extension.

Thank you! I'll check it.

Jeckerson avatar Jul 17 '22 08:07 Jeckerson

$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
    {
    }
}

Jeckerson avatar Aug 23 '22 07:08 Jeckerson

Resolved in https://github.com/phalcon/cphalcon/pull/16066

niden avatar Aug 23 '22 11:08 niden

Released in v5.0.0

Jeckerson avatar Sep 22 '22 23:09 Jeckerson