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 MappingPass implements CompilerPassInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | * @throws \Exception |
||
| 30 | */ |
||
| 31 | public function process(ContainerBuilder $container) |
||
| 55 | |||
| 56 | private function getNamespaces($directory) { |
||
| 78 | |||
| 79 | // |
||
| 80 | // private function getFullNamespace($filename) { |
||
| 81 | // $lines = file($filename); |
||
| 82 | // $namespaceLine = array_shift(preg_grep('/^namespace /', $lines)); |
||
| 83 | // $match = array(); |
||
| 84 | // preg_match('/^namespace (.*);$/', $namespaceLine, $match); |
||
| 85 | // $fullNamespace = array_pop($match); |
||
| 86 | // |
||
| 87 | // return $fullNamespace; |
||
| 88 | // } |
||
| 89 | // |
||
| 90 | // private function getClassname($filename) { |
||
| 91 | // $directoriesAndFilename = explode('/', $filename); |
||
| 92 | // $filename = array_pop($directoriesAndFilename); |
||
| 93 | // $nameAndExtension = explode('.', $filename); |
||
| 94 | // $className = array_shift($nameAndExtension); |
||
| 95 | // |
||
| 96 | // return $className; |
||
| 97 | // } |
||
| 98 | } |
||
| 99 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.