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 |
||
| 13 | class Magento2Initializer |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var \Composer\Autoload\ClassLoader |
||
| 17 | */ |
||
| 18 | private $autoloader; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Magento2Initializer constructor. |
||
| 22 | * @param \Composer\Autoload\ClassLoader $autoloader |
||
| 23 | */ |
||
| 24 | public function __construct(ClassLoader $autoloader) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $magentoRootFolder |
||
| 31 | * @return \N98\Magento\Framework\App\Magerun |
||
| 32 | * @throws \Exception |
||
| 33 | */ |
||
| 34 | public function init($magentoRootFolder) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * use require-once inside a function with it's own variable scope w/o any other variables |
||
| 71 | * and $this unbound. |
||
| 72 | * |
||
| 73 | * @param string $path |
||
| 74 | */ |
||
| 75 | View Code Duplication | private function requireOnce($path) |
|
| 86 | } |
||
| 87 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: