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 declare(strict_types = 1); | ||
| 5 | final class MatchTester | ||
| 6 | { | ||
| 7 | /** | ||
| 8 | * Thou shalt not instantiate | ||
| 9 | */ | ||
| 10 |     private function __construct() { } | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Lookup table of all built-in types | ||
| 14 | */ | ||
| 15 | private static $builtInTypes = [ | ||
| 16 | BuiltInTypes::STRING => true, | ||
| 17 | BuiltInTypes::INT => true, | ||
| 18 | BuiltInTypes::FLOAT => true, | ||
| 19 | BuiltInTypes::BOOL => true, | ||
| 20 | BuiltInTypes::ARRAY => true, | ||
| 21 | BuiltInTypes::CALLABLE => true, | ||
| 22 | BuiltInTypes::VOID => true, | ||
| 23 | BuiltInTypes::ITERABLE => true, | ||
| 24 | ]; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * Lookup table of scalar types | ||
| 28 | */ | ||
| 29 | private static $scalarTypes = [ | ||
| 30 | BuiltInTypes::STRING => true, | ||
| 31 | BuiltInTypes::INT => true, | ||
| 32 | BuiltInTypes::FLOAT => true, | ||
| 33 | BuiltInTypes::BOOL => true, | ||
| 34 | ]; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * @param string $superTypeName | ||
| 38 | * @param string $subTypeName | ||
| 39 | * @return bool | ||
| 40 | */ | ||
| 41 | 16 | public static function isWeakScalarMatch($superTypeName, $subTypeName) | |
| 60 | |||
| 61 | /** | ||
| 62 | * @param string|null $superTypeName | ||
| 63 | * @param bool $superTypeNullable | ||
| 64 | * @param string|null $subTypeName | ||
| 65 | * @param bool $subTypeNullable | ||
| 66 | * @param bool $weak | ||
| 67 | * @return bool | ||
| 68 | */ | ||
| 69 | 90 | public static function isMatch($superTypeName, $superTypeNullable, $subTypeName, $subTypeNullable, $weak) | |
| 122 | } | ||
| 123 |