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 |
||
14 | class AccountFactory |
||
15 | { |
||
16 | /** |
||
17 | * @var Format[] Possible formats used when parsing account |
||
18 | */ |
||
19 | private $formats; |
||
20 | |||
21 | /** |
||
22 | * @var RewriterStrategy[] Strategies for rewriting failing account numbers |
||
23 | */ |
||
24 | private $rewrites; |
||
25 | |||
26 | /** |
||
27 | * @var bool Flag if rewrites should be allowed when creating account objects |
||
28 | */ |
||
29 | private $allowRewrites; |
||
30 | |||
31 | /** |
||
32 | * @var UnknownFormat The unknown format or null if unknown is disallowed |
||
33 | */ |
||
34 | private $unknownFormat; |
||
35 | |||
36 | /** |
||
37 | * @var RewriterStrategy[] Preprocessors used to alter account before parsing starts |
||
38 | */ |
||
39 | private $preprocessors; |
||
40 | |||
41 | /** |
||
42 | * @param Format[] $formats Possible formats used when parsing account |
||
43 | * @param RewriterStrategy[] $rewrites Rewrites used if parsed the raw account number fails |
||
44 | * @param boolean $allowRewrites Flag if rewrites should be allowed when creating account objects |
||
45 | * @param boolean $allowUnknown Flag if the unknown account format should be used |
||
46 | * @param RewriterStrategy[] $preprocessors Preprocessors used to alter account before parsing starts |
||
47 | */ |
||
48 | 110 | public function __construct( |
|
61 | |||
62 | /** |
||
63 | * Enable formats |
||
64 | * |
||
65 | * Please note that formats not listed will be dropped and |
||
66 | * can not be recovered. |
||
67 | * |
||
68 | * @param string[] $formats List of formats to whitelist |
||
69 | * @return null |
||
70 | */ |
||
71 | 1 | public function whitelistFormats(array $formats) |
|
79 | |||
80 | /** |
||
81 | * Disable formats |
||
82 | * |
||
83 | * Please note that listed formats will be dropped and |
||
84 | * can not be recovered. |
||
85 | * |
||
86 | * @param string[] $formats List of formats to blacklist |
||
87 | * @return null |
||
88 | */ |
||
89 | 100 | public function blacklistFormats(array $formats) |
|
95 | |||
96 | /** |
||
97 | * Create bank account object using number |
||
98 | * |
||
99 | * @param string $number |
||
100 | * @return AccountNumber |
||
101 | * @throws UnableToCreateAccountException If unable to create |
||
102 | */ |
||
103 | 110 | public function createAccount($number) |
|
203 | } |
||
204 |
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..