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 |
||
9 | class AbstractCouchbaseBucketFactory implements AbstractFactoryInterface |
||
10 | { |
||
11 | /** |
||
12 | * Determine if we can create a service with name |
||
13 | * |
||
14 | * @param ServiceLocatorInterface $serviceLocator |
||
15 | * @param $name |
||
16 | * @param $requestedName |
||
17 | * @return bool |
||
18 | */ |
||
19 | 1 | public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
|
23 | |||
24 | /** |
||
25 | * Create service with name |
||
26 | * |
||
27 | * @param ServiceLocatorInterface $serviceLocator |
||
28 | * @param string $name |
||
29 | * @param string $requestedName |
||
30 | * @return \CouchbaseBucket |
||
31 | * @throws \RuntimeException |
||
32 | * @throws \Assert\AssertionFailedException |
||
33 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
34 | */ |
||
35 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
50 | |||
51 | /** |
||
52 | * @param \CouchbaseCluster $cluster |
||
53 | * @param BucketOptions $options |
||
54 | * @return \CouchbaseBucket |
||
55 | */ |
||
56 | public function createCouchbaseBucket(\CouchbaseCluster $cluster, BucketOptions $options) |
||
60 | |||
61 | /** |
||
62 | * @param ClusterOptions $options |
||
63 | * @return \CouchbaseCluster |
||
64 | * @throws \RuntimeException |
||
65 | */ |
||
66 | public function createCouchbaseCluster(ClusterOptions $options) |
||
79 | |||
80 | /** |
||
81 | * @param ServiceLocatorInterface $serviceLocator |
||
82 | * @return array |
||
83 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
84 | * @throws \Assert\AssertionFailedException |
||
85 | */ |
||
86 | 3 | public function getModuleConfig(ServiceLocatorInterface $serviceLocator) |
|
97 | |||
98 | /** |
||
99 | * @param array $moduleConfig |
||
100 | * @param string $bucketConfigName |
||
101 | * @return BucketOptions |
||
102 | * @throws \Assert\AssertionFailedException |
||
103 | */ |
||
104 | 1 | View Code Duplication | public function getBucketOptions(array $moduleConfig, $bucketConfigName) |
117 | |||
118 | /** |
||
119 | * @param array $moduleConfig |
||
120 | * @param string $clusterName |
||
121 | * @return ClusterOptions |
||
122 | * @throws \Assert\AssertionFailedException |
||
123 | */ |
||
124 | 1 | View Code Duplication | public function getClusterOptions(array $moduleConfig, $clusterName) |
137 | } |
||
138 |
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.