DetectsChanges   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A bootDetectsChanges() 0 11 2
A attributesToBeLogged() 0 7 2
A attributeValuesToBeLogged() 0 12 4
A logChanges() 0 4 1
1
<?php
2
namespace LaiVu\ActivityLog\Traits;
3
use Illuminate\Database\Eloquent\Model;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: Lai Vu
8
 * Date: 10/25/2016
9
 * Time: 10:10 AM
10
 */
11
trait DetectsChanges
12
{
13
    protected $oldAttributes = [];
14
    protected static function bootDetectsChanges()
15
    {
16
        if (static::eventsToBeRecorded()->contains('updated')) {
17
            static::updating(function (Model $model) {
18
                //temporary hold the original attributes on the model
19
                //as we'll need these in the updating event
20
                $oldValues = $model->replicate()->setRawAttributes($model->getOriginal());
21
                $model->oldAttributes = static::logChanges($oldValues);
22
            });
23
        }
24
    }
25
    public function attributesToBeLogged()
26
    {
27
        if (! isset(static::$logAttributes)) {
28
            return [];
29
        }
30
        return static::$logAttributes;
31
    }
32
    public function attributeValuesToBeLogged( $processingEvent)
33
    {
34
        if (! count($this->attributesToBeLogged())) {
35
            return [];
36
        }
37
        $properties['attributes'] = static::logChanges($this);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$properties was never initialized. Although not strictly required by PHP, it is generally a good practice to add $properties = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
        if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') {
39
            $nullProperties = array_fill_keys(array_keys($properties['attributes']), null);
40
            $properties['old'] = array_merge($nullProperties, $this->oldAttributes);
41
        }
42
        return $properties;
43
    }
44
    public static function logChanges(Model $model)
45
    {
46
        return collect($model)->only($model->attributesToBeLogged())->toArray();
47
    }
48
}