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 |
||
| 15 | trait TDataGridAggregationFunction |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var IAggregationFunction[] |
||
| 20 | */ |
||
| 21 | private $aggregationFunctions = []; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $key |
||
| 26 | * @param IAggregationFunction $aggregationFunction |
||
| 27 | * @return static |
||
| 28 | */ |
||
| 29 | public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * @param IDataSource $dataSource |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function beforeDataModelFilter(IDataSource $dataSource) |
|
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @param IDataSource $dataSource |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function afterDataModelFilter(IDataSource $dataSource) |
|
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * @param IDataSource $dataSource |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function afterDataModelPaginated(IDataSource $dataSource) |
|
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function hasSomeAggregationFunction() |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * @return IAggregationFunction[] |
||
| 100 | */ |
||
| 101 | public function getAggregationFunctions() |
||
| 105 | |||
| 106 | } |
||
| 107 |
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: