Passed
Push — master ( 36c463...758946 )
by Carlos
02:32
created

Version   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 76
ccs 20
cts 23
cp 0.8696
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A versionable() 0 4 1
A createForModel() 0 15 1
A revert() 0 4 1
A diff() 0 16 4
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-versionable.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace Overtrue\LaravelVersionable;
12
13
use Illuminate\Database\Eloquent\Model;
14
use Illuminate\Support\Arr;
15
use SebastianBergmann\Diff\Differ;
16
17
/**
18
 * Class Version.
19
 *
20
 * @property Model $versionable
21
 * @property array $contents
22
 */
23
class Version extends Model
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $casts = [
29
        'contents' => 'array',
30
    ];
31
32
    /**
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35
    public function user()
36
    {
37
        return $this->belongsTo(\config('versionable.user_model'), \config('versionable.user_foreign_key'));
38
    }
39
40
    /**
41
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
42
     */
43 2
    public function versionable()
44
    {
45 2
        return $this->morphTo('versionable');
46
    }
47
48
    /**
49
     * @param \Illuminate\Database\Eloquent\Model $model
50
     *
51
     * @return \Overtrue\LaravelVersionable\Version
52
     */
53 5
    public static function createForModel(Model $model)
54
    {
55 5
        $versionClass = $model->getVersionModel();
56
57 5
        $version = new $versionClass();
58
59 5
        $version->versionable_id = $model->getKey();
60 5
        $version->versionable_type = $model->getMorphClass();
61 5
        $version->{\config('versionable.user_foreign_key')} = \auth()->id();
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62 5
        $version->contents = $model->getVersionableAttributes();
63
64 5
        $version->save();
65
66 5
        return $version;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 1
    public function revert()
73
    {
74 1
        return $this->versionable->forceFill($this->contents)->save();
75
    }
76
77
    /**
78
     * @param \Illuminate\Database\Eloquent\Model|null $model
79
     *
80
     * @return string
81
     */
82 1
    public function diff(Model $model = null)
83
    {
84 1
        $model || $model = $this->versionable;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $model. This often makes code more readable.
Loading history...
85
86 1
        if ($model instanceof Version) {
87 1
            $source = $model->contents;
88
        } else {
89 1
            if (!\in_array(Versionable::class, \class_uses($model))) {
90
                throw new \InvalidArgumentException(\sprintf('Model %s is not versionable.', \get_class($model)));
91
            }
92
93 1
            $source = $model->versionableFromArray($this->versionable->toArray());
0 ignored issues
show
Bug introduced by
The method versionableFromArray cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
94
        }
95
96 1
        return (new Differ())->diff(Arr::only($source, \array_keys($this->contents)), $this->contents);
97
    }
98
}
99