Complex classes like Object 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 Object, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Object { |
||
| 23 | use Extensible; |
||
| 24 | use Injectable; |
||
| 25 | use Configurable; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string the class name |
||
| 29 | */ |
||
| 30 | public $class; |
||
| 31 | |||
| 32 | private static $_cache_inst_args = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create an object from a string representation. It treats it as a PHP constructor without the |
||
| 36 | * 'new' keyword. It also manages to construct the object without the use of eval(). |
||
| 37 | * |
||
| 38 | * Construction itself is done with Object::create(), so that Object::useCustomClass() calls |
||
| 39 | * are respected. |
||
| 40 | * |
||
| 41 | * `Object::create_from_string("Versioned('Stage','Live')")` will return the result of |
||
| 42 | * `Versioned::create('Stage', 'Live);` |
||
| 43 | * |
||
| 44 | * It is designed for simple, cloneable objects. The first time this method is called for a given |
||
| 45 | * string it is cached, and clones of that object are returned. |
||
| 46 | * |
||
| 47 | * If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's |
||
| 48 | * impossible to pass null as the firstArg argument. |
||
| 49 | * |
||
| 50 | * `Object::create_from_string("Varchar(50)", "MyField")` will return the result of |
||
| 51 | * `Varchar::create('MyField', '50');` |
||
| 52 | * |
||
| 53 | * Arguments are always strings, although this is a quirk of the current implementation rather |
||
| 54 | * than something that can be relied upon. |
||
| 55 | * |
||
| 56 | * @param string $classSpec |
||
| 57 | * @param mixed $firstArg |
||
| 58 | * @return object |
||
| 59 | */ |
||
| 60 | public static function create_from_string($classSpec, $firstArg = null) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string(). |
||
| 88 | * Returns a 2-element array, with classname and arguments |
||
| 89 | * |
||
| 90 | * @param string $classSpec |
||
| 91 | * @return array |
||
| 92 | * @throws Exception |
||
| 93 | */ |
||
| 94 | public static function parse_class_spec($classSpec) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the value of a static property of a class, even in that property is declared protected (but not private), |
||
| 177 | * without any inheritance, merging or parent lookup if it doesn't exist on the given class. |
||
| 178 | * |
||
| 179 | * @static |
||
| 180 | * @param $class - The class to get the static from |
||
| 181 | * @param $name - The property to get from the class |
||
| 182 | * @param null $default - The value to return if property doesn't exist on class |
||
| 183 | * @return any - The value of the static property $name on class $class, or $default if that property is not |
||
| 184 | * defined |
||
| 185 | */ |
||
| 186 | public static function static_lookup($class, $name, $default = null) { |
||
| 222 | |||
| 223 | public function __construct() { |
||
| 227 | |||
| 228 | // -------------------------------------------------------------------------------------------------------------- |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return true if this object "exists" i.e. has a sensible value |
||
| 232 | * |
||
| 233 | * This method should be overriden in subclasses to provide more context about the classes state. For example, a |
||
| 234 | * {@link DataObject} class could return false when it is deleted from the database |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function exists() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return string this classes parent class |
||
| 244 | */ |
||
| 245 | public function parentClass() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Check if this class is an instance of a specific class, or has that class as one of its parents |
||
| 251 | * |
||
| 252 | * @param string $class |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function is_a($class) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string the class name |
||
| 261 | */ |
||
| 262 | public function __toString() { |
||
| 265 | |||
| 266 | } |
||
| 267 |
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_CBCcould 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.