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 |
||
20 | class PageGetter extends Service { |
||
21 | |||
22 | /** |
||
23 | * @since 0.2 |
||
24 | * |
||
25 | * @param int $id |
||
26 | * @param array $extraParams |
||
27 | * |
||
28 | * @return Page |
||
29 | */ |
||
30 | View Code Duplication | public function getFromRevisionId( $id, array $extraParams = [] ) { |
|
|
|||
31 | $result = |
||
32 | $this->api->getRequest( |
||
33 | new SimpleRequest( |
||
34 | 'query', |
||
35 | $this->getQuery( [ 'revids' => $id ], $extraParams ) |
||
36 | ) |
||
37 | ); |
||
38 | |||
39 | return $this->newPageFromResult( array_shift( $result['query']['pages'] ) ); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @since 0.2 |
||
44 | * |
||
45 | * @param string|Title $title |
||
46 | * @param array $extraParams |
||
47 | * |
||
48 | * @return Page |
||
49 | */ |
||
50 | 7 | public function getFromTitle( $title, array $extraParams = [] ) { |
|
51 | 7 | if ( $title instanceof Title ) { |
|
52 | 7 | $title = $title->getTitle(); |
|
53 | 7 | } |
|
54 | $result = |
||
55 | 7 | $this->api->getRequest( |
|
56 | 7 | new SimpleRequest( |
|
57 | 7 | 'query', |
|
58 | 7 | $this->getQuery( [ 'titles' => $title ], $extraParams ) |
|
59 | 7 | ) |
|
60 | 7 | ); |
|
61 | |||
62 | 7 | return $this->newPageFromResult( array_shift( $result['query']['pages'] ) ); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @since 0.2 |
||
67 | * |
||
68 | * @param int $id |
||
69 | * @param array $extraParams |
||
70 | * |
||
71 | * @return Page |
||
72 | */ |
||
73 | 1 | View Code Duplication | public function getFromPageId( $id, array $extraParams = [] ) { |
74 | $result = |
||
75 | 1 | $this->api->getRequest( |
|
76 | 1 | new SimpleRequest( |
|
77 | 1 | 'query', |
|
78 | 1 | $this->getQuery( [ 'pageids' => $id ], $extraParams ) |
|
79 | 1 | ) |
|
80 | 1 | ); |
|
81 | |||
82 | 1 | return $this->newPageFromResult( array_shift( $result['query']['pages'] ) ); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @since 0.4 |
||
87 | * |
||
88 | * @param PageIdentifier $pageIdentifier |
||
89 | * @param array $extraParams |
||
90 | * |
||
91 | * @throws RuntimeException |
||
92 | * @return Page |
||
93 | */ |
||
94 | 5 | public function getFromPageIdentifier( |
|
95 | PageIdentifier $pageIdentifier, |
||
96 | array $extraParams = [] |
||
97 | ) { |
||
98 | 5 | if ( !$pageIdentifier->identifiesPage() ) { |
|
99 | throw new RuntimeException( '$pageIdentifier does not identify a page' ); |
||
100 | } |
||
101 | 5 | if ( $pageIdentifier->getId() !== null ) { |
|
102 | return $this->getFromPageId( $pageIdentifier->getId(), $extraParams ); |
||
103 | } else { |
||
104 | 5 | return $this->getFromTitle( $pageIdentifier->getTitle(), $extraParams ); |
|
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @since 0.2 |
||
110 | * |
||
111 | * @param Page $page |
||
112 | * @param array $extraParams |
||
113 | * |
||
114 | * @return Page |
||
115 | */ |
||
116 | public function getFromPage( Page $page, array $extraParams = [] ) { |
||
132 | |||
133 | /** |
||
134 | * @since 0.2 |
||
135 | * |
||
136 | * @param Revision $revision |
||
137 | * @param array $extraParams |
||
138 | * |
||
139 | * @return Page |
||
140 | */ |
||
141 | public function getFromRevision( Revision $revision, array $extraParams = [] ) { |
||
163 | |||
164 | /** |
||
165 | * @param array $additionalParams |
||
166 | * |
||
167 | * @param array $extraParams |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 8 | private function getQuery( $additionalParams, array $extraParams = [] ) { |
|
180 | |||
181 | /** |
||
182 | * @param array $array |
||
183 | * |
||
184 | * @return Revisions |
||
185 | */ |
||
186 | 8 | private function getRevisionsFromResult( $array ) { |
|
209 | |||
210 | /** |
||
211 | * @param string $model |
||
212 | * @param string $content returned from the API |
||
213 | * |
||
214 | * @throws RuntimeException |
||
215 | * @return Content |
||
216 | 8 | */ |
|
217 | 8 | private function getContent( $model, $content ) { |
|
220 | |||
221 | /** |
||
222 | * @param array $array |
||
223 | * |
||
224 | * @return Page |
||
225 | 8 | */ |
|
226 | 8 | private function newPageFromResult( $array ) { |
|
246 | |||
247 | } |
||
248 |
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.