Complex classes like EntityProperty often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntityProperty, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class EntityProperty |
||
21 | { |
||
22 | |||
23 | const STRING = 'string'; |
||
24 | const INTEGER = 'integer'; |
||
25 | const FLOAT = 'float'; |
||
26 | const DOUBLE = 'double'; |
||
27 | const BOOL = 'bool'; |
||
28 | |||
29 | const DATE = 'date'; |
||
30 | const DATETIME = 'datetime'; |
||
31 | |||
32 | const IPADDRESS = 'ipaddress'; |
||
33 | |||
34 | const UUID = 'uuid'; |
||
35 | |||
36 | const ENUM = 'enum'; |
||
37 | |||
38 | /** |
||
39 | * @var string Entity property type |
||
40 | */ |
||
41 | protected $type; |
||
42 | |||
43 | /** |
||
44 | * @var int Entity property max length |
||
45 | */ |
||
46 | protected $length; |
||
47 | |||
48 | /** |
||
49 | * @var array Allowed values |
||
50 | */ |
||
51 | protected $values; |
||
52 | |||
53 | /** |
||
54 | * @param string|array $type |
||
55 | */ |
||
56 | 13 | public function __construct($type) |
|
85 | |||
86 | /** |
||
87 | * Set entity property type |
||
88 | * |
||
89 | * @param string $type |
||
90 | * |
||
91 | * @throws EntityPropertyTypeNonexistentException |
||
92 | */ |
||
93 | 13 | protected function setType(string $type) |
|
101 | |||
102 | /** |
||
103 | * Validate value for entity property |
||
104 | * |
||
105 | * @param mixed $value Entity property value |
||
106 | * |
||
107 | * @return bool TRUE if valid, otherwise FALSE |
||
108 | * |
||
109 | * @throws EntityPropertyTypeNotImplementedException If entity property type is not implemented |
||
110 | * @throws EntityPropertyValueExceedingLengthException If value is exceeding allowed length for entity property |
||
111 | * @throws InvalidENUMValueForEntityPropertyException If ENUM value is invalid for entity property |
||
112 | * @throws InvalidUUIDValueForEntityPropertyException If UUID value is invalid for entity property |
||
113 | * @throws InvalidValueTypeForEntityPropertyException If value is invalid for entity property |
||
114 | * @throws InvalidIPAddressValueForEntityPropertyException If IP address value is invalid |
||
115 | */ |
||
116 | 24 | public function validateValue($value): bool |
|
213 | |||
214 | /** |
||
215 | * Format value according to entity property type |
||
216 | * |
||
217 | * @param mixed $value Value |
||
218 | * |
||
219 | * @return mixed Value |
||
220 | * |
||
221 | * @throws EntityPropertyTypeNotImplementedException If entity property type is not implemented |
||
222 | */ |
||
223 | 8 | public function formatValueForEntity($value) |
|
261 | |||
262 | /** |
||
263 | * Format value for insertion into the database |
||
264 | * |
||
265 | * @param mixed $value Value |
||
266 | * |
||
267 | * @return mixed Value |
||
268 | */ |
||
269 | 4 | public function formatValueForDatabase($value) |
|
301 | } |
||
302 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.