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 |
||
16 | class DomainService extends Model |
||
17 | { |
||
18 | const SERVICE_OPERATION_PURCHASE = 'purchase'; |
||
19 | const SERVICE_OPERATION_RENEW = 'renew'; |
||
20 | |||
21 | /** |
||
22 | * @var string Human-readable name |
||
23 | */ |
||
24 | public $name; |
||
25 | |||
26 | /** |
||
27 | * @var string Service type |
||
28 | */ |
||
29 | public $type; |
||
30 | |||
31 | /** @var DomainResource[] */ |
||
32 | public $resources; |
||
33 | |||
34 | /** |
||
35 | * Returns resource for $operation. |
||
36 | * |
||
37 | * @param string $operation |
||
38 | * @return DomainResource |
||
39 | */ |
||
40 | public function getResource($operation) |
||
44 | |||
45 | /** |
||
46 | * Tries to assign a resource in this service, if the type is correct. |
||
47 | * |
||
48 | * @param DomainResource $resource |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function tryResourceAssignation(DomainResource $resource) |
||
61 | |||
62 | /** |
||
63 | * Check whether $resource belongs to this service type. |
||
64 | * |
||
65 | * @param DomainResource $resource |
||
66 | * @return false|string operation type or false, if resource does not match this service |
||
67 | */ |
||
68 | protected function matchType(DomainResource $resource) |
||
78 | |||
79 | /** |
||
80 | * @return array available operations |
||
81 | */ |
||
82 | View Code Duplication | public static function getOperations() |
|
89 | |||
90 | /** |
||
91 | * @return bool whether service contains all necessary resources |
||
92 | */ |
||
93 | public function isFulfilled() |
||
97 | } |
||
98 |
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.