Complex classes like RecordSchema 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 RecordSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class RecordSchema implements SchemaInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var ReflectionEntity |
||
| 29 | */ |
||
| 30 | private $reflection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @invisible |
||
| 34 | * @var MutatorsConfig |
||
| 35 | */ |
||
| 36 | private $mutatorsConfig; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @invisible |
||
| 40 | * @var ColumnRenderer |
||
| 41 | */ |
||
| 42 | private $renderer; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param ReflectionEntity $reflection |
||
| 46 | * @param MutatorsConfig $mutators |
||
| 47 | * @param ColumnRenderer|null $rendered |
||
| 48 | */ |
||
| 49 | public function __construct( |
||
| 50 | ReflectionEntity $reflection, |
||
| 51 | MutatorsConfig $mutators, |
||
| 52 | ColumnRenderer $rendered = null |
||
| 53 | ) { |
||
| 54 | $this->reflection = $reflection; |
||
| 55 | $this->mutatorsConfig = $mutators; |
||
| 56 | $this->renderer = $rendered ?? new ColumnRenderer(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function getClass(): string |
||
| 63 | { |
||
| 64 | return $this->reflection->getName(); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function getRole(): string |
||
| 71 | { |
||
| 72 | $role = $this->reflection->getProperty('model_role'); |
||
| 73 | |||
| 74 | //When role not defined we are going to use short class name |
||
| 75 | return $role ?? lcfirst($this->reflection->getShortName()); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return ReflectionEntity |
||
| 80 | */ |
||
| 81 | public function getReflection(): ReflectionEntity |
||
| 82 | { |
||
| 83 | return $this->reflection; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function getInstantiator(): string |
||
| 90 | { |
||
| 91 | return $this->reflection->getProperty('instantiator') ?? RecordInstantiator::class; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function getDatabase() |
||
| 98 | { |
||
| 99 | $database = $this->reflection->getProperty('database'); |
||
| 100 | if (empty($database)) { |
||
| 101 | //Empty database to be used |
||
| 102 | return null; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $database; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function getTable(): string |
||
| 112 | { |
||
| 113 | $table = $this->reflection->getProperty('table'); |
||
| 114 | if (empty($table)) { |
||
| 115 | //Generate collection using short class name |
||
| 116 | $table = Inflector::camelize($this->reflection->getShortName()); |
||
| 117 | $table = Inflector::pluralize($table); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $table; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Fields and their types declared in Record model. |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function getFields(): array |
||
| 129 | { |
||
| 130 | $fields = $this->reflection->getSchema(); |
||
| 131 | |||
| 132 | foreach ($fields as $field => $type) { |
||
| 133 | if ($this->isRelation($type)) { |
||
| 134 | unset($fields[$field]); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $fields; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Returns set of declared indexes. |
||
| 143 | * |
||
| 144 | * Example: |
||
| 145 | * const INDEXES = [ |
||
| 146 | * [self::UNIQUE, 'email'], |
||
| 147 | * [self::INDEX, 'status', 'balance'], |
||
| 148 | * [self::INDEX, 'public_id'] |
||
| 149 | * ]; |
||
| 150 | * |
||
| 151 | * @do generator |
||
| 152 | * |
||
| 153 | * @return \Generator|IndexDefinition[] |
||
| 154 | * |
||
| 155 | * @throws DefinitionException |
||
| 156 | */ |
||
| 157 | public function getIndexes(): \Generator |
||
| 158 | { |
||
| 159 | $definitions = $this->reflection->getProperty('indexes') ?? []; |
||
| 160 | |||
| 161 | foreach ($definitions as $definition) { |
||
| 162 | yield $this->castIndex($definition); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function declareTable(AbstractTable $table): AbstractTable |
||
| 170 | { |
||
| 171 | return $this->renderer->renderColumns( |
||
| 172 | $this->getFields(), |
||
| 173 | $this->getDefaults(), |
||
| 174 | $table |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function getRelations(): \Generator |
||
| 182 | { |
||
| 183 | $schema = $this->reflection->getSchema(); |
||
| 184 | |||
| 185 | foreach ($schema as $name => $definition) { |
||
| 186 | if (!$this->isRelation($definition)) { |
||
| 187 | continue; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * We expect relations to be defined in a following form: |
||
| 192 | * |
||
| 193 | * [type => target, option => value, option => value] |
||
| 194 | */ |
||
| 195 | $type = key($definition); |
||
| 196 | $target = $definition[$type]; |
||
| 197 | unset($definition[$type]); |
||
| 198 | |||
| 199 | //Defining relation |
||
| 200 | yield new RelationDefinition( |
||
| 201 | $name, |
||
| 202 | $type, |
||
| 203 | $target, |
||
| 204 | $definition, |
||
| 205 | $definition[Record::INVERSE] ?? null |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function packSchema(SchemaBuilder $builder, AbstractTable $table): array |
||
| 214 | { |
||
| 215 | return [ |
||
| 216 | RecordEntity::SH_PRIMARY_KEY => current($table->getPrimaryKeys()), |
||
| 217 | |||
| 218 | //Default entity values |
||
| 219 | RecordEntity::SH_DEFAULTS => $this->packDefaults($table), |
||
| 220 | |||
| 221 | //Entity behaviour |
||
| 222 | RecordEntity::SH_HIDDEN => $this->reflection->getHidden(), |
||
| 223 | RecordEntity::SH_SECURED => $this->reflection->getSecured(), |
||
| 224 | RecordEntity::SH_FILLABLE => $this->reflection->getFillable(), |
||
| 225 | |||
| 226 | //Mutators can be altered based on ORM\SchemasConfig |
||
| 227 | RecordEntity::SH_MUTATORS => $this->buildMutators($table), |
||
| 228 | ]; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Generate set of default values to be used by record. |
||
| 233 | * |
||
| 234 | * @param AbstractTable $table |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function packDefaults(AbstractTable $table): array |
||
| 239 | { |
||
| 240 | //We need mutators to normalize default values |
||
| 241 | $mutators = $this->buildMutators($table); |
||
| 242 | |||
| 243 | $defaults = []; |
||
| 244 | foreach ($table->getColumns() as $column) { |
||
| 245 | $field = $column->getName(); |
||
| 246 | |||
| 247 | $default = $column->getDefaultValue(); |
||
| 248 | |||
| 249 | //For non null values let's apply mutators to typecast it |
||
| 250 | if (!is_null($default) && !is_object($default) && !$column->isNullable()) { |
||
| 251 | $default = $this->mutateValue($mutators, $field, $default); |
||
| 252 | } |
||
| 253 | |||
| 254 | $defaults[$field] = $default; |
||
| 255 | } |
||
| 256 | |||
| 257 | return $defaults; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Generate set of mutators associated with entity fields using user defined and automatic |
||
| 262 | * mutators. |
||
| 263 | * |
||
| 264 | * @see MutatorsConfig |
||
| 265 | * |
||
| 266 | * @param AbstractTable $table |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function buildMutators(AbstractTable $table): array |
||
| 271 | { |
||
| 272 | $mutators = $this->reflection->getMutators(); |
||
| 273 | |||
| 274 | //Trying to resolve mutators based on field type |
||
| 275 | foreach ($table->getColumns() as $column) { |
||
| 276 | //Resolved mutators |
||
| 277 | $resolved = []; |
||
| 278 | |||
| 279 | if (!empty($filter = $this->mutatorsConfig->getMutators($column->abstractType()))) { |
||
| 280 | //Mutator associated with type directly |
||
| 281 | $resolved += $filter; |
||
| 282 | } elseif (!empty($filter = $this->mutatorsConfig->getMutators('php:' . $column->phpType()))) { |
||
| 283 | //Mutator associated with php type |
||
| 284 | $resolved += $filter; |
||
| 285 | } |
||
| 286 | |||
| 287 | //Merging mutators and default mutators |
||
| 288 | foreach ($resolved as $mutator => $filter) { |
||
| 289 | if (!array_key_exists($column->getName(), $mutators[$mutator])) { |
||
| 290 | $mutators[$mutator][$column->getName()] = $filter; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | foreach ($this->getFields() as $field => $type) { |
||
| 296 | if ( |
||
| 297 | class_exists($type) |
||
| 298 | && is_a($type, RecordAccessorInterface::class, true) |
||
| 299 | ) { |
||
| 300 | //Direct column accessor definition |
||
| 301 | $mutators['accessor'][$field] = $type; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | return $mutators; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check if field schema/type defines relation. |
||
| 310 | * |
||
| 311 | * @param mixed $type |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function isRelation($type): bool |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param array $definition |
||
| 326 | * |
||
| 327 | * @return IndexDefinition |
||
| 328 | * |
||
| 329 | * @throws DefinitionException |
||
| 330 | */ |
||
| 331 | protected function castIndex(array $definition) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Default defined values. |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | protected function getDefaults(): array |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Process value thought associated mutator if any. |
||
| 373 | * |
||
| 374 | * @param array $mutators |
||
| 375 | * @param string $field |
||
| 376 | * @param mixed $default |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | protected function mutateValue(array $mutators, string $field, $default) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Pass value thought accessor to ensure it's default. |
||
| 408 | * |
||
| 409 | * @param mixed $default |
||
| 410 | * @param string $accessor |
||
| 411 | * |
||
| 412 | * @return mixed |
||
| 413 | * |
||
| 414 | * @throws AccessException |
||
| 415 | */ |
||
| 416 | protected function accessorDefault($default, string $accessor) |
||
| 431 | } |