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 |
||
19 | class IntegerConverter implements Converter |
||
20 | { |
||
21 | const FLOAT_VALIDATOR_IDENTIFIER = 'IntegerValueValidator'; |
||
22 | |||
23 | const HAS_MIN_VALUE = 1; |
||
24 | const HAS_MAX_VALUE = 2; |
||
25 | |||
26 | /** |
||
27 | * Factory for current class. |
||
28 | * |
||
29 | * @note Class should instead be configured as service if it gains dependencies. |
||
30 | * |
||
31 | * @return Integer |
||
32 | */ |
||
33 | public static function create() |
||
37 | |||
38 | /** |
||
39 | * Converts data from $value to $storageFieldValue. |
||
40 | * |
||
41 | * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value |
||
42 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue |
||
43 | */ |
||
44 | public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) |
||
49 | |||
50 | /** |
||
51 | * Converts data from $value to $fieldValue. |
||
52 | * |
||
53 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
54 | * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue |
||
55 | */ |
||
56 | public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
||
61 | |||
62 | /** |
||
63 | * Converts field definition data in $fieldDef into $storageFieldDef. |
||
64 | * |
||
65 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
66 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
67 | */ |
||
68 | public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) |
||
90 | |||
91 | /** |
||
92 | * Converts field definition data in $storageDef into $fieldDef. |
||
93 | * |
||
94 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
95 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
96 | */ |
||
97 | public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) |
||
111 | |||
112 | /** |
||
113 | * Returns the name of the index column in the attribute table. |
||
114 | * |
||
115 | * Returns the name of the index column the datatype uses, which is either |
||
116 | * "sort_key_int" or "sort_key_string". This column is then used for |
||
117 | * filtering and sorting for this type. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getIndexColumn() |
||
125 | |||
126 | /** |
||
127 | * Returns validator state for storage definition. |
||
128 | * Validator state is a bitfield value composed of: |
||
129 | * - {@link self::HAS_MAX_VALUE} |
||
130 | * - {@link self::HAS_MIN_VALUE}. |
||
131 | * |
||
132 | * @param int|null $minValue Minimum int value, or null if not set |
||
133 | * @param int|null $maxValue Maximum int value, or null if not set |
||
134 | * |
||
135 | * @return int |
||
136 | */ |
||
137 | View Code Duplication | private function getStorageDefValidatorState($minValue, $maxValue) |
|
151 | } |
||
152 |
If you suppress an error, we recommend checking for the error condition explicitly: