codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.39k stars 1.9k forks source link

Bug: Access to an undefined property CodeIgniter\Session\Session::$something when running phpstan #6779

Closed hurtsky closed 2 years ago

hurtsky commented 2 years ago

PHP Version

8.1

CodeIgniter4 Version

develop

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MariaDB 10.4

What happened?

after updating to latest daed3f82e090aa1f61e3f2f9fd9482054e35f9b1

im getting alot of error of Access to an undefined property CodeIgniter\Session\Session::$something. when running composer analyze , which did not happen before.

it seem the error only produce if I try getting the session through the magic getter.. session()->something , if using session()->get('something') function phpstan not complaining anything.

Steps to Reproduce

   public function test()
    {
        $data['name'] = name(session()->user_id);
    }

and run composer analyze

Expected Output

should not have error because it doesn't trigger any error before merging the new update

Anything else?

No response

kenjis commented 2 years ago

Because CodeIgniter\Session\Session::$something is not defined, so the PHPStan errors seem to be reasonable.

should not have error because it doesn't trigger any error before merging the new update

It is not true. If the checking of PHPStan gets more strict, a new error might be reported even if you don't change anything.

hurtsky commented 2 years ago

but @kenjis the use of session()->something is documented on documentation. so it should not produce error, or documentation need an update?

paulbalandan commented 2 years ago

PHPStan, as a static analyzer, has no way of telling that accessing the members of session() will yield something. It will know that if it runs the code but running the code is not what static analysis do.

paulbalandan commented 2 years ago

It is not a bug on the framework. It is just that PHPStan has no mechanism to check that. However, if we can somehow create a return type specifying extension for session(), we can aide PHPStan in analysing the code.