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 |
||
27 | class Aperture implements JsonSerializable |
||
28 | { |
||
29 | /** |
||
30 | * The f-number |
||
31 | * |
||
32 | * @see https://en.wikipedia.org/wiki/F-number |
||
33 | * |
||
34 | * @var float |
||
35 | */ |
||
36 | private $fNumber; |
||
37 | |||
38 | /** |
||
39 | * @param float $fNumber |
||
40 | * |
||
41 | * @throws InvalidArgumentException If given f-number is not a float |
||
42 | */ |
||
43 | public function __construct($fNumber) |
||
51 | |||
52 | /** |
||
53 | * Sets the fNumber |
||
54 | * |
||
55 | * @param float|int $fNumber |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | private function setFNumber($fNumber) |
||
63 | |||
64 | /** |
||
65 | * Getter for fNumber |
||
66 | * |
||
67 | * @return float|int |
||
68 | */ |
||
69 | public function getFNumber() |
||
73 | |||
74 | /** |
||
75 | * Creates new instance from given Focal Length format |
||
76 | * |
||
77 | * Format: |
||
78 | * f/<f-number> |
||
79 | * |
||
80 | * @param string $focalLength |
||
81 | * |
||
82 | * @throws \InvalidArgumentException If focalLength is not a string |
||
83 | * |
||
84 | * @return Aperture |
||
85 | */ |
||
86 | View Code Duplication | public static function fromFocalLength($focalLength) |
|
87 | { |
||
88 | if (!is_string($focalLength)) { |
||
89 | throw new InvalidArgumentException('focalLength must be a string'); |
||
90 | } |
||
91 | |||
92 | if (!preg_match('#^f/([0-9]*\.[0-9]+|[0-9]*)$#', $focalLength, $matches)) { |
||
93 | throw new RuntimeException('Given focalLength is not in a valid format. Need: "f/<number>"'); |
||
94 | } |
||
95 | |||
96 | $fNumber = $matches[1]; |
||
97 | |||
98 | if (($filtered = filter_var($fNumber, FILTER_VALIDATE_INT)) !== false) { |
||
99 | $fNumber = $filtered; |
||
100 | } else { |
||
101 | $fNumber = (float) $fNumber; |
||
102 | } |
||
103 | |||
104 | return new self($fNumber); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Creates a new instance from given Aperture object |
||
109 | * |
||
110 | * @param Aperture $aperture |
||
111 | * |
||
112 | * @return Aperture |
||
113 | */ |
||
114 | public static function fromAperture(Aperture $aperture) |
||
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | public function jsonSerialize() |
||
130 | |||
131 | /** |
||
132 | * Returns string representation |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function __toString() |
||
143 | } |
||
144 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.