Complex classes like DataObjectSchema 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 DataObjectSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DataObjectSchema { |
||
| 19 | |||
| 20 | use Injectable; |
||
| 21 | use Configurable; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Default separate for table namespaces. Can be set to any string for |
||
| 25 | * databases that do not support some characters. |
||
| 26 | * |
||
| 27 | * Defaults to \ to to conform to 3.x convention. |
||
| 28 | * |
||
| 29 | * @config |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private static $table_namespace_separator = '\\'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Cache of database fields |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $databaseFields = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Cache of composite database field |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $compositeFields = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Cache of table names |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $tableNames = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Clear cached table names |
||
| 57 | */ |
||
| 58 | public function reset() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get all table names |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function getTableNames() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Given a DataObject class and a field on that class, determine the appropriate SQL for |
||
| 76 | * selecting / filtering on in a SQL string. Note that $class must be a valid class, not an |
||
| 77 | * arbitrary table. |
||
| 78 | * |
||
| 79 | * The result will be a standard ANSI-sql quoted string in "Table"."Column" format. |
||
| 80 | * |
||
| 81 | * @param string $class Class name (not a table). |
||
| 82 | * @param string $field Name of field that belongs to this class (or a parent class) |
||
| 83 | * @return string The SQL identifier string for the corresponding column for this field |
||
| 84 | */ |
||
| 85 | public function sqlColumnForField($class, $field) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get table name for the given class. |
||
| 95 | * |
||
| 96 | * Note that this does not confirm a table actually exists (or should exist), but returns |
||
| 97 | * the name that would be used if this table did exist. |
||
| 98 | * |
||
| 99 | * @param string $class |
||
| 100 | * @return string Returns the table name, or null if there is no table |
||
| 101 | */ |
||
| 102 | public function tableName($class) { |
||
| 110 | /** |
||
| 111 | * Returns the root class (the first to extend from DataObject) for the |
||
| 112 | * passed class. |
||
| 113 | * |
||
| 114 | * @param string|object $class |
||
| 115 | * @return string |
||
| 116 | * @throws InvalidArgumentException |
||
| 117 | */ |
||
| 118 | public function baseDataClass($class) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the base table |
||
| 132 | * |
||
| 133 | * @param string|object $class |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function baseDataTable($class) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * fieldSpec should exclude virtual fields (such as composite fields), and only include fields with a db column. |
||
| 142 | */ |
||
| 143 | const DB_ONLY = 1; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * fieldSpec should only return fields that belong to this table, and not any ancestors |
||
| 147 | */ |
||
| 148 | const UNINHERITED = 2; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * fieldSpec should prefix all field specifications with the class name in RecordClass.Column(spec) format. |
||
| 152 | */ |
||
| 153 | const INCLUDE_CLASS = 4; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get all DB field specifications for a class, including ancestors and composite fields. |
||
| 157 | * |
||
| 158 | * @param string|DataObject $classOrInstance |
||
| 159 | * @param int $options Bitmask of options |
||
| 160 | * - UNINHERITED Limit to only this table |
||
| 161 | * - DB_ONLY Exclude virtual fields (such as composite fields), and only include fields with a db column. |
||
| 162 | * - INCLUDE_CLASS Prefix the field specification with the class name in RecordClass.Column(spec) format. |
||
| 163 | * @return array List of fields, where the key is the field name and the value is the field specification. |
||
| 164 | */ |
||
| 165 | public function fieldSpecs($classOrInstance, $options = 0) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Get specifications for a single class field |
||
| 201 | * |
||
| 202 | * @param string|DataObject $classOrInstance Name or instance of class |
||
| 203 | * @param string $fieldName Name of field to retrieve |
||
| 204 | * @param int $options Bitmask of options |
||
| 205 | * - UNINHERITED Limit to only this table |
||
| 206 | * - DB_ONLY Exclude virtual fields (such as composite fields), and only include fields with a db column. |
||
| 207 | * - INCLUDE_CLASS Prefix the field specification with the class name in RecordClass.Column(spec) format. |
||
| 208 | * @return string|null Field will be a string in FieldClass(args) format, or |
||
| 209 | * RecordClass.FieldClass(args) format if using INCLUDE_CLASS. Will be null if no field is found. |
||
| 210 | */ |
||
| 211 | public function fieldSpec($classOrInstance, $fieldName, $options = 0) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Find the class for the given table |
||
| 218 | * |
||
| 219 | * @param string $table |
||
| 220 | * @return string|null The FQN of the class, or null if not found |
||
| 221 | */ |
||
| 222 | public function tableClass($table) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Cache all table names if necessary |
||
| 243 | */ |
||
| 244 | protected function cacheTableNames() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Generate table name for a class. |
||
| 268 | * |
||
| 269 | * Note: some DB schema have a hard limit on table name length. This is not enforced by this method. |
||
| 270 | * See dev/build errors for details in case of table name violation. |
||
| 271 | * |
||
| 272 | * @param string $class |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | protected function buildTableName($class) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
| 289 | * "ID" will be included on every table. |
||
| 290 | * |
||
| 291 | * @param string $class Class name to query from |
||
| 292 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 293 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 294 | */ |
||
| 295 | public function databaseFields($class, $aggregated = true) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Gets a single database field. |
||
| 314 | * |
||
| 315 | * @param string $class Class name to query from |
||
| 316 | * @param string $field Field name |
||
| 317 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 318 | * @return string|null Field specification, or null if not a field |
||
| 319 | */ |
||
| 320 | public function databaseField($class, $field, $aggregated = true) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check if the given class has a table |
||
| 327 | * |
||
| 328 | * @param string $class |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function classHasTable($class) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 338 | * Will check all applicable ancestor classes and aggregate results. |
||
| 339 | * |
||
| 340 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
| 341 | * to aggregate. |
||
| 342 | * |
||
| 343 | * Includes composite has_one (Polymorphic) fields |
||
| 344 | * |
||
| 345 | * @param string $class Name of class to check |
||
| 346 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 347 | * @return array List of composite fields and their class spec |
||
| 348 | */ |
||
| 349 | public function compositeFields($class, $aggregated = true) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get a composite field for a class |
||
| 369 | * |
||
| 370 | * @param string $class Class name to query from |
||
| 371 | * @param string $field Field name |
||
| 372 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 373 | * @return string|null Field specification, or null if not a field |
||
| 374 | */ |
||
| 375 | public function compositeField($class, $field, $aggregated = true) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Cache all database and composite fields for the given class. |
||
| 382 | * Will do nothing if already cached |
||
| 383 | * |
||
| 384 | * @param string $class Class name to cache |
||
| 385 | */ |
||
| 386 | protected function cacheDatabaseFields($class) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Returns the table name in the class hierarchy which contains a given |
||
| 446 | * field column for a {@link DataObject}. If the field does not exist, this |
||
| 447 | * will return null. |
||
| 448 | * |
||
| 449 | * @param string $candidateClass |
||
| 450 | * @param string $fieldName |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function tableForField($candidateClass, $fieldName) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Returns the class name in the class hierarchy which contains a given |
||
| 463 | * field column for a {@link DataObject}. If the field does not exist, this |
||
| 464 | * will return null. |
||
| 465 | * |
||
| 466 | * @param string $candidateClass |
||
| 467 | * @param string $fieldName |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function classForField($candidateClass, $fieldName) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Return information about a specific many_many component. Returns a numeric array. |
||
| 496 | * The first item in the array will be the class name of the relation. |
||
| 497 | * |
||
| 498 | * Standard many_many return type is: |
||
| 499 | * |
||
| 500 | * array( |
||
| 501 | * <manyManyClass>, Name of class for relation. E.g. "Categories" |
||
| 502 | * <classname>, The class that relation is defined in e.g. "Product" |
||
| 503 | * <candidateName>, The target class of the relation e.g. "Category" |
||
| 504 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID". |
||
| 505 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID". |
||
| 506 | * <joinTableOrRelation> The join table between the two classes e.g. "Product_Categories". |
||
| 507 | * If the class name is 'ManyManyThroughList' then this is the name of the |
||
| 508 | * has_many relation. |
||
| 509 | * ) |
||
| 510 | * @param string $class Name of class to get component for |
||
| 511 | * @param string $component The component name |
||
| 512 | * @return array|null |
||
| 513 | */ |
||
| 514 | public function manyManyComponent($class, $component) { |
||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Parse a belongs_many_many component to extract class and relationship name |
||
| 545 | * |
||
| 546 | * @param string $parentClass Name of class |
||
| 547 | * @param string $component Name of relation on class |
||
| 548 | * @param string $specification specification for this belongs_many_many |
||
| 549 | * @return array Array with child class and relation name |
||
| 550 | */ |
||
| 551 | protected function parseBelongsManyManyComponent($parentClass, $component, $specification) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Return the many-to-many extra fields specification for a specific component. |
||
| 578 | * |
||
| 579 | * @param string $class |
||
| 580 | * @param string $component |
||
| 581 | * @return array|null |
||
| 582 | */ |
||
| 583 | public function manyManyExtraFieldsForComponent($class, $component) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Return data for a specific has_many component. |
||
| 606 | * |
||
| 607 | * @param string $class Parent class |
||
| 608 | * @param string $component |
||
| 609 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form |
||
| 610 | * "ClassName.Field" will have the field data stripped off. It defaults to TRUE. |
||
| 611 | * @return string|null |
||
| 612 | */ |
||
| 613 | public function hasManyComponent($class, $component, $classOnly = true) { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Return data for a specific has_one component. |
||
| 630 | * |
||
| 631 | * @param string $class |
||
| 632 | * @param string $component |
||
| 633 | * @return string|null |
||
| 634 | */ |
||
| 635 | public function hasOneComponent($class, $component) { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Return data for a specific belongs_to component. |
||
| 649 | * |
||
| 650 | * @param string $class |
||
| 651 | * @param string $component |
||
| 652 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the |
||
| 653 | * form "ClassName.Field" will have the field data stripped off. It defaults to TRUE. |
||
| 654 | * @return string|null |
||
| 655 | */ |
||
| 656 | public function belongsToComponent($class, $component, $classOnly = true) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * |
||
| 673 | * @param string $parentClass Parent class name |
||
| 674 | * @param string $component ManyMany name |
||
| 675 | * @param string|array $specification Declaration of many_many relation type |
||
| 676 | * @return array |
||
| 677 | */ |
||
| 678 | protected function parseManyManyComponent($parentClass, $component, $specification) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Find a many_many on the child class that points back to this many_many |
||
| 721 | * |
||
| 722 | * @param string $childClass |
||
| 723 | * @param string $parentClass |
||
| 724 | * @return string|null |
||
| 725 | */ |
||
| 726 | protected function getManyManyInverseRelationship($childClass, $parentClass) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Tries to find the database key on another object that is used to store a |
||
| 742 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
| 743 | * |
||
| 744 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
| 745 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
| 746 | * |
||
| 747 | * @param string $class |
||
| 748 | * @param string $component Name of the relation on the current object pointing to the |
||
| 749 | * remote object. |
||
| 750 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
| 751 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
| 752 | * @return string |
||
| 753 | * @throws Exception |
||
| 754 | */ |
||
| 755 | public function getRemoteJoinField($class, $component, $type = 'has_many', &$polymorphic = false) { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Validate the to or from field on a has_many mapping class |
||
| 826 | * |
||
| 827 | * @param string $parentClass Name of parent class |
||
| 828 | * @param string $component Name of many_many component |
||
| 829 | * @param string $joinClass Class for the joined table |
||
| 830 | * @param array $specification Complete many_many specification |
||
| 831 | * @param string $key Name of key to check ('from' or 'to') |
||
| 832 | * @return string Class that matches the given relation |
||
| 833 | * @throws InvalidArgumentException |
||
| 834 | */ |
||
| 835 | protected function checkManyManyFieldClass($parentClass, $component, $joinClass, $specification, $key) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param string $parentClass Name of parent class |
||
| 885 | * @param string $component Name of many_many component |
||
| 886 | * @param array $specification Complete many_many specification |
||
| 887 | * @return string Name of join class |
||
| 888 | */ |
||
| 889 | protected function checkManyManyJoinClass($parentClass, $component, $specification) |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Validate a given class is valid for a relation |
||
| 908 | * |
||
| 909 | * @param string $class Parent class |
||
| 910 | * @param string $component Component name |
||
| 911 | * @param string $relationClass Candidate class to check |
||
| 912 | * @param string $type Relation type (e.g. has_one) |
||
| 913 | */ |
||
| 914 | protected function checkRelationClass($class, $component, $relationClass, $type) |
||
| 944 | } |
||
| 945 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: