ModelHistory::causer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Hamidrezaniazi\Laramist\Models;
4
5
use Hamidrezaniazi\Laramist\Guard;
6
use Illuminate\Contracts\Auth\Authenticatable as User;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11
class ModelHistory extends Model
12
{
13
    protected $guarded = ['id'];
14
    protected $casts = [
15
        'changed' => 'array',
16
    ];
17
18
    /**
19
     * @return MorphTo
20
     */
21
    public function subject(): MorphTo
22
    {
23
        return $this->morphTo();
24
    }
25
26
    /**
27
     * @return BelongsTo
28
     */
29
    public function causer(): BelongsTo
30
    {
31
        return $this->belongsTo(Guard::getGuardClassName(), 'causer_id');
32
    }
33
34
    /**
35
     * @param Model $subject
36
     * @param User|null $user
37
     * @return void
38
     */
39
    public static function logChanges(Model $subject, ?User $user): void
40
    {
41
        if ($subject->wasChanged()) {
42
            $changed = $subject->getChanges();
43
            unset($changed['updated_at']);
44
            $history = new self();
45
            $history->subject()->associate($subject);
46
            $history->causer()->associate($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contra...h\Authenticatable>|null, but the function expects a object<Illuminate\Databa...t\Model>|integer|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
            $history->changed = $changed;
48
            $history->save();
49
        }
50
    }
51
}
52