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 |
||
| 8 | class MaintenanceComponent extends Component |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Controller object |
||
| 12 | * |
||
| 13 | * @var \Cake\Controller\Controller |
||
| 14 | */ |
||
| 15 | protected $_controller; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The allowed routes. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $_allowedRoutes = [ |
||
| 23 | 'Pages.maintenance', |
||
| 24 | 'Users.login', |
||
| 25 | 'Users.logout' |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Initialize properties. |
||
| 30 | * |
||
| 31 | * @param array $config The config data. |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function initialize(array $config) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Handle the maintenance mode. |
||
| 42 | * |
||
| 43 | * @return false|void |
||
| 44 | */ |
||
| 45 | public function handle() |
||
| 91 | } |
||
| 92 |
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: