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 |
||
| 21 | class AppFileLocator extends FileLocator |
||
| 22 | { |
||
| 23 | protected $moduleManager; |
||
| 24 | protected $path; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor. |
||
| 28 | * |
||
| 29 | * @param ModuleManager $moduleManager A ModuleManager instance |
||
| 30 | * @param null|string $path The path the global resource directory |
||
| 31 | * @param array $paths An array of paths where to look for resources |
||
| 32 | */ |
||
| 33 | public function __construct(ModuleManager $moduleManager, $path = null, array $paths = array()) |
||
| 34 | { |
||
| 35 | $this->moduleManager = $moduleManager; |
||
| 36 | if (null !== $path) { |
||
| 37 | $this->path = $path; |
||
| 38 | $paths[] = $path; |
||
| 39 | } |
||
| 40 | |||
| 41 | parent::__construct($paths); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function locate($file, $currentPath = null, $first = true) |
||
| 48 | { |
||
| 49 | if ('@' === $file[0]) { |
||
| 50 | return $this->moduleManager->locateResource($file, $this->path, $first); |
||
| 51 | } |
||
| 52 | |||
| 53 | return parent::locate($file, $currentPath, $first); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns the path to the app directory. |
||
| 58 | * |
||
| 59 | * @return string The path to the app directory. |
||
| 60 | */ |
||
| 61 | public function getAppPath() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns an array of paths to modules. |
||
| 68 | * |
||
| 69 | * @return array An array of paths to each loaded module |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function getModulesPath() |
|
| 80 | } |
||
| 81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.