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 | 2 | */ |
|
| 24 | public function getTagDependencyCacheComponent() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Returns common tag name for model instance |
||
| 31 | * @return string tag name |
||
| 32 | 2 | */ |
|
| 33 | public static function commonTag() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Returns object tag name including it's id |
||
| 41 | * @param array Changed fields from Update Event |
||
| 42 | 2 | * @return string tag name |
|
| 43 | */ |
||
| 44 | public function objectTag($oldfields) |
||
| 61 | 1 | ||
| 62 | 1 | /** |
|
| 63 | * Returns composite tags name including fields |
||
| 64 | 1 | * @param array Changed fields from Update Event |
|
| 65 | 1 | * @return array tag names |
|
| 66 | */ |
||
| 67 | 1 | public function objectCompositeTag($oldfields) |
|
| 100 | |||
| 101 | /** |
||
| 102 | 1 | * Specific fields from model for build composite tags for invalidate |
|
| 103 | * Example: |
||
| 104 | * return [ |
||
| 105 | * ['field1', 'field2'], |
||
| 106 | * ['field1', 'field2', 'field3'], |
||
| 107 | * ]; |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | protected function cacheCompositeTagFields() |
||
| 114 | 1 | ||
| 115 | 1 | /** |
|
| 116 | 1 | * Finds or creates new model using or not using cache(objectTag is applied) |
|
| 117 | 1 | * @param string|int $id ID of model to find |
|
| 118 | * @param bool $createIfEmptyId Create new model instance(record) if id is empty |
||
| 119 | 1 | * @param bool $useCache Use cache |
|
| 120 | * @param int $cacheLifetime Cache lifetime in seconds |
||
| 121 | * @param bool|\Exception $throwException False or exception instance to throw if model not found or (empty id AND createIfEmptyId==false) |
||
| 122 | 1 | * @param bool $useIdentityMap True if we want to use identity map |
|
| 123 | * @return \yii\db\ActiveRecord|null|self|TagDependencyTrait |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | public static function loadModel( |
||
| 183 | 2 | ||
| 184 | /** |
||
| 185 | * Invalidate model tags. |
||
| 186 | * @param yii\db\AfterSaveEvent when called as an event handler. |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function invalidateTags($event = null) |
||
| 209 | |||
| 210 | } |
||
| 211 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.