Completed
Push — master ( b01581...4b6f45 )
by Freek
04:00
created

DetectsChanges::getPropertiesToBeLogged()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Activitylog\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
trait DetectsChanges
8
{
9
    protected $oldAttributes = [];
10
11
    protected static function bootDetectsChanges()
12
    {
13
        if (static::eventsToBeRecorded()->contains('updated')) {
14
            static::updating(function (Model $model) {
15
16
                //temporary hold the original attributes on the model
17
                //as we'll need these in the updating event
18
                $oldValues = $model->replicate()->setRawAttributes($model->getOriginal());
19
20
                $model->oldAttributes = static::logChanges($oldValues);
21
            });
22
        }
23
    }
24
25
    public function attributesToBeLogged(): array
26
    {
27
        if (!isset(static::$logAttributes)) {
28
            return [];
29
        }
30
31
        return static::$logAttributes;
32
    }
33
34
    public function attributeValuesToBeLogged(string $processingEvent): array
35
    {
36
        if (!count($this->attributesToBeLogged())) {
37
            return [];
38
        }
39
40
        $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...
41
42
        if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') {
43
            $nullProperties = array_fill_keys(array_keys($properties['attributes']), null);
44
45
            $properties['old'] = array_merge($nullProperties, $this->oldAttributes);
46
        }
47
48
        return $properties;
49
    }
50
51
    public static function logChanges(Model $model): array
52
    {
53
        return collect($model)->only($model->attributesToBeLogged())->toArray();
54
    }
55
}
56