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 |
||
14 | class Category extends Model |
||
15 | { |
||
16 | use Validatable; |
||
17 | |||
18 | /** |
||
19 | * Constant for card active status |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | const ACTIVE = true; |
||
24 | /** |
||
25 | * Constant for card inactive status |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | const INACTIVE = false; |
||
30 | |||
31 | /** |
||
32 | * Timestamp enabled. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | public $timestamps = true; |
||
37 | |||
38 | /** |
||
39 | * Name of database table. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $table = 'card_category'; |
||
44 | |||
45 | /** |
||
46 | * List of allowed columns to be used in $this->fill(). |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $fillable = ['title', 'description', 'parent', 'active', 'color']; |
||
51 | |||
52 | /** |
||
53 | * Get collection of cards in the category. |
||
54 | */ |
||
55 | public function cards() |
||
59 | |||
60 | /** |
||
61 | * Get collection of children categories |
||
62 | */ |
||
63 | public function children() |
||
67 | |||
68 | /** |
||
69 | * Get the parent category |
||
70 | */ |
||
71 | public function parent() |
||
75 | |||
76 | /** |
||
77 | * Get number of cards in a category |
||
78 | * |
||
79 | * @return int |
||
80 | */ |
||
81 | public function countCards() |
||
85 | |||
86 | /** |
||
87 | * Get collection of validation rules for model attributes |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | protected function getRules(): array |
||
97 | |||
98 | /** |
||
99 | * Scope to filter by active categories |
||
100 | * |
||
101 | * @param Builder $query |
||
102 | * @return Builder |
||
103 | */ |
||
104 | public function scopeActive(Builder $query): Builder |
||
108 | |||
109 | /** |
||
110 | * Score to filter by LIKE search |
||
111 | * |
||
112 | * @param Builder $query |
||
113 | * @param string $keyword |
||
114 | * @return Builder |
||
115 | */ |
||
116 | View Code Duplication | public function scopeSearch(Builder $query, string $keyword): Builder |
|
123 | } |
||
124 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.