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 |
||
| 24 | class EavToolbox |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * List of accepted value types. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | public static $types = [ |
||
| 33 | 'biginteger', |
||
| 34 | 'binary', |
||
| 35 | 'date', |
||
| 36 | 'float', |
||
| 37 | 'decimal', |
||
| 38 | 'integer', |
||
| 39 | 'time', |
||
| 40 | 'datetime', |
||
| 41 | 'timestamp', |
||
| 42 | 'uuid', |
||
| 43 | 'string', |
||
| 44 | 'text', |
||
| 45 | 'boolean', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The table being managed. |
||
| 50 | * |
||
| 51 | * @var \Cake\ORM\Table |
||
| 52 | */ |
||
| 53 | protected $_table = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Attributes index by bundle, and by name within each bundle. |
||
| 57 | * |
||
| 58 | * ```php |
||
| 59 | * [ |
||
| 60 | * 'administrator' => [ |
||
| 61 | * 'admin-address' => [ |
||
| 62 | * 'type' => 'varchar', |
||
| 63 | * 'searchable' => false |
||
| 64 | * ], |
||
| 65 | * 'admin-phone' => [ |
||
| 66 | * 'type' => 'varchar', |
||
| 67 | * 'searchable' => true |
||
| 68 | * ] |
||
| 69 | * ], |
||
| 70 | * 'editor' => [ |
||
| 71 | * 'editor-last-login' => [ |
||
| 72 | * 'type' => 'datetime', |
||
| 73 | * 'searchable' => false, |
||
| 74 | * ] |
||
| 75 | * ] |
||
| 76 | * ] |
||
| 77 | * ``` |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $_attributes = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor. |
||
| 85 | * |
||
| 86 | * @param \Cake\ORM\Table $table The table being handled |
||
| 87 | */ |
||
| 88 | public function __construct(Table $table) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Gets a clean column name from query expression. |
||
| 95 | * |
||
| 96 | * ### Example: |
||
| 97 | * |
||
| 98 | * ```php |
||
| 99 | * EavToolbox::columnName('Tablename.some_column'); |
||
| 100 | * // returns "some_column" |
||
| 101 | * |
||
| 102 | * EavToolbox::columnName('my_column'); |
||
| 103 | * // returns "my_column" |
||
| 104 | * ``` |
||
| 105 | * |
||
| 106 | * @param string $column Column name from query |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public static function columnName($column) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Checks if the provided entity has defined certain $property, regardless of |
||
| 122 | * its value. |
||
| 123 | * |
||
| 124 | * @param \Cake\ORM\Entity $entity The entity to check |
||
| 125 | * @param string $property The property name |
||
| 126 | * @return bool True if exists |
||
| 127 | */ |
||
| 128 | public function propertyExists(Entity $entity, $property) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Marshalls flat data into PHP objects. |
||
| 136 | * |
||
| 137 | * @param mixed $value The value to convert |
||
| 138 | * @param string $type Type identifier, `integer`, `float`, etc |
||
| 139 | * @return mixed Converted value |
||
| 140 | */ |
||
| 141 | public function marshal($value, $type) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Gets all attributes added to this table. |
||
| 148 | * |
||
| 149 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
| 150 | * regardless of the bundle if not provided |
||
| 151 | * @return array List of attributes indexed by name (virtual column name) |
||
| 152 | */ |
||
| 153 | public function attributes($bundle = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Gets a list of attribute names. |
||
| 182 | * |
||
| 183 | * @param string $bundle Filter by bundle name |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function getAttributeNames($bundle = null) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Calculates entity's primary key. |
||
| 194 | * |
||
| 195 | * If PK is composed of multiple columns they will be merged with `:` symbol. |
||
| 196 | * For example, consider `Users` table with composed PK <nick, email>, then for |
||
| 197 | * certain User entity this method could return: |
||
| 198 | * |
||
| 199 | * john-locke:[email protected] |
||
| 200 | * |
||
| 201 | * @param \Cake\Datasource\EntityInterface $entity The entity |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function getEntityId(EntityInterface $entity) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Gets attribute's EAV type. |
||
| 217 | * |
||
| 218 | * @param string $attrName Attribute name |
||
| 219 | * @return string Attribute's EAV type |
||
| 220 | * @see \Eav\Model\Behavior\EavBehavior::_mapType() |
||
| 221 | */ |
||
| 222 | public function getType($attrName) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets attribute's bundle. |
||
| 229 | * |
||
| 230 | * @param string $attrName Attribute name |
||
| 231 | * @return string|null |
||
| 232 | */ |
||
| 233 | public function getBundle($attrName) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Whether the given attribute can be used in WHERE clauses. |
||
| 240 | * |
||
| 241 | * @param string $attrName Attribute name |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function isSearchable($attrName) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Maps schema data types to EAV's supported types. |
||
| 251 | * |
||
| 252 | * @param string $type A schema type. e.g. "string", "integer" |
||
| 253 | * @return string A EAV type. Possible values are `datetime`, `binary`, `time`, |
||
| 254 | * `date`, `float`, `intreger`, `biginteger`, `text`, `string`, `boolean` or |
||
| 255 | * `uuid` |
||
| 256 | */ |
||
| 257 | public function mapType($type) |
||
| 269 | } |
||
| 270 |