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 JsonDefinitionField implements DefinitionElementInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Typemap from our source types to doctrine types |
||
| 19 | */ |
||
| 20 | private static $doctrineTypeMap = [ |
||
| 21 | self::TYPE_STRING => 'string', |
||
| 22 | self::TYPE_VARCHAR => 'string', |
||
| 23 | self::TYPE_TEXT => 'string', |
||
| 24 | self::TYPE_INTEGER => 'int', |
||
| 25 | self::TYPE_LONG => 'int', |
||
| 26 | self::TYPE_FLOAT => 'float', |
||
| 27 | self::TYPE_DOUBLE => 'float', |
||
| 28 | self::TYPE_DECIMAL => 'float', |
||
| 29 | self::TYPE_DATETIME => 'date', |
||
| 30 | self::TYPE_BOOLEAN => 'boolean', |
||
| 31 | self::TYPE_OBJECT => 'hash', |
||
| 32 | self::TYPE_EXTREF => 'extref', |
||
| 33 | ]; |
||
| 34 | |||
| 35 | private static $serializerTypeMap = [ |
||
| 36 | self::TYPE_STRING => 'string', |
||
| 37 | self::TYPE_VARCHAR => 'string', |
||
| 38 | self::TYPE_TEXT => 'string', |
||
| 39 | self::TYPE_INTEGER => 'integer', |
||
| 40 | self::TYPE_LONG => 'integer', |
||
| 41 | self::TYPE_FLOAT => 'double', |
||
| 42 | self::TYPE_DOUBLE => 'double', |
||
| 43 | self::TYPE_DECIMAL => 'double', |
||
| 44 | self::TYPE_DATETIME => 'DateTime', |
||
| 45 | self::TYPE_BOOLEAN => 'boolean', |
||
| 46 | self::TYPE_OBJECT => Hash::class, |
||
| 47 | self::TYPE_EXTREF => ExtReference::class, |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $name; |
||
| 54 | /** |
||
| 55 | * Our definition |
||
| 56 | * |
||
| 57 | * @var Schema\Field |
||
| 58 | */ |
||
| 59 | private $definition; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param string $name Field name |
||
| 65 | * @param Schema\Field $definition Definition |
||
| 66 | */ |
||
| 67 | 43 | public function __construct($name, Schema\Field $definition) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the field definition |
||
| 75 | * |
||
| 76 | * @return Schema\Field definition |
||
| 77 | */ |
||
| 78 | 12 | public function getDef() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the field name |
||
| 85 | * |
||
| 86 | * @return string Name |
||
| 87 | */ |
||
| 88 | 11 | public function getName() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the whole definition in array form |
||
| 95 | * |
||
| 96 | * @return array Definition |
||
|
|
|||
| 97 | */ |
||
| 98 | 8 | public function getDefAsArray() |
|
| 99 | { |
||
| 100 | return [ |
||
| 101 | 8 | 'length' => $this->definition->getLength(), |
|
| 102 | 8 | 'title' => $this->definition->getTitle(), |
|
| 103 | 8 | 'description' => $this->definition->getDescription(), |
|
| 104 | 8 | 'readOnly' => $this->definition->getReadOnly(), |
|
| 105 | 8 | 'required' => $this->definition->getRequired(), |
|
| 106 | 8 | 'searchable' => $this->definition->getSearchable(), |
|
| 107 | 8 | 'translatable' => $this->definition->getTranslatable(), |
|
| 108 | 8 | 'collection' => $this->definition->getCollection(), |
|
| 109 | |||
| 110 | 8 | 'name' => $this->getName(), |
|
| 111 | 8 | 'type' => $this->getType(), |
|
| 112 | 8 | 'exposedName' => $this->getExposedName(), |
|
| 113 | 8 | 'doctrineType' => $this->getTypeDoctrine(), |
|
| 114 | 8 | 'serializerType' => $this->getTypeSerializer(), |
|
| 115 | 8 | 'xDynamicKey' => $this->getXDynamicKey(), |
|
| 116 | 8 | 'relType' => null, |
|
| 117 | 8 | 'isClassType' => false, |
|
| 118 | 8 | 'constraints' => array_map( |
|
| 119 | View Code Duplication | function (Schema\Constraint $constraint) { |
|
| 120 | return [ |
||
| 121 | 3 | 'name' => $constraint->getName(), |
|
| 122 | 3 | 'options' => array_map( |
|
| 123 | 3 | function (Schema\ConstraintOption $option) { |
|
| 124 | return [ |
||
| 125 | 3 | 'name' => $option->getName(), |
|
| 126 | 3 | 'value' => $option->getValue(), |
|
| 127 | 3 | ]; |
|
| 128 | 3 | }, |
|
| 129 | 3 | $constraint->getOptions() |
|
| 130 | 3 | ) |
|
| 131 | 3 | ]; |
|
| 132 | 8 | }, |
|
| 133 | 8 | $this->definition->getConstraints() |
|
| 134 | 8 | ) |
|
| 135 | 8 | ]; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the field type in a doctrine-understandable way.. |
||
| 140 | * |
||
| 141 | * @return string Type |
||
| 142 | */ |
||
| 143 | 16 | public function getTypeDoctrine() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the field type |
||
| 155 | * |
||
| 156 | * @return string Type |
||
| 157 | */ |
||
| 158 | 28 | public function getType() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the field type in a serializer-understandable way.. |
||
| 165 | * |
||
| 166 | * @return string Type |
||
| 167 | */ |
||
| 168 | 16 | public function getTypeSerializer() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return XDynamicKey|void |
||
| 180 | */ |
||
| 181 | 8 | public function getXDynamicKey() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Gets the name this field should be exposed as (serializer concern). |
||
| 191 | * Normally this is the name, but can be overriden by "exposeAs" property on the field. |
||
| 192 | * |
||
| 193 | * @return string exposed field name |
||
| 194 | */ |
||
| 195 | 8 | private function getExposedName() |
|
| 201 | } |
||
| 202 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.