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 |
||
| 17 | class FActiveImage extends FActiveRecord implements ImageInterface |
||
| 18 | {
|
||
| 19 | /** |
||
| 20 | * Изображение, отображаемое при отсутствии файла |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $defaultImage = '/i/sp.gif'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Массив доступных префиксов для файла |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $availableTypes = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Путь к папке с изображениями относительно корня проекта без начального слэша |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $imageDir = 'f/'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * По умолчанию отдаётся оригинальный файл |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 2 | View Code Duplication | public function __toString() |
| 56 | |||
| 57 | /** |
||
| 58 | * Получение неоригинального изображения |
||
| 59 | * Допустимые типы находятся в $this->availableTypes |
||
| 60 | * Если файл не совпадает с доступными типами, вызывается магия Yii |
||
| 61 | * Если файл не существует, отдается defaultImage |
||
| 62 | * |
||
| 63 | * @param string $name |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | * @throws CException |
||
| 67 | */ |
||
| 68 | 3 | View Code Duplication | public function __get($name) |
| 84 | |||
| 85 | /** |
||
| 86 | * Создание пути для файла |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 2 | public function getPath() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param $imageDir |
||
| 97 | */ |
||
| 98 | 1 | public function setImageDir($imageDir) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Создание абсолютного пути до файла |
||
| 105 | * |
||
| 106 | * @param null $name |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getAbsolutePath($name = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Создание полного пути до файла |
||
| 117 | * |
||
| 118 | * @param string $name |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 2 | View Code Duplication | protected function getFullPath($name = null) |
| 133 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.