Complex classes like EntityWithDBProperties 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 EntityWithDBProperties, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class EntityWithDBProperties extends \core\common\Entity { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This variable gets initialised with the known IdP attributes in the constructor. It never gets updated until the object |
||
| 39 | * is destroyed. So if attributes change in the database, and IdP attributes are to be queried afterwards, the object |
||
| 40 | * needs to be re-instantiated to have current values in this variable. |
||
| 41 | * |
||
| 42 | * @var array of entity's attributes |
||
| 43 | */ |
||
| 44 | protected $attributes; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The database to query for attributes regarding this entity |
||
| 48 | * |
||
| 49 | * @var string DB type |
||
| 50 | */ |
||
| 51 | protected $databaseType; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * This variable contains the name of the table that stores the entity's options |
||
| 55 | * |
||
| 56 | * @var string DB table name |
||
| 57 | */ |
||
| 58 | protected $entityOptionTable; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * column name to find entity in that table |
||
| 62 | * |
||
| 63 | * @var string DB column name of entity |
||
| 64 | */ |
||
| 65 | protected $entityIdColumn; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * We need database access. Be sure to instantiate the singleton, and then |
||
| 69 | * use its instance (rather than always accessing everything statically) |
||
| 70 | * |
||
| 71 | * @var DBConnection the instance of the default database we talk to usually |
||
| 72 | */ |
||
| 73 | protected $databaseHandle; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * the unique identifier of this entity instance |
||
| 77 | * refers to the integer row name in the DB -> int; Federation has no own |
||
| 78 | * DB, so the identifier is of no use there -> use Fedearation->$tld |
||
| 79 | * |
||
| 80 | * @var int identifier of the entity instance |
||
| 81 | */ |
||
| 82 | public $identifier; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * the name of the entity in the current locale |
||
| 86 | */ |
||
| 87 | public $name; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The constructor initialises the entity. Since it has DB properties, |
||
| 91 | * this means the DB connection is set up for it. |
||
| 92 | */ |
||
| 93 | public function __construct() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * How is the object identified in the database? |
||
| 103 | * @return string|int |
||
| 104 | * @throws Exception |
||
| 105 | */ |
||
| 106 | private function getRelevantIdentifier() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * This function retrieves the entity's attributes. |
||
| 123 | * |
||
| 124 | * If called with the optional parameter, only attribute values for the attribute |
||
| 125 | * name in $optionName are retrieved; otherwise, all attributes are retrieved. |
||
| 126 | * The retrieval is in-memory from the internal attributes class member - no |
||
| 127 | * DB callback, so changes in the database during the class instance lifetime |
||
| 128 | * are not considered. |
||
| 129 | * |
||
| 130 | * @param string $optionName optionally, the name of the attribute that is to be retrieved |
||
| 131 | * @return array of arrays of attributes which were set for this IdP |
||
| 132 | */ |
||
| 133 | public function getAttributes(string $optionName = NULL) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * deletes all attributes in this profile except the _file ones, these are reported as array |
||
| 148 | * |
||
| 149 | * @return array list of row id's of file-based attributes which weren't deleted |
||
| 150 | */ |
||
| 151 | public function beginFlushAttributes() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * after a beginFlushAttributes, deletes all attributes which are in the tobedeleted array. |
||
| 166 | * |
||
| 167 | * @param array $tobedeleted array of database rows which are to be deleted |
||
| 168 | */ |
||
| 169 | public function commitFlushAttributes(array $tobedeleted) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * deletes all attributes of this entity from the database |
||
| 179 | */ |
||
| 180 | public function flushAttributes() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Adds an attribute for the entity instance into the database. Multiple instances of the same attribute are supported. |
||
| 186 | * |
||
| 187 | * @param string $attrName Name of the attribute. This must be a well-known value from the profile_option_dict table in the DB. |
||
| 188 | * @param string $attrLang language of the attribute. Can be NULL. |
||
| 189 | * @param mixed $attrValue Value of the attribute. Can be anything; will be stored in the DB as-is. |
||
| 190 | */ |
||
| 191 | public function addAttribute($attrName, $attrLang, $attrValue) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * retrieve attributes from a database. Only does SELECT queries. |
||
| 200 | * @param string $query sub-classes set the query to execute to get to the options |
||
| 201 | * @param string $level the retrieved options get flagged with this "level" identifier |
||
| 202 | * @return array the attributes in one array |
||
| 203 | */ |
||
| 204 | protected function retrieveOptionsFromDatabase($query, $level) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Retrieves data from the underlying tables, for situations where instantiating the IdP or Profile object is inappropriate |
||
| 231 | * |
||
| 232 | * @param string $table institution_option or profile_option |
||
| 233 | * @param string $row rowindex |
||
| 234 | * @return string|FALSE the data, or FALSE if something went wrong |
||
| 235 | */ |
||
| 236 | public static function fetchRawDataByIndex($table, $row) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Checks if a raw data pointer is public data (return value FALSE) or if |
||
| 259 | * yes who the authorised admins to view it are (return array of user IDs) |
||
| 260 | * |
||
| 261 | * @param string $table which database table is this about |
||
| 262 | * @param int $row row index of the table |
||
| 263 | * @return mixed FALSE if the data is public, an array of owners of the data if it is NOT public |
||
| 264 | */ |
||
| 265 | public static function isDataRestricted($table, $row) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * when options in the DB change, this can mean generated installers become stale. sub-classes must define whether this is the case for them |
||
| 315 | */ |
||
| 316 | abstract public function updateFreshness(); |
||
| 317 | } |
||
| 318 |