Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Validator { |
||
18 | |||
19 | private function __construct() {} |
||
20 | |||
21 | /** |
||
22 | * Short-cut methods for determining if a value is of a particular type. |
||
23 | * @param string $name |
||
24 | * @param array $args array of arguments pass to the function |
||
25 | * @return boolean |
||
26 | */ |
||
27 | public static function __callStatic( $name, $args ) { |
||
46 | |||
47 | |||
48 | public static function isEmpty( $v, $type = Type::TEXT ) { |
||
77 | |||
78 | /** |
||
79 | * Validates a string has the correct encoding and trims whitespace. |
||
80 | * @param string $v |
||
81 | * @param string $encoding |
||
82 | * @return string|boolean Returns the trimmed string or false if incorrect encoding |
||
83 | */ |
||
84 | public static function validateText( $v, $encoding = 'UTF-8' ) { |
||
87 | |||
88 | /** |
||
89 | * Validates a value as an integer. |
||
90 | * Null, false and empty strings are converted to zero. |
||
91 | * @param mixed $v |
||
92 | * @return integer|false |
||
93 | */ |
||
94 | public static function validateInteger( $v ) { |
||
97 | |||
98 | /** |
||
99 | * Validates a value as a float. |
||
100 | * Null, false and empty strings are converted to zero. |
||
101 | * @param mixed $v |
||
102 | * @return float|false |
||
103 | */ |
||
104 | public static function validateFloat( $v ) { |
||
107 | |||
108 | /** |
||
109 | * Validates a value as a boolean. |
||
110 | * Recognises the following string: "1", "true", "on" and "yes". |
||
111 | * @param mixed $v |
||
112 | * @return boolean|null |
||
113 | */ |
||
114 | public static function validateBoolean( $v ) { |
||
118 | |||
119 | public static function validateTimestamp( $v ) { |
||
125 | |||
126 | public static function validateDateTime( $v, $format = 'Y-m-d H:i:s' ) { |
||
136 | |||
137 | public static function validateDate( $v ) { |
||
140 | |||
141 | public static function validateTime( $v ) { |
||
144 | |||
145 | public static function validateYear( $v, $min = 1900, $max = 2155 ) { |
||
149 | |||
150 | /** |
||
151 | * Validates a value as an ip4 address. |
||
152 | * @param mixed $v |
||
153 | * @return string |
||
154 | */ |
||
155 | public static function validateIP( $v ) { |
||
161 | |||
162 | /** |
||
163 | * Validates a value as an email address. |
||
164 | * Empty values are allowed and are converted to an empty string. |
||
165 | * @param mixed $v |
||
166 | * @return string|boolean |
||
167 | */ |
||
168 | public static function validateEmail( $v ) { |
||
171 | |||
172 | /** |
||
173 | * Validates a value as a url. |
||
174 | * Empty values are allowed and are converted to an empty string. |
||
175 | * @param mixed $v |
||
176 | * @return string|boolean |
||
177 | */ |
||
178 | public static function validateURL( $v ) { |
||
181 | |||
182 | public static function validateJSON( $v ) { |
||
194 | |||
195 | public static function validateObject( $v, $class, $nullable = false ) { |
||
206 | |||
207 | } |
||
208 | |||
209 | // EOF |
According to the PSR-2, the body of a default statement must start on the line immediately following the statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.