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