4

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

Joren Van Hocht
  • 845
  • 1
  • 9
  • 21
  • 2
    u should use `$this->auth->user()->getAuthIdentifier()` – venca Jun 11 '15 at 08:50
  • If a look at definition *public function log($model, $row, $user_id, $action = 'created')*, dont know why you need *$row* and *$user_id*, but ok... I changed it to `log($model, \Illuminate\Contracts\Auth\Authenticatable $user, $action = 'created')` and in method pull $user()->getAuthIdentifier() as id – venca Jun 11 '15 at 09:02
  • Seems like getAuthIdentifier() fixed the problem. But how would you go over fixing it on a relation like this: $this->auth->user()->role->name or on another column then the id like: $this->auth->user()->role_id ? – Joren Van Hocht Jun 11 '15 at 09:40
  • 2
    problem is pretty clear scrutinizer doesnt know if the attrib is in your instance. i would try to cover *problematic code* in `if ($object instanceof YourClass)`. I assume `$object = new YourClass()`. – venca Jun 11 '15 at 10:20
  • 1
    Thanks a lot, you helped me very good! – Joren Van Hocht Jun 11 '15 at 11:00
  • I updated my answer with this workaround. – venca Jun 11 '15 at 11:05

1 Answers1

6

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) {
    //
}
Joren Van Hocht
  • 845
  • 1
  • 9
  • 21
venca
  • 1,196
  • 5
  • 18