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:
Complex classes like OverloadClass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OverloadClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class OverloadClass |
||
| 8 | { |
||
| 9 | const EXTRA_OVERLOAD_CACHE_DIR = 'composer-overload-cache-dir'; |
||
| 10 | const EXTRA_OVERLOAD_CLASS = 'composer-overload-class'; |
||
| 11 | const NAMESPACE_PREFIX = 'ComposerOverloadClass'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param Event $event |
||
| 15 | */ |
||
| 16 | public static function overload(Event $event) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string $cacheDir |
||
| 40 | * @param string $fullyQualifiedClassName |
||
| 41 | * @param string $filePath |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | protected static function generateProxy($cacheDir, $fullyQualifiedClassName, $filePath) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $filePath |
||
| 62 | * @param string $fullyQualifiedClassName |
||
| 63 | * @return string |
||
| 64 | * @throws \Exception |
||
| 65 | */ |
||
| 66 | protected static function getPhpForDuplicatedFile($filePath, $fullyQualifiedClassName) |
||
| 67 | { |
||
| 68 | if (is_readable($filePath) === false) { |
||
| 69 | throw new \Exception('File "' . $filePath . '" does not exists, or is not readable.'); |
||
| 70 | } |
||
| 71 | |||
| 72 | $phpLines = file($filePath); |
||
| 73 | $namespace = substr($fullyQualifiedClassName, 0, strrpos($fullyQualifiedClassName, '\\')); |
||
| 74 | $nextIsNamespace = false; |
||
| 75 | $namespaceFound = null; |
||
| 76 | $classesFound = []; |
||
| 77 | $phpCodeForNamespace = null; |
||
| 78 | $namespaceLine = null; |
||
| 79 | $uses = []; |
||
| 80 | $addUses = []; |
||
| 81 | $isGlobalUse = true; |
||
| 82 | $lastUseLine = null; |
||
| 83 | $tokens = token_get_all(implode(null, $phpLines)); |
||
| 84 | foreach ($tokens as $index => $token) { |
||
| 85 | if (is_array($token)) { |
||
| 86 | if ($token[0] === T_NAMESPACE) { |
||
| 87 | $nextIsNamespace = true; |
||
| 88 | $namespaceLine = $token[2]; |
||
| 89 | View Code Duplication | } elseif ($isGlobalUse && $token[0] === T_CLASS) { |
|
|
1 ignored issue
–
show
|
|||
| 90 | $classesFound[] = static::getClassNameFromTokens($tokens, $index + 1); |
||
| 91 | $isGlobalUse = false; |
||
| 92 | } elseif ($token[0] === T_EXTENDS) { |
||
| 93 | static::addUse(static::getClassNameFromTokens($tokens, $index + 1), $namespaceFound, $uses, $addUses); |
||
| 94 | View Code Duplication | } elseif ($isGlobalUse && $token[0] === T_USE) { |
|
|
1 ignored issue
–
show
|
|||
| 95 | $uses[] = static::getClassNameFromTokens($tokens, $index + 1); |
||
| 96 | $lastUseLine = $token[2]; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($nextIsNamespace) { |
||
| 100 | $phpCodeForNamespace .= $token[1]; |
||
| 101 | if ($token[0] === T_NS_SEPARATOR || $token[0] === T_STRING) { |
||
| 102 | $namespaceFound .= $token[1]; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } elseif ($nextIsNamespace && $token === ';') { |
||
| 106 | $phpCodeForNamespace .= $token; |
||
| 107 | if ($namespaceFound !== $namespace) { |
||
| 108 | $message = 'Expected namespace "' . $namespace . '", found "' . $namespaceFound . '" '; |
||
| 109 | $message .= 'in "' . $filePath . '".'; |
||
| 110 | throw new \Exception($message); |
||
| 111 | } |
||
| 112 | $nextIsNamespace = false; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | static::assertOnlyRightClassFound($classesFound, $fullyQualifiedClassName, $filePath); |
||
| 117 | static::replaceNamespace($namespaceFound, $phpCodeForNamespace, $phpLines, $namespaceLine); |
||
| 118 | static::addUsesInPhpLines($addUses, $phpLines, ($lastUseLine === null ? $namespaceLine : $lastUseLine)); |
||
| 119 | |||
| 120 | return implode(null, $phpLines); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $classFound |
||
| 125 | * @param string $fullyQualifiedClassName |
||
| 126 | * @param string $filePath |
||
| 127 | * @throws \Exception |
||
| 128 | */ |
||
| 129 | protected static function assertOnlyRightClassFound(array $classFound, $fullyQualifiedClassName, $filePath) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $namespace |
||
| 143 | * @param string $phpCodeForNamespace |
||
| 144 | * @param array $phpLines |
||
| 145 | * @param int $namespaceLine |
||
| 146 | */ |
||
| 147 | protected static function replaceNamespace($namespace, $phpCodeForNamespace, &$phpLines, $namespaceLine) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param array $tokens |
||
| 158 | * @param int $index |
||
| 159 | * @return string |
||
| 160 | * @throws \Exception |
||
| 161 | */ |
||
| 162 | protected static function getClassNameFromTokens(array &$tokens, $index) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $className |
||
| 195 | * @param string $namespace |
||
| 196 | * @param array $uses |
||
| 197 | * @param array $addUses |
||
| 198 | */ |
||
| 199 | protected static function addUse($className, $namespace, array $uses, array &$addUses) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param array $addUses |
||
| 217 | * @param array $phpLines |
||
| 218 | * @param int $line |
||
| 219 | */ |
||
| 220 | protected static function addUsesInPhpLines(array $addUses, array &$phpLines, $line) |
||
| 231 | } |
||
| 232 |
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.