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 |
||
16 | trait TagDependencyTrait |
||
17 | { |
||
18 | /** @var array IdentityMap pattern support */ |
||
19 | public static $identityMap = []; |
||
20 | |||
21 | /** |
||
22 | * @return \yii\caching\Cache |
||
23 | */ |
||
24 | 2 | public function getTagDependencyCacheComponent() |
|
28 | |||
29 | /** |
||
30 | * Returns common tag name for model instance |
||
31 | * @return string tag name |
||
32 | */ |
||
33 | 2 | public static function commonTag() |
|
38 | |||
39 | /** |
||
40 | * Returns object tag name including it's id |
||
41 | * @param array $oldFields Changed fields from Update Event |
||
42 | * @return string tag name |
||
43 | */ |
||
44 | 2 | public function objectTag($oldFields = []) |
|
45 | { |
||
46 | /** @var \yii\db\ActiveRecord $this */ |
||
47 | 2 | $primaryKey = null; |
|
|
|||
48 | 2 | if (count($this->primaryKey()) === 1) |
|
49 | 2 | { |
|
50 | 2 | $key = $this->primaryKey()[0]; |
|
51 | 2 | $primaryKey = isset($oldFields[$key]) ? $oldFields[$key] : $this->$key; |
|
52 | 2 | } else { |
|
53 | $primaryKey = []; |
||
54 | foreach ($this->primaryKey() as $key) |
||
55 | { |
||
56 | $primaryKey[$key] = isset($oldFields[$key]) ? $oldFields[$key] : $this->$key; |
||
57 | } |
||
58 | } |
||
59 | 2 | return NamingHelper::getObjectTag($this->className(), $primaryKey); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns composite tags name including fields |
||
64 | * @param array $oldFields Changed fields from Update Event |
||
65 | * @return array tag names |
||
66 | */ |
||
67 | 1 | public function objectCompositeTag($oldFields = []) |
|
68 | { |
||
69 | /** @var \yii\db\ActiveRecord|TagDependencyTrait $this */ |
||
70 | 1 | $cacheFields = $this->cacheCompositeTagFields(); |
|
71 | |||
72 | 1 | if(empty($cacheFields)) { |
|
73 | 1 | return []; |
|
74 | } |
||
75 | |||
76 | 1 | $cacheFields = (is_array($cacheFields) && !empty($cacheFields) && is_array($cacheFields[0])) ? $cacheFields : [$cacheFields]; |
|
77 | 1 | $tags = []; |
|
78 | |||
79 | 1 | foreach ($cacheFields as $tagFields) { |
|
80 | 1 | $tag = []; |
|
81 | 1 | $changed = false; |
|
82 | |||
83 | 1 | View Code Duplication | foreach ($tagFields as $tagField) { |
84 | 1 | $tag[$tagField] = $this->$tagField; |
|
85 | 1 | $changed |= isset($oldFields[$tagField]); |
|
86 | 1 | } |
|
87 | |||
88 | 1 | $tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
|
89 | |||
90 | 1 | if ($changed) { |
|
91 | $tag = []; |
||
92 | View Code Duplication | foreach ($tagFields as $tagField) { |
|
93 | $tag[$tagField] = isset($oldFields[$tagField]) ? $oldFields[$tagField] : $this->$tagField; |
||
94 | } |
||
95 | $tags[] = NamingHelper::getCompositeTag($this->className(), $tag); |
||
96 | } |
||
97 | 1 | } |
|
98 | |||
99 | 1 | return $tags; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Specific fields from model for build composite tags for invalidate |
||
104 | * Example: |
||
105 | * return [ |
||
106 | * ['field1', 'field2'], |
||
107 | * ['field1', 'field2', 'field3'], |
||
108 | * ]; |
||
109 | * @return array |
||
110 | */ |
||
111 | 2 | protected function cacheCompositeTagFields() |
|
115 | |||
116 | /** |
||
117 | * Finds or creates new model using or not using cache(objectTag is applied) |
||
118 | * @param string|int $id ID of model to find |
||
119 | * @param bool $createIfEmptyId Create new model instance(record) if id is empty |
||
120 | * @param bool $useCache Use cache |
||
121 | * @param int $cacheLifetime Cache lifetime in seconds |
||
122 | * @param bool|\Exception $throwException False or exception instance to throw if model not found or (empty id AND createIfEmptyId==false) |
||
123 | * @param bool $useIdentityMap True if we want to use identity map |
||
124 | * @return \yii\db\ActiveRecord|null|self|TagDependencyTrait |
||
125 | * @throws \Exception |
||
126 | */ |
||
127 | 1 | public static function loadModel( |
|
184 | |||
185 | /** |
||
186 | * Invalidate model tags. |
||
187 | * @param yii\db\AfterSaveEvent|null $event when called as an event handler. |
||
188 | * @return bool |
||
189 | */ |
||
190 | 2 | public function invalidateTags($event = null) |
|
211 | |||
212 | } |
||
213 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.