Complex classes like Blueprint 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 Blueprint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Blueprint |
||
| 23 | { |
||
| 24 | use Macroable; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The connection that is used by the blueprint. |
||
| 28 | * |
||
| 29 | * @var Connection |
||
| 30 | */ |
||
| 31 | protected $connection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The grammar that is used by the blueprint. |
||
| 35 | * |
||
| 36 | * @var \LaravelFreelancerNL\Aranguent\Schema\Grammars\Grammar |
||
| 37 | */ |
||
| 38 | protected $grammar; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The collection the blueprint describes. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $collection; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The handler for collection manipulation. |
||
| 49 | */ |
||
| 50 | protected $collectionHandler; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The prefix of the collection. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $prefix; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The commands that should be run for the collection. |
||
| 61 | * |
||
| 62 | * @var Fluent[] |
||
| 63 | */ |
||
| 64 | protected $commands = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Catching attributes to be able to add fluent indexes. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $attributes = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Whether to make the collection temporary. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | public $temporary = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Detect if _key (and thus proxy _id) should autoincrement. |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $autoIncrement = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Create a new schema blueprint. |
||
| 89 | * |
||
| 90 | * Blueprint constructor. |
||
| 91 | * @param string $collection |
||
| 92 | * @param \ArangoDBClient\CollectionHandler $collectionHandler |
||
| 93 | * @param Closure|null $callback |
||
| 94 | * @param string $prefix |
||
| 95 | */ |
||
| 96 | public function __construct($collection, $collectionHandler, Closure $callback = null, $prefix = '') |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Execute the blueprint against the database. |
||
| 111 | * |
||
| 112 | * @param Connection $connection |
||
| 113 | * @param Grammar $grammar |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function build(Connection $connection, Grammar $grammar) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Generate the compilation method name and call it if method exists in the Grammar object. |
||
| 132 | * |
||
| 133 | * @param $command |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | public function compileAqlCommand($command) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Generate the execution method name and call it if the method exists. |
||
| 146 | * |
||
| 147 | * @param $command |
||
| 148 | */ |
||
| 149 | public function executeCommand($command) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Execute an AQL statement. |
||
| 162 | * |
||
| 163 | * @param $command |
||
| 164 | */ |
||
| 165 | public function executeAqlCommand($command) |
||
| 169 | |||
| 170 | public function executeCollectionCommand($command) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Drop the index by first getting all the indexes on the collection; then selecting the matching one |
||
| 185 | * by type and attributes. |
||
| 186 | * @param $command |
||
| 187 | * @throws \ArangoDBClient\Exception |
||
| 188 | */ |
||
| 189 | public function executeDropIndexCommand($command) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $command |
||
| 215 | * @throws \ArangoDBClient\Exception |
||
| 216 | */ |
||
| 217 | public function executeIndexCommand($command) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Solely provides feedback to the developer in pretend mode. |
||
| 230 | * |
||
| 231 | * @param $command |
||
| 232 | * @return null |
||
| 233 | */ |
||
| 234 | public function executeIgnoreCommand($command) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Determine if the blueprint has a create command. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | protected function creating() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Indicate that the collection needs to be created. |
||
| 257 | * |
||
| 258 | * @param array $options |
||
| 259 | * @return Fluent |
||
| 260 | */ |
||
| 261 | public function create($options = []) |
||
| 269 | |||
| 270 | public function executeCreateCommand($command) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Indicate that the collection should be dropped. |
||
| 293 | * |
||
| 294 | * @return Fluent |
||
| 295 | */ |
||
| 296 | public function drop() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Indicate that the collection should be dropped if it exists. |
||
| 306 | * |
||
| 307 | * @return Fluent |
||
| 308 | */ |
||
| 309 | public function dropIfExists() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Indicate that the given attribute(s) should be dropped. |
||
| 319 | * |
||
| 320 | * @param array|mixed $attributes |
||
| 321 | * @return Fluent |
||
| 322 | */ |
||
| 323 | public function dropColumn($attributes) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check if any document within the collection has the attribute. |
||
| 336 | * |
||
| 337 | * @param string|array $attribute |
||
| 338 | * @return Fluent |
||
| 339 | */ |
||
| 340 | public function hasAttribute($attribute) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Indicate that the given attributes should be renamed. |
||
| 351 | * |
||
| 352 | * @param string $from |
||
| 353 | * @param string $to |
||
| 354 | * @return Fluent |
||
| 355 | */ |
||
| 356 | public function renameColumn($from, $to) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Indicate that the given index should be dropped. |
||
| 368 | * |
||
| 369 | * @param string|array $attributes |
||
| 370 | * @param string $type |
||
| 371 | * @return Fluent |
||
| 372 | */ |
||
| 373 | public function dropIndex($attributes, $type) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Rename the collection to a given name. |
||
| 385 | * |
||
| 386 | * @param string $to |
||
| 387 | * @return Fluent |
||
| 388 | */ |
||
| 389 | public function rename($to) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Specify an index for the table. |
||
| 396 | * |
||
| 397 | * @param string|array $columns |
||
| 398 | * @param string $name |
||
| 399 | * @param string|null $algorithm |
||
| 400 | * @return Fluent |
||
| 401 | */ |
||
| 402 | public function index($columns = null, $name = null, $algorithm = null) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Create a hash index for fast exact matching. |
||
| 411 | * Hash ! has ;). |
||
| 412 | * |
||
| 413 | * @param null $attributes |
||
| 414 | * @param array $indexOptions |
||
| 415 | * @return Fluent |
||
| 416 | */ |
||
| 417 | public function hashIndex($attributes = null, $indexOptions = []) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param null|string $attribute |
||
| 424 | * @param array $indexOptions |
||
| 425 | * @return Fluent |
||
| 426 | */ |
||
| 427 | public function fulltextIndex($attribute = null, $indexOptions = []) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Specify a spatial index for the collection. |
||
| 434 | * |
||
| 435 | * @param $attributes |
||
| 436 | * @param array $indexOptions |
||
| 437 | * @return Fluent |
||
| 438 | */ |
||
| 439 | public function geoIndex($attributes, $indexOptions = []) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Specify a spatial index for the table. |
||
| 446 | * Alias for geoIndex(). |
||
| 447 | * @param string|array $columns |
||
| 448 | * @param string $name |
||
| 449 | * @return Fluent |
||
| 450 | */ |
||
| 451 | public function spatialIndex($columns, $name = null) |
||
| 455 | |||
| 456 | public function skiplistIndex($attributes, $indexOptions = []) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Create a TTL index for the table. |
||
| 463 | * |
||
| 464 | * @param $attributes |
||
| 465 | * @param array $indexOptions |
||
| 466 | * @return \Illuminate\Support\Fluent |
||
| 467 | */ |
||
| 468 | public function ttlIndex($attributes, $indexOptions = []) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Specify a unique index for the table. |
||
| 475 | * |
||
| 476 | * @param string|array $columns |
||
| 477 | * @param string $name |
||
| 478 | * @param string|null $algorithm |
||
| 479 | * @return Fluent |
||
| 480 | */ |
||
| 481 | public function unique($columns = null, $name = null, $algorithm = null) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Add a new index command to the blueprint. |
||
| 492 | * |
||
| 493 | * @param string $type |
||
| 494 | * @param string|array $attributes |
||
| 495 | * @param array $indexOptions |
||
| 496 | * @return Fluent |
||
| 497 | */ |
||
| 498 | protected function indexCommand($type = '', $attributes = [], $indexOptions = []) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Add a new command to the blueprint. |
||
| 523 | * |
||
| 524 | * @param string $name |
||
| 525 | * @param array $parameters |
||
| 526 | * @return Fluent |
||
| 527 | */ |
||
| 528 | protected function addCommand($name, array $parameters = []) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Create a new Fluent command. |
||
| 537 | * |
||
| 538 | * @param string $name |
||
| 539 | * @param array $parameters |
||
| 540 | * @return Fluent |
||
| 541 | */ |
||
| 542 | protected function createCommand($name, array $parameters = []) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get the collection the blueprint describes. |
||
| 549 | * |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | public function getCollection() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Alias for getCollection. |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public function getTable() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Get the commands on the blueprint. |
||
| 569 | * |
||
| 570 | * @return Fluent[] |
||
| 571 | */ |
||
| 572 | public function getCommands() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Silently catch unsupported schema methods. Store attributes for backwards compatible fluent index creation. |
||
| 579 | * |
||
| 580 | * @param $method |
||
| 581 | * @param $args |
||
| 582 | * @return Blueprint |
||
| 583 | */ |
||
| 584 | public function __call($method, $args) |
||
| 615 | |||
| 616 | public function mapIndexType($algorithm) |
||
| 628 | } |
||
| 629 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.