Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace Sofa\Revisionable\Laravel; |
||
6 | class Revision extends Model |
||
7 | { |
||
8 | /** |
||
9 | * The database table used by the model. |
||
10 | * |
||
11 | * @var string |
||
12 | */ |
||
13 | protected static $customTable; |
||
14 | |||
15 | /** |
||
16 | * Allow mass assignement. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $fillable = ['*']; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | public static function boot() |
||
26 | { |
||
27 | parent::boot(); |
||
28 | |||
29 | // Make it read-only |
||
30 | static::saving(function () { |
||
31 | return false; |
||
32 | }); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get array of updated fields. |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getUpdated() |
||
44 | |||
45 | /** |
||
46 | * Get diff of the old/new arrays. |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | public function getDiff() |
||
51 | { |
||
52 | $diff = []; |
||
53 | |||
54 | foreach ($this->getUpdated() as $key) { |
||
55 | $diff[$key]['old'] = $this->old($key); |
||
56 | |||
57 | $diff[$key]['new'] = $this->new($key); |
||
58 | } |
||
59 | |||
60 | return $diff; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Determine whether field was updated during current action. |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @return boolean |
||
68 | */ |
||
69 | public function isUpdated($key) |
||
73 | |||
74 | /** |
||
75 | * Accessor for old property |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getOldAttribute($old) |
||
83 | |||
84 | /** |
||
85 | * Accessor for new property |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | public function getNewAttribute($new) |
||
93 | |||
94 | /** |
||
95 | * Get single value from the new/old array. |
||
96 | * |
||
97 | * @param string $version |
||
98 | * @param string $key |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function getFromArray($version, $key) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function getTable() |
||
115 | |||
116 | /** |
||
117 | * Set custom table name for the model. |
||
118 | * |
||
119 | * @param string $table |
||
120 | * @return void |
||
121 | */ |
||
122 | public static function setCustomTable($table) |
||
128 | |||
129 | /** |
||
130 | * Handle dynamic method calls. |
||
131 | * |
||
132 | * @param string $method |
||
133 | * @param array $parameters |
||
134 | * @return mixed |
||
135 | */ |
||
136 | public function __call($method, $parameters) |
||
150 | } |
||
151 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.