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 PageDeleter extends Service { |
||
17 | |||
18 | /** |
||
19 | * @since 0.2 |
||
20 | * |
||
21 | * @param Page $page |
||
22 | * @param array $extraParams |
||
23 | * |
||
24 | * @return bool |
||
25 | */ |
||
26 | View Code Duplication | public function delete( Page $page, array $extraParams = [] ) { |
|
|
|||
27 | $this->api->postRequest( new SimpleRequest( |
||
28 | 'delete', |
||
29 | $this->getDeleteParams( $page->getPageIdentifier(), $extraParams ) |
||
30 | ) ); |
||
31 | return true; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @since 0.2 |
||
36 | * |
||
37 | * @param Revision $revision |
||
38 | * @param array $extraParams |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | View Code Duplication | public function deleteFromRevision( Revision $revision, array $extraParams = [] ) { |
|
43 | $this->api->postRequest( new SimpleRequest( |
||
44 | 'delete', |
||
45 | $this->getDeleteParams( $revision->getPageIdentifier(), $extraParams ) |
||
46 | ) ); |
||
47 | return true; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @since 0.2 |
||
52 | * |
||
53 | * @param int $pageid |
||
54 | * @param array $extraParams |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function deleteFromPageId( $pageid, array $extraParams = [] ) { |
||
65 | |||
66 | /** |
||
67 | * @since 0.5 |
||
68 | * |
||
69 | * @param Title|string $title |
||
70 | * @param array $extraParams |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function deleteFromPageTitle( $title, array $extraParams = [] ) { |
||
84 | |||
85 | /** |
||
86 | * @param PageIdentifier $identifier |
||
87 | * @param array $extraParams |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | private function getDeleteParams( PageIdentifier $identifier, $extraParams ) { |
||
104 | |||
105 | } |
||
106 |
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.