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 |
||
26 | class EavToolbox |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * List of accepted value types. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | public static $types = [ |
||
35 | 'biginteger', |
||
36 | 'binary', |
||
37 | 'date', |
||
38 | 'float', |
||
39 | 'decimal', |
||
40 | 'integer', |
||
41 | 'time', |
||
42 | 'datetime', |
||
43 | 'timestamp', |
||
44 | 'uuid', |
||
45 | 'string', |
||
46 | 'text', |
||
47 | 'boolean', |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * The table being managed. |
||
52 | * |
||
53 | * @var \Cake\ORM\Table |
||
54 | */ |
||
55 | protected $_table = null; |
||
56 | |||
57 | /** |
||
58 | * Attributes index by bundle, and by name within each bundle. |
||
59 | * |
||
60 | * ```php |
||
61 | * [ |
||
62 | * 'administrator' => [ |
||
63 | * 'admin-address' => [ |
||
64 | * 'type' => 'varchar', |
||
65 | * 'searchable' => false |
||
66 | * ], |
||
67 | * 'admin-phone' => [ |
||
68 | * 'type' => 'varchar', |
||
69 | * 'searchable' => true |
||
70 | * ] |
||
71 | * ], |
||
72 | * 'editor' => [ |
||
73 | * 'editor-last-login' => [ |
||
74 | * 'type' => 'datetime', |
||
75 | * 'searchable' => false, |
||
76 | * ] |
||
77 | * ] |
||
78 | * ] |
||
79 | * ``` |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $_attributes = []; |
||
84 | |||
85 | /** |
||
86 | * Constructor. |
||
87 | * |
||
88 | * @param \Cake\ORM\Table $table The table being handled |
||
89 | */ |
||
90 | public function __construct(Table $table) |
||
94 | |||
95 | /** |
||
96 | * Gets a clean column name from query expression. |
||
97 | * |
||
98 | * ### Example: |
||
99 | * |
||
100 | * ```php |
||
101 | * EavToolbox::columnName('Tablename.some_column'); |
||
102 | * // returns "some_column" |
||
103 | * |
||
104 | * EavToolbox::columnName('my_column'); |
||
105 | * // returns "my_column" |
||
106 | * ``` |
||
107 | * |
||
108 | * @param string $column Column name from query |
||
109 | * @return string |
||
110 | */ |
||
111 | public static function columnName($column) |
||
122 | |||
123 | /** |
||
124 | * Checks if the provided entity has defined certain $property, regardless of |
||
125 | * its value. |
||
126 | * |
||
127 | * @param \Cake\ORM\Entity $entity The entity to check |
||
128 | * @param string $property The property name |
||
129 | * @return bool True if exists |
||
130 | */ |
||
131 | public function propertyExists(Entity $entity, $property) |
||
137 | |||
138 | /** |
||
139 | * Marshalls flat data into PHP objects. |
||
140 | * |
||
141 | * @param mixed $value The value to convert |
||
142 | * @param string $type Type identifier, `integer`, `float`, etc |
||
143 | * @return mixed Converted value |
||
144 | */ |
||
145 | public function marshal($value, $type) |
||
149 | |||
150 | /** |
||
151 | * Gets all attributes added to this table. |
||
152 | * |
||
153 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
154 | * regardless of the bundle if not provided |
||
155 | * @return array List of attributes indexed by name (virtual column name) |
||
156 | */ |
||
157 | public function attributes($bundle = null) |
||
183 | |||
184 | /** |
||
185 | * Gets a list of attribute names. |
||
186 | * |
||
187 | * @param string $bundle Filter by bundle name |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getAttributeNames($bundle = null) |
||
196 | |||
197 | /** |
||
198 | * Gets a list of attribute IDs. |
||
199 | * |
||
200 | * @param string $bundle Filter by bundle name |
||
201 | * @return array |
||
202 | */ |
||
203 | public function getAttributeIds($bundle = null) |
||
214 | |||
215 | /** |
||
216 | * Given a set of entities gets the ID of all of them. |
||
217 | * |
||
218 | * This method iterates the given set and invokes `getEntityId()` for every |
||
219 | * entity in the set. |
||
220 | * |
||
221 | * @param \Cake\ORM\ResultSet $results Set of entities |
||
222 | * @return array List of entity ids suitable for EAV logic |
||
223 | */ |
||
224 | public function extractEntityIds(ResultSet $results) |
||
235 | |||
236 | /** |
||
237 | * Calculates entity's primary key. |
||
238 | * |
||
239 | * If PK is composed of multiple columns they will be merged with `:` symbol. |
||
240 | * For example, consider `Users` table with composed PK <nick, email>, then for |
||
241 | * certain User entity this method could return: |
||
242 | * |
||
243 | * john-locke:[email protected] |
||
244 | * |
||
245 | * @param \Cake\Datasource\EntityInterface $entity The entity |
||
246 | * @return string |
||
247 | */ |
||
248 | View Code Duplication | public function getEntityId(EntityInterface $entity) |
|
259 | |||
260 | /** |
||
261 | * Gets attribute's EAV type. |
||
262 | * |
||
263 | * @param string $attrName Attribute name |
||
264 | * @return string Attribute's EAV type |
||
265 | * @see \Eav\Model\Behavior\EavBehavior::_mapType() |
||
266 | */ |
||
267 | public function getType($attrName) |
||
271 | |||
272 | /** |
||
273 | * Gets attribute's bundle. |
||
274 | * |
||
275 | * @param string $attrName Attribute name |
||
276 | * @return string|null |
||
277 | */ |
||
278 | public function getBundle($attrName) |
||
282 | |||
283 | /** |
||
284 | * Whether the given attribute can be used in WHERE clauses. |
||
285 | * |
||
286 | * @param string $attrName Attribute name |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function isSearchable($attrName) |
||
293 | |||
294 | /** |
||
295 | * Maps schema data types to EAV's supported types. |
||
296 | * |
||
297 | * @param string $type A schema type. e.g. "string", "integer" |
||
298 | * @return string A EAV type. Possible values are `datetime`, `binary`, `time`, |
||
299 | * `date`, `float`, `intreger`, `biginteger`, `text`, `string`, `boolean` or |
||
300 | * `uuid` |
||
301 | */ |
||
302 | public function mapType($type) |
||
314 | } |
||
315 |