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 |
||
13 | class Revisions { |
||
14 | |||
15 | /** |
||
16 | * @var Revision[] |
||
17 | */ |
||
18 | private $revisions; |
||
19 | |||
20 | /** |
||
21 | * @param Revisions[] $revisions |
||
22 | 4 | */ |
|
23 | 4 | public function __construct( $revisions = [] ) { |
|
27 | |||
28 | /** |
||
29 | * @param Revision[]|Revisions $revisions |
||
30 | * |
||
31 | * @throws InvalidArgumentException |
||
32 | 4 | */ |
|
33 | 4 | View Code Duplication | public function addRevisions( $revisions ) { |
44 | |||
45 | /** |
||
46 | * @param Revision $revision |
||
47 | 4 | */ |
|
48 | 4 | public function addRevision( Revision $revision ) { |
|
51 | |||
52 | /** |
||
53 | * @param int $id |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function hasRevisionWithId( $id ) { |
||
60 | |||
61 | /** |
||
62 | * @param Revision $revision |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function hasRevision( Revision $revision ) { |
||
69 | |||
70 | /** |
||
71 | * @return Revision|null Revision or null if there is no revision |
||
72 | */ |
||
73 | public function getLatest() { |
||
79 | |||
80 | /** |
||
81 | * @param int $revid |
||
82 | * |
||
83 | * @throws RuntimeException |
||
84 | * @throws InvalidArgumentException |
||
85 | * @return Revision |
||
86 | */ |
||
87 | public function get( $revid ) { |
||
96 | |||
97 | /** |
||
98 | * @return Revision[] |
||
99 | 4 | */ |
|
100 | 4 | public function toArray() { |
|
103 | } |
||
104 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: