Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | abstract class Schema implements ISchema |
||
11 | { |
||
12 | const REPLACE_MODE = 'replace'; |
||
13 | const UPDATE_MODE = 'update'; |
||
14 | |||
15 | public static function cast($object, $skipExtraneous = false) |
||
16 | { |
||
17 | return new static($object, $skipExtraneous); |
||
18 | } |
||
19 | |||
20 | public static function map($object, $map, $skipExtraneous = false) |
||
21 | { |
||
22 | $mappedObject = ObjectMapper::map($object, $map); |
||
23 | return new static($mappedObject, $skipExtraneous); |
||
24 | } |
||
25 | |||
26 | 1 | public function __construct($object = null, $skipExtraneous = false) |
|
27 | { |
||
28 | 1 | if ($object === null) { |
|
29 | 1 | return; |
|
30 | } |
||
31 | if (gettype($object) !== 'object') { |
||
32 | throw new RestException('Schema constructor expects object or null as parameter', [ |
||
33 | 'parameter' => $object, |
||
34 | ]); |
||
35 | } |
||
36 | if (!is_bool($skipExtraneous)) { |
||
37 | $skipExtraneous = false; |
||
38 | } |
||
39 | if ($skipExtraneous) { |
||
40 | $this->castPropertiesWithoutExtraneous($object, $this->schema()); |
||
41 | return; |
||
42 | } |
||
43 | $this->castProperties($object, $this->schema()); |
||
44 | } |
||
45 | |||
46 | private function castPropertiesWithoutExtraneous($object, $schema) |
||
47 | { |
||
48 | foreach (get_object_vars($object) as $name => $value) { |
||
49 | list($propertyExists, $propertyValue) = $this->castPropertyWithoutExtraneous($schema, $name, $value); |
||
50 | if ($propertyExists) { |
||
51 | $this->{$name} = $propertyValue; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | |||
56 | private function castPropertyWithoutExtraneous($schema, $name, $value) |
||
57 | { |
||
58 | if (!array_key_exists($name, $schema)) { |
||
59 | return [false, null]; |
||
60 | } |
||
61 | if (isset($schema[$name]['type']) && $schema[$name]['type'] === 'schema') { |
||
62 | return [true, $schema[$name]['validator']['class']::cast($value, true)]; |
||
63 | } |
||
64 | $params = empty($schema[$name]['validator']['params']) |
||
65 | ? null |
||
66 | : $schema[$name]['validator']['params']; |
||
67 | return [true, $schema[$name]['validator']['class']::cast($value, $params, true)]; |
||
68 | } |
||
69 | |||
70 | private function castProperties($object, $schema) |
||
71 | { |
||
72 | foreach (get_object_vars($object) as $name => $value) { |
||
73 | $this->{$name} = $this->castProperty($schema, $name, $value); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | private function castProperty($schema, $name, $value) |
||
78 | { |
||
79 | if (!array_key_exists($name, $schema)) { |
||
80 | return $value; |
||
81 | } |
||
82 | if (isset($schema[$name]['type']) && $schema[$name]['type'] === 'schema') { |
||
83 | return $schema[$name]['validator']['class']::cast($value); |
||
84 | } |
||
85 | $params = empty($schema[$name]['validator']['params']) |
||
86 | ? null |
||
87 | : $schema[$name]['validator']['params']; |
||
88 | return $schema[$name]['validator']['class']::cast($value, $params); |
||
89 | } |
||
90 | |||
91 | public function __get($name) |
||
92 | { |
||
93 | if (!isset($this->{$name})) { |
||
94 | throw new RestException( |
||
95 | 'Trying to access undefined property ' . $name, |
||
96 | [] |
||
97 | ); |
||
98 | } |
||
99 | return $this->{$name}; |
||
100 | } |
||
101 | |||
102 | abstract public function schema(); |
||
103 | |||
104 | public function toArray() |
||
105 | { |
||
106 | return json_decode(json_encode($this), true); |
||
107 | } |
||
108 | |||
109 | public function setFieldDefaults() |
||
110 | { |
||
111 | $schemaValidator = new SchemaValidator(static::class); |
||
112 | $schemaFields = $schemaValidator->getSchemaFieldsWithDetails($this); |
||
118 | |||
119 | protected function setNestedFieldDefaults($schemaFields) |
||
132 | |||
133 | protected function setRemainingFieldDefaults($defaultFields) |
||
143 | |||
144 | protected function setFieldDefaultValue($fieldName, $value) |
||
152 | |||
153 | public function validate($mode = null) |
||
171 | |||
172 | public function removeExtraneous() |
||
185 | } |
||
186 |