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 |
||
| 22 | class ConfigFile extends \hidev\base\Component implements \yii\base\Arrayable, \ArrayAccess, \IteratorAggregate |
||
| 23 | { |
||
| 24 | use \hiqdev\yii2\collection\ObjectTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string specifies handler to be used |
||
| 28 | */ |
||
| 29 | public $fileType; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool Don't touch file if exists |
||
| 33 | */ |
||
| 34 | public $once; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string Username to change file owner to |
||
| 38 | */ |
||
| 39 | public $chown; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string Group to change file group to |
||
| 43 | */ |
||
| 44 | public $chgrp; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|integer Permissions to change to |
||
| 48 | */ |
||
| 49 | public $chmod; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array|File the file to be handled |
||
| 53 | */ |
||
| 54 | protected $_file; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string path to copy from |
||
| 58 | */ |
||
| 59 | protected $_copy; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string the path to the file |
||
| 63 | */ |
||
| 64 | protected $_path; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string the template name |
||
| 68 | */ |
||
| 69 | protected $_template; |
||
| 70 | |||
| 71 | public function init() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Template setter. |
||
| 78 | * @param string $template name |
||
| 79 | */ |
||
| 80 | public function setTemplate($template) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Template getter. |
||
| 87 | */ |
||
| 88 | public function getTemplate() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the file object. |
||
| 95 | * Instantiates it if necessary. |
||
| 96 | * @return File |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function getFile() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Sets file with given info. |
||
| 117 | * @param mixed $info could be anything that is good for File::create |
||
| 118 | */ |
||
| 119 | public function setFile($info) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the path to the file, but file info has precendence. |
||
| 126 | * @param string $value |
||
| 127 | */ |
||
| 128 | public function setPath($value) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Copy setter. Turns this file type to `copy`. |
||
| 135 | */ |
||
| 136 | public function setCopy($value) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Copy getter. Processes aliases. |
||
| 144 | */ |
||
| 145 | public function getCopy() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Dirname getter. |
||
| 152 | */ |
||
| 153 | public function getDirname() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Path getter. |
||
| 160 | */ |
||
| 161 | public function getPath() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Checks if the file exists. |
||
| 168 | */ |
||
| 169 | public function exists() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Read the file. |
||
| 176 | */ |
||
| 177 | public function read() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Read the file into array. |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function readArray() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function actionPerform($name = null, $path = null) |
||
| 200 | |||
| 201 | View Code Duplication | public function load() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * DEPRECATED in favour of save(). |
||
| 213 | */ |
||
| 214 | public function actionSave() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Save the file. |
||
| 221 | */ |
||
| 222 | View Code Duplication | public function save() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Modify action. |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function actionModify() |
|
| 243 | } |
||
| 244 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.