Total Complexity | 47 |
Total Lines | 206 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Complex classes like TPropertyValue 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.
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 TPropertyValue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
51 | class TPropertyValue |
||
52 | { |
||
53 | /** |
||
54 | * Converts a value to boolean type. |
||
55 | * Note, string 'true' (case-insensitive) will be converted to true, |
||
56 | * string 'false' (case-insensitive) will be converted to false. |
||
57 | * If a string represents a non-zero number, it will be treated as true. |
||
58 | * @param mixed $value the value to be converted. |
||
59 | * @return bool |
||
60 | */ |
||
61 | public static function ensureBoolean($value): bool |
||
62 | { |
||
63 | if (is_string($value)) { |
||
64 | return strcasecmp($value, 'true') == 0 || (is_numeric($value) && $value != 0); |
||
65 | } else { |
||
66 | return (bool) $value; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Converts a value to string type. |
||
72 | * Note, a boolean value will be converted to 'true' if it is true |
||
73 | * and 'false' if it is false. |
||
74 | * @param mixed $value the value to be converted. |
||
75 | * @return string |
||
76 | */ |
||
77 | public static function ensureString($value): string |
||
78 | { |
||
79 | if (TJavaScript::isJsLiteral($value)) { |
||
80 | return $value; |
||
81 | } |
||
82 | if (is_bool($value)) { |
||
83 | return $value ? 'true' : 'false'; |
||
84 | } else { |
||
85 | return (string) $value; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Converts a value to integer type. |
||
91 | * @param mixed $value the value to be converted. |
||
92 | * @return int |
||
93 | */ |
||
94 | public static function ensureInteger($value): int |
||
95 | { |
||
96 | return (int) $value; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Converts a value to float type. |
||
101 | * @param mixed $value the value to be converted. |
||
102 | * @return float |
||
103 | */ |
||
104 | public static function ensureFloat($value): float |
||
105 | { |
||
106 | return (float) $value; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Converts a value to array type. If the value is a string and it is |
||
111 | * in the form (a,b,c) then an array consisting of each of the elements |
||
112 | * will be returned. If the value is a string and it is not in this form |
||
113 | * then an array consisting of just the string will be returned. If the value |
||
114 | * is not a string then |
||
115 | * @param mixed $value the value to be converted. |
||
116 | * @return array |
||
117 | */ |
||
118 | public static function ensureArray($value): array |
||
119 | { |
||
120 | if (is_string($value)) { |
||
121 | $value = trim($value); |
||
122 | $len = strlen($value); |
||
123 | if ($len >= 2 && $value[0] == '(' && $value[$len - 1] == ')') { |
||
124 | return eval('return array' . $value . ';'); |
||
|
|||
125 | } else { |
||
126 | return $len > 0 ? [$value] : []; |
||
127 | } |
||
128 | } else { |
||
129 | return (array) $value; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Converts a value to object type. |
||
135 | * @param mixed $value the value to be converted. |
||
136 | * @return object |
||
137 | */ |
||
138 | public static function ensureObject($value): object |
||
139 | { |
||
140 | return (object) $value; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Converts a value to enum type. |
||
145 | * |
||
146 | * This method checks if the value is of the specified enumerable type. |
||
147 | * A value is a valid enumerable value if it is equal to the name of a constant |
||
148 | * in the specified enumerable type (class). |
||
149 | * For more details about enumerable, see {@see \Prado\TEnumerable}. |
||
150 | * |
||
151 | * For backward compatibility, this method also supports sanity |
||
152 | * check of a string value to see if it is among the given list of strings. |
||
153 | * @param mixed $value the value to be converted. |
||
154 | * @param mixed $enums class name of the enumerable type, or array of valid enumeration values. If this is not an array, |
||
155 | * the method considers its parameters are of variable length, and the second till the last parameters are enumeration values. |
||
156 | * @throws TInvalidDataValueException if the original value is not in the string array. |
||
157 | * @return string the valid enumeration value |
||
158 | */ |
||
159 | public static function ensureEnum($value, $enums): string |
||
160 | { |
||
161 | static $types = []; |
||
162 | if (func_num_args() === 2 && is_string($enums)) { |
||
163 | if (!isset($types[$enums])) { |
||
164 | $types[$enums] = new \ReflectionClass($enums); |
||
165 | } |
||
166 | if ($types[$enums]->hasConstant($value)) { |
||
167 | return $value; |
||
168 | } else { |
||
169 | throw new TInvalidDataValueException( |
||
170 | 'propertyvalue_enumvalue_invalid', |
||
171 | $value, |
||
172 | implode(' | ', $types[$enums]->getConstants()) |
||
173 | ); |
||
174 | } |
||
175 | } elseif (!is_array($enums)) { |
||
176 | $enums = func_get_args(); |
||
177 | array_shift($enums); |
||
178 | } |
||
179 | if (in_array($value, $enums, true)) { |
||
180 | return $value; |
||
181 | } else { |
||
182 | throw new TInvalidDataValueException('propertyvalue_enumvalue_invalid', $value, implode(' | ', $enums)); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Converts the value to 'null' if the given value is empty |
||
188 | * @param mixed $value value to be converted |
||
189 | * @return mixed input or NULL if input is empty |
||
190 | */ |
||
191 | public static function ensureNullIfEmpty($value) |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Converts the value to a web "#RRGGBB" hex color. |
||
198 | * The value[s] could be as an A) Web Color or # Hex Color string, or B) as a color |
||
199 | * encoded integer, eg 0x00RRGGBB, C) a triple ($value [red], $green, $blue), or D) |
||
200 | * an array of red, green, and blue, and index 0, 1, 2 or 'red', 'green', 'blue'. |
||
201 | * In instance (A), $green is treated as a boolean flag for whether to convert |
||
202 | * any web colors to their # hex color. When red, green, or blue colors are specified |
||
203 | * they are assumed be be bound [0...255], inclusive. |
||
204 | * @param array|float|int|string $value String Web Color name or Hex Color (eg. '#336699'), |
||
205 | * array of [$r, $g, $b] or ['red' => $red, 'green' => $green, 'blue' = $blue], or |
||
206 | * int color (0x00RRGGBB [$blue is null]), or int red [0..255] when $blue is not null. |
||
207 | * @param bool|float|int $green When $blue !== null, $green is an int color, otherwise its |
||
208 | * the flag to allow converting Web Color names to their web colors. Default true, |
||
209 | * for allow web colors to translate into their # hex color. |
||
210 | * @param null|float|int $blue The blue color. Default null for (A) or (B) |
||
211 | * @return string The valid # hex color. |
||
212 | */ |
||
213 | public static function ensureHexColor($value, $green = true, $blue = null) |
||
257 | } |
||
258 | } |
||
259 |