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 TypeFactory |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Available data types. |
||
| 19 | * Mapped by the type name/key to fully-qualified class name. |
||
| 20 | * The class must extend the abstract DataTypes\Types\Type class. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $types = [ |
||
| 25 | 'array' => 'As3\Modlr\DataTypes\Types\ArrayType', |
||
| 26 | 'boolean' => 'As3\Modlr\DataTypes\Types\BooleanType', |
||
| 27 | 'date' => 'As3\Modlr\DataTypes\Types\DateType', |
||
| 28 | 'float' => 'As3\Modlr\DataTypes\Types\FloatType', |
||
| 29 | 'integer' => 'As3\Modlr\DataTypes\Types\IntegerType', |
||
| 30 | 'mixed' => 'As3\Modlr\DataTypes\Types\MixedType', |
||
| 31 | 'object' => 'As3\Modlr\DataTypes\Types\ObjectType', |
||
| 32 | 'string' => 'As3\Modlr\DataTypes\Types\StringType', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * In-memory loaded type objects. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $loaded = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Converts the value to the internal, Modlr (PHP) value. |
||
| 44 | * |
||
| 45 | * @param string $name The data type name. |
||
| 46 | * @param mixed $value The value to convert. |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public function convertToModlrValue($name, $value) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Gets all registered types by name/key. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getTypes() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Gets a type object. |
||
| 66 | * |
||
| 67 | * @param string $name |
||
| 68 | * @return DataTypes\Types\Type |
||
| 69 | * @throws InvalidArgumentException If the type wasn't found. |
||
| 70 | */ |
||
| 71 | public function getType($name) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Adds a type object. |
||
| 89 | * |
||
| 90 | * @param string $name |
||
| 91 | * @param string $fqcn |
||
| 92 | * @return self |
||
| 93 | * @throws InvalidArgumentException If the type already exists. |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function addType($name, $fqcn) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Overrides a type object with new class. |
||
| 105 | * |
||
| 106 | * @param string $name |
||
| 107 | * @param string $fqcn |
||
| 108 | * @return self |
||
| 109 | * @throws InvalidArgumentException If the type was not found. |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function overrideType($name, $fqcn) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Sets a type. |
||
| 121 | * |
||
| 122 | * @param string $name |
||
| 123 | * @param string $fqcn |
||
| 124 | * @return self |
||
| 125 | */ |
||
| 126 | private function setType($name, $fqcn) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Determines if a type exists. |
||
| 134 | * |
||
| 135 | * @param string $name |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function hasType($name) |
||
| 142 | } |
||
| 143 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.