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 declare(strict_types=1); |
||
33 | class Repository extends BaseRepository |
||
34 | { |
||
35 | public function builds(): Observable |
||
36 | { |
||
37 | return unwrapObservableFromPromise($this->handleCommand( |
||
38 | new BuildsCommand($this->slug()) |
||
39 | )); |
||
40 | } |
||
41 | |||
42 | public function jobs(int $buildId): Observable |
||
43 | { |
||
44 | return Promise::toObservable($this->build($buildId))->flatMap(function (Build $build) { |
||
|
|||
45 | return $build->jobs(); |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param int $id |
||
51 | * @return PromiseInterface |
||
52 | */ |
||
53 | View Code Duplication | public function build(int $id): PromiseInterface |
|
54 | { |
||
55 | return $this->handleCommand( |
||
56 | new SimpleRequestCommand('repos/' . $this->slug() . '/builds/' . $id) |
||
57 | )->then(function (ResponseInterface $response) { |
||
58 | return resolve($this->handleCommand( |
||
59 | new HydrateCommand('Build', $response->getBody()->getJson()['build']) |
||
60 | )); |
||
61 | }); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return ObservableInterface |
||
66 | */ |
||
67 | public function commits(): ObservableInterface |
||
68 | { |
||
69 | return unwrapObservableFromPromise($this->handleCommand( |
||
70 | new CommitsCommand($this->slug()) |
||
71 | )); |
||
72 | } |
||
73 | |||
74 | public function events(): Observable |
||
105 | |||
106 | /** |
||
107 | * @return PromiseInterface |
||
108 | */ |
||
109 | public function settings(): PromiseInterface |
||
115 | |||
116 | /** |
||
117 | * @return PromiseInterface |
||
118 | */ |
||
119 | public function isActive(): PromiseInterface |
||
137 | |||
138 | /** |
||
139 | * @return PromiseInterface |
||
140 | */ |
||
141 | public function enable(): PromiseInterface |
||
145 | |||
146 | /** |
||
147 | * @return PromiseInterface |
||
148 | */ |
||
149 | public function disable(): PromiseInterface |
||
153 | |||
154 | /** |
||
155 | * @param bool $status |
||
156 | * @return PromiseInterface |
||
157 | */ |
||
158 | protected function setActiveStatus(bool $status) |
||
175 | |||
176 | /** |
||
177 | * @return ObservableInterface |
||
178 | */ |
||
179 | public function branches(): ObservableInterface |
||
185 | |||
186 | /** |
||
187 | * @return ObservableInterface |
||
188 | */ |
||
189 | public function vars(): ObservableInterface |
||
195 | |||
196 | /** |
||
197 | * @return ObservableInterface |
||
198 | */ |
||
199 | public function caches(): ObservableInterface |
||
205 | |||
206 | /** |
||
207 | * @return PromiseInterface |
||
208 | */ |
||
209 | public function key(): PromiseInterface |
||
215 | |||
216 | public function refresh(): PromiseInterface |
||
222 | } |
||
223 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.