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 |
||
| 18 | final class FlexibleExtensionFilesystemLoader implements \Twig_LoaderInterface, EventSubscriberInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var FilesystemLoader |
||
| 22 | */ |
||
| 23 | private $filesystemLoader; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $cachedCacheKey = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $cachedCacheKeyExtension = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $cachedCacheKeyException = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @param string $sourceDir |
||
| 44 | * @param string[] $sourcePaths |
||
| 45 | * @param string[] $paths |
||
| 46 | * @param string[] $extensions |
||
| 47 | */ |
||
| 48 | public function __construct($sourceDir, array $sourcePaths, array $paths, array $extensions) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function getSource($name) |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function getCacheKey($name) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function isFresh($name, $time) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public static function getSubscribedEvents() : array |
||
| 132 | |||
| 133 | public function beforeRun(SourceSetEvent $sourceSetEvent) |
||
| 141 | } |
||
| 142 |
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: