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 |
||
| 9 | class NamingHelper |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Get object tag name. |
||
| 13 | * @param string|ActiveRecord $class |
||
| 14 | * @param mixed $id |
||
| 15 | * @return string |
||
| 16 | * @throws \yii\base\InvalidParamException |
||
| 17 | */ |
||
| 18 | 2 | View Code Duplication | public static function getObjectTag($class, $id) |
| 19 | { |
||
| 20 | 2 | if (is_object($class) && $class instanceof ActiveRecord) { |
|
| 21 | 1 | $class = $class->className(); |
|
| 22 | 2 | } elseif (!is_string($class)) { |
|
| 23 | 1 | throw new InvalidParamException('Param $class must be a string or an object.'); |
|
| 24 | } |
||
| 25 | 2 | return $class . '[ObjectTag:' . self::getCacheKeyById($id) . ']'; |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get common tag name. |
||
| 30 | * @param string|ActiveRecord $class |
||
| 31 | * @return string |
||
| 32 | * @throws \yii\base\InvalidParamException |
||
| 33 | */ |
||
| 34 | 2 | View Code Duplication | public static function getCommonTag($class) |
| 35 | { |
||
| 36 | 2 | if (is_object($class) && $class instanceof ActiveRecord) { |
|
| 37 | 1 | $class = $class->className(); |
|
| 38 | 2 | } elseif (!is_string($class)) { |
|
| 39 | 1 | throw new InvalidParamException('Param $class must be a string or an object.'); |
|
| 40 | } |
||
| 41 | 2 | return $class . '[CommonTag]'; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get composite tag name from model fields. |
||
| 46 | * @param string|ActiveRecord $class |
||
| 47 | * @param array $fields - ['field_model' => 'value'] |
||
| 48 | * @return string |
||
| 49 | * @throws \yii\base\InvalidParamException |
||
| 50 | */ |
||
| 51 | 1 | public static function getCompositeTag($class, $fields) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Return string for cache-key from varios primary key |
||
| 69 | * @param mixed $id |
||
| 70 | * @param bool $keys if true, will used keys array for build the cache key |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 2 | public static function getCacheKeyById($id, $keys = false) |
|
| 84 | |||
| 85 | |||
| 86 | } |
||
| 87 |