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 |
||
| 18 | class FileList implements \Iterator, \Countable |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * A list of file paths that are included in the list. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | private $files = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The number of files in the list. |
||
| 30 | * |
||
| 31 | * @var integer |
||
| 32 | */ |
||
| 33 | private $numFiles = 0; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The config data for the run. |
||
| 37 | * |
||
| 38 | * @var \PHP_CodeSniffer\Config |
||
| 39 | */ |
||
| 40 | public $config = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The ruleset used for the run. |
||
| 44 | * |
||
| 45 | * @var \PHP_CodeSniffer\Ruleset |
||
| 46 | */ |
||
| 47 | public $ruleset = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * An array of patterns to use for skipping files. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $ignorePatterns = array(); |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructs a file list and loads in an array of file paths to process. |
||
| 59 | * |
||
| 60 | * @param \PHP_CodeSniffer\Config $config The config data for the run. |
||
| 61 | * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
||
| 62 | * |
||
| 63 | * @return void |
||
|
|
|||
| 64 | */ |
||
| 65 | public function __construct(Config $config, Ruleset $ruleset) |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Add a file to the list. |
||
| 83 | * |
||
| 84 | * If a file object has already been created, it can be passed here. |
||
| 85 | * If it is left NULL, it will be created when accessed. |
||
| 86 | * |
||
| 87 | * @param string $path The path to the file being added. |
||
| 88 | * @param \PHP_CodeSniffer\Files\File $file The file being added. |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function addFile($path, $file=null) |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the class name of the filter being used for the run. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | private function getFilterClass() |
||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * Rewind the iterator to the first file. |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | function rewind() |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the file that is currently being processed. |
||
| 161 | * |
||
| 162 | * @return \PHP_CodeSniffer\Files\File |
||
| 163 | */ |
||
| 164 | function current() |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the file path of the current file being processed. |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | function key() |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Move forward to the next file. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | function next() |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Checks if current position is valid. |
||
| 202 | * |
||
| 203 | * @return boolean |
||
| 204 | */ |
||
| 205 | function valid() |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Return the number of files in the list. |
||
| 218 | * |
||
| 219 | * @return integer |
||
| 220 | */ |
||
| 221 | function count() |
||
| 226 | |||
| 227 | |||
| 228 | }//end class |
||
| 229 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.