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 |
||
22 | class DateConverter implements Converter |
||
23 | { |
||
24 | /** |
||
25 | * Factory for current class. |
||
26 | * |
||
27 | * Note: Class should instead be configured as service if it gains dependencies. |
||
28 | * |
||
29 | * @deprecated since 6.8, will be removed in 7.x, use default constructor instead. |
||
30 | * |
||
31 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\DateConverter |
||
32 | */ |
||
33 | public static function create() |
||
34 | { |
||
35 | return new self(); |
||
36 | } |
||
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) |
||
45 | { |
||
46 | $storageFieldValue->dataInt = ($value->data !== null ? $value->data['timestamp'] : null); |
||
47 | $storageFieldValue->sortKeyInt = (int)$value->sortKey; |
||
48 | } |
||
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 | View Code Duplication | public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
|
57 | { |
||
58 | if ($value->dataInt === null || $value->dataInt == 0) { |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | $fieldValue->data = array( |
||
63 | 'timestamp' => $value->dataInt, |
||
64 | 'rfc850' => null, |
||
65 | ); |
||
66 | $fieldValue->sortKey = $value->sortKeyInt; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Converts field definition data in $fieldDef into $storageFieldDef. |
||
71 | * |
||
72 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
73 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
74 | */ |
||
75 | public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) |
||
76 | { |
||
77 | $storageDef->dataInt1 = $fieldDef->fieldTypeConstraints->fieldSettings['defaultType']; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Converts field definition data in $storageDef into $fieldDef. |
||
82 | * |
||
83 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
84 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
85 | */ |
||
86 | public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) |
||
87 | { |
||
88 | $fieldDef->fieldTypeConstraints->fieldSettings = new FieldSettings( |
||
89 | array( |
||
90 | 'defaultType' => $storageDef->dataInt1, |
||
91 | ) |
||
92 | ); |
||
93 | |||
94 | // Building default value |
||
95 | switch ($fieldDef->fieldTypeConstraints->fieldSettings['defaultType']) { |
||
96 | case DateType::DEFAULT_CURRENT_DATE: |
||
97 | $data = array( |
||
98 | 'timestamp' => time(), // @deprecated timestamp is no longer used and will be removed in a future version. |
||
99 | 'rfc850' => null, |
||
100 | 'timestring' => 'now', |
||
101 | ); |
||
102 | break; |
||
103 | default: |
||
104 | $data = null; |
||
105 | } |
||
106 | |||
107 | $fieldDef->defaultValue->data = $data; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns the name of the index column in the attribute table. |
||
112 | * |
||
113 | * Returns the name of the index column the datatype uses, which is either |
||
114 | * "sort_key_int" or "sort_key_string". This column is then used for |
||
115 | * filtering and sorting for this type. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getIndexColumn() |
||
123 | } |
||
124 |