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 | class Initialiser |
||
| 19 | { |
||
| 20 | const PATH_APP_MAGE_PHP = 'app/Mage.php'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string path to Magento root directory |
||
| 24 | */ |
||
| 25 | private $magentoPath; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Bootstrap Magento application |
||
| 29 | */ |
||
| 30 | public static function bootstrap($magentoPath) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Initialiser constructor. |
||
| 38 | * |
||
| 39 | * @param string $magentoPath |
||
| 40 | */ |
||
| 41 | public function __construct($magentoPath) |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Require app/Mage.php if class Mage does not yet exists. Preserves auto-loaders |
||
| 49 | * |
||
| 50 | * @see \Mage (final class) |
||
| 51 | */ |
||
| 52 | public function requireMage() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 69 | * and $this unbound. |
||
| 70 | * |
||
| 71 | * @param string $path |
||
| 72 | */ |
||
| 73 | private function requireOnce($path) |
||
| 77 | } |
||
| 78 | |||
| 86 |
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.