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[] Loaded formats |
||
| 18 | */ |
||
| 19 | private $formats; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var RewriterStrategy[] Strategies for rewriting failing account numbers |
||
| 23 | */ |
||
| 24 | private $rewriteStrategies; |
||
| 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 | * @param Format[] $formats |
||
| 38 | * @param RewriterStrategy[] $rewrites |
||
| 39 | * @param boolean $allowRewrites Flag if rewrites should be allowed when creating account objects |
||
| 40 | * @param boolean $allowUnknown Flag if the unknown account format should be used |
||
| 41 | */ |
||
| 42 | 9 | public function __construct(array $formats = [], array $rewrites = [], $allowRewrites = true, $allowUnknown = true) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Enable formats |
||
| 52 | * |
||
| 53 | * Please note that formats not listed will be dropped and |
||
| 54 | * can not be recovered. |
||
| 55 | * |
||
| 56 | * @param string[] $formats List of formats to whitelist |
||
| 57 | * @return null |
||
| 58 | */ |
||
| 59 | 1 | public function whitelistFormats(array $formats) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Disable formats |
||
| 70 | * |
||
| 71 | * Please note that listed formats will be dropped and |
||
| 72 | * can not be recovered. |
||
| 73 | * |
||
| 74 | * @param string[] $formats List of formats to blacklist |
||
| 75 | * @return null |
||
| 76 | */ |
||
| 77 | 1 | public function blacklistFormats(array $formats) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Create bank account object using number |
||
| 86 | * |
||
| 87 | * @param string $number |
||
| 88 | * @return AccountNumber |
||
| 89 | * @throws UnableToCreateAccountException If unable to create |
||
| 90 | */ |
||
| 91 | 9 | public function createAccount($number) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $number |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 9 | private function createParseMap($number) |
|
| 184 | } |
||
| 185 |
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..