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 | class MemoryFileDictLoader implements DictLoaderInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Words segment name. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $segmentName = 'words_%s'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Segment files. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $segments = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Surname cache. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $surnames = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor. |
||
| 41 | * |
||
| 42 | * @param string $path |
||
| 43 | */ |
||
| 44 | 9 | public function __construct($path) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Load dict. |
||
| 59 | * |
||
| 60 | * @param Closure $callback |
||
| 61 | */ |
||
| 62 | 9 | public function map(Closure $callback) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Load surname dict. |
||
| 71 | * |
||
| 72 | * @param Closure $callback |
||
| 73 | */ |
||
| 74 | 1 | public function mapSurname(Closure $callback) |
|
| 86 | } |
||
| 87 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: