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 SecretSoftware |
||
19 | { |
||
20 | private $examinators = array( |
||
21 | 'CI', |
||
22 | 'CONTINUOUS_INTEGRATION', |
||
23 | 'BUILD_ID', |
||
24 | 'BUILD_NUMBER', |
||
25 | 'TEAMCITY_VERSION', |
||
26 | 'TRAVIS', |
||
27 | 'CIRCLECI', |
||
28 | 'JENKINS_URL', |
||
29 | 'HUDSON_URL', |
||
30 | 'bamboo.buildKey', |
||
31 | 'PHPCI', |
||
32 | 'GOCD_SERVER_HOST', |
||
33 | 'BUILDKITE', |
||
34 | ); |
||
35 | |||
36 | public function __construct(array $additionalEnvVariables = array()) |
||
40 | |||
41 | /** |
||
42 | * Where the magic occurs. |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | public function underScrutiny() |
||
56 | |||
57 | /** |
||
58 | * Failing test cases are not a problem anymore. |
||
59 | * |
||
60 | * @param PHPUnit_Framework_TestCase $test |
||
61 | */ |
||
62 | public function force(PHPUnit_Framework_TestCase $test) |
||
78 | |||
79 | /** |
||
80 | * @param ReflectionClass $reflection |
||
81 | * @param string $property |
||
82 | * @param mixed $value |
||
83 | * @param mixed $object |
||
84 | * |
||
85 | * @return self |
||
86 | */ |
||
87 | View Code Duplication | private function forcePropertyValue(ReflectionClass $reflection, $property, $value, $object) |
|
95 | |||
96 | /** |
||
97 | * @param ReflectionClass $reflection |
||
98 | * @param string $property |
||
99 | * @param mixed $object |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | View Code Duplication | private function getPropertyValue(ReflectionClass $reflection, $property, $object) |
|
111 | |||
112 | /** |
||
113 | * @param ReflectionClass $reflection |
||
114 | * @param string $property |
||
115 | * |
||
116 | * @return ReflectionProperty |
||
117 | */ |
||
118 | private function accessProperty(ReflectionClass $reflection, $property) |
||
125 | } |
||
126 |
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.