UseMuffin / Footprint

CakePHP plugin to allow passing currently logged in user to model layer.
MIT License
95 stars 23 forks source link

CakePHP 3.6.12 Notice - Undefined property: UsersController::$_currentUserViewVar #55

Closed reloxx13 closed 6 years ago

reloxx13 commented 6 years ago

Heya,

PHP Version 7.0.30-0+deb9u1 Apache/2.4.25 (Debian) Linux 4.9.0-8-amd64 #1 SMP Debian 4.9.110-3+deb9u4 (2018-08-21) x86_64

just updated cakephp3 from 3.6.11 to 3.6.12 and now i get a notice after login submit.

Notice (1024): Undefined property: UsersController::$_currentUserViewVar in /vendor/muffin/footprint/src/Auth/FootprintAwareTrait.php on line 81 [CORE/src/Controller/Controller.php, line 387]

i think it comes from this change: CakePHP 3.6.12 released cakephp/cakephp#PR12593

Undefined controller properties now emit warnings when accessed. This prevents 'invalid method call on null' type errors.

Cuz $this is an object(App\Controller\UsersController) in that context.

Workaround: In my UsersController i just added the property

class UsersController extends AppController
{
    protected $_currentUserViewVar; // Workaround 3.6.12 notice

    public function initialize()
    {

FIX: Add this line in vendor/muffin/footprint/src/Auth/FootprintAwareTrait.php L81 to fix:

isset($this->_currentUserViewVar) && //FIX 3.6.12 notice

    protected function _getCurrentUser($user = NULL)
    {
        $this->_setCurrentUser($user);

        if (!$this->_currentUserInstance &&
            isset($this->_currentUserViewVar) && //FIX 3.6.12 notice
            !empty($this->viewVars[$this->_currentUserViewVar])
        )
        {
            $this->_currentUserInstance = $this->viewVars[$this->_currentUserViewVar];
        }

        return $this->_currentUserInstance;
    }

But i dont know where this "_currentUserViewVar" comes from or why it is not set.

EDIT: use isset and not empty in fix like ADmad did in the pr

SD-dthompson commented 6 years ago

We got a similar error, from a different controller in our code, but I believe the same issue:

(1024): Undefined property: WebPagesController::$_currentUserViewVar in [local_directory]/vendor/muffin/footprint/src/Auth/FootprintAwareTrait.php on line 81 [CORE/src/Controller/Controller.php, line 387]

our workaround, we added the property in the AppController (which our WebPagesController extends) as a public property:

`class AppController extends BaseController
{
    use FootprintAwareTrait;    
    public $_currentUserViewVar;
        ...`

We're curious to know more about what is intended to happen with this property that was previously undefined but didn't seem to pose any problems. As reloxx13 mentioned, it seems like the warning is due to a change in behavior in CakePHP release 3.6.12 :

Undefined controller properties now emit warnings when accessed. This prevents 'invalid method call on null' type errors.