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 |
||
15 | class AccountFactory |
||
16 | { |
||
17 | /** |
||
18 | * @var Format[] Loaded formats |
||
19 | */ |
||
20 | private $formats; |
||
21 | |||
22 | /** |
||
23 | * @var RewriterStrategy[] Strategies for rewriting failing account numbers |
||
24 | */ |
||
25 | private $rewriteStrategies; |
||
26 | |||
27 | /** |
||
28 | * @var bool Flag if rewrites should be allowed when creating account objects |
||
29 | */ |
||
30 | private $allowRewrites; |
||
31 | |||
32 | /** |
||
33 | * @var UnknownFormat The unknown format or null if unknown is disallowed |
||
34 | */ |
||
35 | private $unknownFormat; |
||
36 | |||
37 | /** |
||
38 | * @param Format[] $formats |
||
39 | * @param RewriterStrategy[] $rewrites |
||
40 | * @param boolean $allowRewrites Flag if rewrites should be allowed when creating account objects |
||
41 | * @param boolean $allowUnknown Flag if the unknown account format should be used |
||
42 | */ |
||
43 | 10 | public function __construct(array $formats = [], array $rewrites = [], $allowRewrites = true, $allowUnknown = true) |
|
50 | |||
51 | /** |
||
52 | * Enable formats |
||
53 | * |
||
54 | * Please note that formats not listed will be dropped and |
||
55 | * can not be recovered. |
||
56 | * |
||
57 | * @param string[] $formats List of formats to whitelist |
||
58 | * @return null |
||
59 | */ |
||
60 | 1 | public function whitelistFormats(array $formats) |
|
68 | |||
69 | /** |
||
70 | * Disable formats |
||
71 | * |
||
72 | * Please note that listed formats will be dropped and |
||
73 | * can not be recovered. |
||
74 | * |
||
75 | * @param string[] $formats List of formats to blacklist |
||
76 | * @return null |
||
77 | */ |
||
78 | 1 | public function blacklistFormats(array $formats) |
|
84 | |||
85 | /** |
||
86 | * Create bank account object using number |
||
87 | * |
||
88 | * @param string $number |
||
89 | * @return AccountNumber |
||
90 | * @throws UnableToCreateAccountException If unable to create |
||
91 | */ |
||
92 | 10 | public function createAccount($number) |
|
158 | |||
159 | /** |
||
160 | * @param string $number |
||
161 | * @return array |
||
162 | */ |
||
163 | 10 | private function createParseMap($number) |
|
199 | } |
||
200 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..