CI accessing property on interface

177 views Asked by At

I'm using scrutinizer to analyze my code. And almost everything has been fixed but I can't seem to fix this issue.

Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

I see the problem beceause it's an interface that propery does not exist, but my code works just fine. So how can I let this code pass when I analyze my code?

Thanks

Edit

Got the error on these lines (and some other files are almost the same)

$this->tracert->log(
            'users',
            $this->auth->user()->id,
            $this->auth->user()->id,
            'Login'
        );

The constructor of that class

 /**
 * @param \Illuminate\Contracts\Auth\Guard $auth
 * @param \jorenvanhocht\Tracert\Tracert $tracert
 */
public function __construct(Guard $auth, Tracert $tracert)
{
    parent::__construct($auth);
    $this->tracert = $tracert;
}

constructor of my base controller:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
    $this->config = objectify(config('blogify'));
    $this->auth_user = $this->auth->check() ? $this->auth->user() : false;
}

The used contract https://github.com/illuminate/contracts/blob/master/Auth/Guard.php

1

There are 1 answers

3
venca On BEST ANSWER

To fix the problem for the id of the authenticated user, you should use:

$this->auth->user()->getAuthIdentifier()

Interface consists of methods. You are accessing attribute directly. eg $foo->id instead of $foo->getId(). And you have to add new method to interface of course.

The workaround is to say to scrutinizer, that $object is instance of desired class.

if ($object instanceof MyClass) {
    //
}