Issues (49)

app/Repositories/AbstractRepository.php (2 issues)

1
<?php
2
3
namespace App\Repositories;
4
5
use Exception;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
10
abstract class AbstractRepository implements RepositoryInterface
11
{
12
    /** @var Model */
13
    protected $model;
14
15
    /** @var Guard */
16
    protected $auth;
17
18
    abstract public function getModelClass(): string;
19
20 132
    public function __construct()
21
    {
22 132
        $this->model = app($this->getModelClass());
0 ignored issues
show
Documentation Bug introduced by
It seems like app($this->getModelClass()) can also be of type Illuminate\Contracts\Foundation\Application. However, the property $model is declared as type Illuminate\Database\Eloquent\Model. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
23
24
        // This instantiation may fail during a console command if e.g. APP_KEY is empty,
25
        // rendering the whole installation failing.
26
        try {
27 132
            $this->auth = app(Guard::class);
28
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
29
        }
30 132
    }
31
32 13
    public function getOneById($id): ?Model
33
    {
34 13
        return $this->model->find($id);
35
    }
36
37 7
    public function getByIds(array $ids): Collection
38
    {
39 7
        return $this->model->whereIn($this->model->getKeyName(), $ids)->get();
40
    }
41
42
    public function getAll(): Collection
43
    {
44
        return $this->model->all();
45
    }
46
}
47