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 |
||
| 25 | class SitesCacheFileBuilder { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var SiteLookup |
||
| 29 | */ |
||
| 30 | private $siteLookup; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $cacheFile; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param SiteLookup $siteLookup |
||
| 39 | * @param string $cacheFile |
||
| 40 | */ |
||
| 41 | public function __construct( SiteLookup $siteLookup, $cacheFile ) { |
||
| 45 | |||
| 46 | public function build() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param Site[] $sites |
||
| 53 | * |
||
| 54 | * @throws MWException if in manualRecache mode |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | private function cacheSites( array $sites ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param Site $site |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | private function getSiteAsArray( Site $site ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Site $site |
||
| 95 | * |
||
| 96 | * @return array Site local identifiers |
||
| 97 | */ |
||
| 98 | private function buildLocalIdentifiers( Site $site ) { |
||
| 112 | |||
| 113 | } |
||
| 114 |
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: