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 |
||
| 7 | abstract class AbstractDatasource implements DatasourceInterface |
||
| 8 | { |
||
| 9 | protected $regions; |
||
| 10 | protected $departements; |
||
| 11 | protected $index; |
||
| 12 | |||
| 13 | public function __construct() { |
||
| 23 | |||
| 24 | protected function slugify($value) { |
||
| 31 | |||
| 32 | protected function addToIndex($type, $value, $key) { |
||
| 35 | |||
| 36 | public function findAllDepartements($sortByValue = false) { |
||
| 43 | |||
| 44 | public function findAllRegions($sortByValue = false) { |
||
| 51 | |||
| 52 | public function findAllCommunes() { |
||
| 55 | |||
| 56 | public function findCommunesByZipcode($zipcode) { |
||
| 59 | |||
| 60 | public function findDepartementByCode($departementCode) { |
||
| 63 | |||
| 64 | View Code Duplication | public function findDepartementByName($departementName) { |
|
| 75 | |||
| 76 | View Code Duplication | public function findRegionByName($regionName) { |
|
| 87 | |||
| 88 | public function findRegionByCode($regionCode) { |
||
| 91 | |||
| 92 | protected function sortByValue($collection) { |
||
| 100 | } |
||
| 101 | |||
| 102 |
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: