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 |
||
9 | class Schema |
||
10 | { |
||
11 | protected $DEFAULT_FIELD_CLASS = '\\frictionlessdata\\tableschema\\Fields\\StringField'; |
||
12 | |||
13 | /** |
||
14 | * Schema constructor. |
||
15 | * |
||
16 | * @param mixed $descriptor |
||
17 | * |
||
18 | * @throws Exceptions\SchemaLoadException |
||
19 | * @throws Exceptions\SchemaValidationFailedException |
||
20 | */ |
||
21 | public function __construct($descriptor) |
||
57 | |||
58 | /** |
||
59 | * loads and validates the given descriptor source (php object / string / path to file / url) |
||
60 | * returns an array of validation error objects. |
||
61 | * |
||
62 | * @param mixed $descriptor |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public static function validate($descriptor) |
||
80 | |||
81 | /** |
||
82 | * @return object |
||
83 | */ |
||
84 | public function descriptor() |
||
88 | |||
89 | public function fullDescriptor() |
||
101 | |||
102 | public function field($name) |
||
111 | |||
112 | /** |
||
113 | * @return Fields\BaseField[] array of field name => field object |
||
114 | */ |
||
115 | public function fields() |
||
130 | |||
131 | public function missingValues() |
||
135 | |||
136 | public function primaryKey() |
||
142 | |||
143 | public function foreignKeys() |
||
157 | |||
158 | /** |
||
159 | * @param mixed[] $row |
||
160 | * |
||
161 | * @return mixed[] |
||
162 | * |
||
163 | * @throws Exceptions\FieldValidationException |
||
164 | */ |
||
165 | public function castRow($row) |
||
186 | |||
187 | /** |
||
188 | * @param array $row |
||
189 | * |
||
190 | * @return SchemaValidationError[] |
||
191 | */ |
||
192 | public function validateRow($row) |
||
202 | |||
203 | public function save($filename) |
||
207 | |||
208 | protected $descriptor; |
||
209 | protected $fieldsCache = null; |
||
210 | } |
||
211 |