Complex classes like CRUDContext 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 CRUDContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class CRUDContext extends PageObjectContext implements KernelAwareContext |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var KernelInterface |
||
| 27 | */ |
||
| 28 | protected $kernel; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \FSi\Component\DataGrid\DataGrid[] |
||
| 32 | */ |
||
| 33 | protected $datagrids; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \FSi\Component\DataSource\DataSource[] |
||
| 37 | */ |
||
| 38 | protected $datasources; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $newsTitle; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $subscriberEmail; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $invalidEmail = 'not_a_valid_email#email.'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param KernelInterface $kernel |
||
| 57 | */ |
||
| 58 | public function setKernel(KernelInterface $kernel) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @Then /^I should see list with following columns$/ |
||
| 67 | */ |
||
| 68 | public function iShouldSeeListWithFollowingColumns(TableNode $table) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @Given /^("[^"]*" element) datasource max results is set (\d+)$/ |
||
| 84 | */ |
||
| 85 | public function elementDatasourceMaxResultsIsSet(ListElement $adminElement, $maxResults) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Then /^I should see pagination with following buttons$/ |
||
| 94 | */ |
||
| 95 | public function iShouldSeePaginationWithFollowingButtons(TableNode $table) |
||
| 96 | { |
||
| 97 | $pagination = $this->getElement('Pagination'); |
||
| 98 | |||
| 99 | foreach ($table->getHash() as $buttonRow) { |
||
| 100 | expect($pagination->hasLink($buttonRow['Button']))->toBe(true); |
||
| 101 | |||
| 102 | if ($buttonRow['Active'] === 'true') { |
||
| 103 | expect($pagination->isDisabled($buttonRow['Button']))->toBe(false); |
||
| 104 | } else { |
||
| 105 | expect($pagination->isDisabled($buttonRow['Button']))->toBe(true); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($buttonRow['Current'] === 'true') { |
||
| 109 | expect($pagination->isCurrentPage($buttonRow['Button']))->toBe(true); |
||
| 110 | } else { |
||
| 111 | expect($pagination->isCurrentPage($buttonRow['Button']))->toBe(false); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @Then /^both sorting buttons in column header "([^"]*)" should be active$/ |
||
| 118 | */ |
||
| 119 | public function bothSortingButtonsInColumnHeaderShouldBeActive($column) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @When /^I press "([^"]*)" button in "([^"]*)" column header$/ |
||
| 126 | */ |
||
| 127 | public function iPressButtonInColumnHeader($sort, $column) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @Then /^"([^"]*)" button in "([^"]*)" column header should be disabled$/ |
||
| 134 | */ |
||
| 135 | public function buttonInColumnHeaderShouldBeDisabled($sort, $column) |
||
| 136 | { |
||
| 137 | $list = $this->getElement('Elements List'); |
||
| 138 | |||
| 139 | switch (strtolower($sort)) { |
||
| 140 | case 'sort asc': |
||
| 141 | expect($list->isColumnAscSortActive($column))->toBe(false); |
||
| 142 | break; |
||
| 143 | case 'sort desc': |
||
| 144 | expect($list->isColumnDescSortActive($column))->toBe(false); |
||
| 145 | break; |
||
| 146 | default : |
||
|
|
|||
| 147 | throw new \Exception(sprintf("Unknown sorting type %s", $sort)); |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Given /^"([^"]*)" button in "([^"]*)" column header should be active$/ |
||
| 154 | */ |
||
| 155 | public function buttonInColumnHeaderShouldBeActive($sort, $column) |
||
| 156 | { |
||
| 157 | $list = $this->getElement('Elements List'); |
||
| 158 | |||
| 159 | switch (strtolower($sort)) { |
||
| 160 | case 'sort asc': |
||
| 161 | expect($list->isColumnAscSortActive($column))->toBe(true); |
||
| 162 | break; |
||
| 163 | case 'sort desc': |
||
| 164 | expect($list->isColumnDescSortActive($column))->toBe(true); |
||
| 165 | break; |
||
| 166 | default : |
||
| 167 | throw new \Exception(sprintf("Unknown sorting type %s", $sort)); |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @Then /^I should not see pagination$/ |
||
| 174 | */ |
||
| 175 | public function iShouldNotSeePagination() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @When /^I press "([^"]*)" button at pagination$/ |
||
| 182 | */ |
||
| 183 | public function iPressButtonAtPagination($button) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @Then /^there should be (\d+) elements at list$/ |
||
| 190 | */ |
||
| 191 | public function thereShouldBeElementsAtList($elemetsCount) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @When /^I change elements per page to (\d+)$/ |
||
| 198 | */ |
||
| 199 | public function iChangeElementsPerPageTo($elemetsCount) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @Then /^I should not see any filters$/ |
||
| 206 | */ |
||
| 207 | public function iShouldNotSeeAnyFilters() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @Then /^I should see simple text filter "([^"]*)"$/ |
||
| 214 | */ |
||
| 215 | public function iShouldSeeSimpleTextFilter($filterName) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @Given /^I should see between filter "([^"]*)" with "([^"]*)" and "([^"]*)" simple text fields$/ |
||
| 222 | */ |
||
| 223 | public function iShouldSeeBetweenFilterWithAndSimpleTextFields($filterName, $fromName, $toName) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Given /^I should see choice filter "([^"]*)"$/ |
||
| 230 | */ |
||
| 231 | public function iShouldSeeChoiceFilter($filterName) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @Given /^I fill simple text filter "([^"]*)" with value "([^"]*)"$/ |
||
| 238 | */ |
||
| 239 | public function iFillSimpleTextFilterWithValue($filterName, $filterValue) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @When /^I select "([^"]*)" in choice filter "([^"]*)"$/ |
||
| 246 | */ |
||
| 247 | public function iSelectInChoiceFilter($filterValue, $filterName) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @Given /^I press "Search" button$/ |
||
| 254 | */ |
||
| 255 | public function iPressSearchButton() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Given /^I press "New element" link$/ |
||
| 262 | */ |
||
| 263 | public function iPressNewElementLink() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Then /^I should see filtered list$/ |
||
| 270 | */ |
||
| 271 | public function iShouldSeeFilteredList() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Given /^simple text filter "([^"]*)" should be filled with value "([^"]*)"$/ |
||
| 278 | */ |
||
| 279 | public function simpleTextFilterShouldBeFilledWithValue($filterName, $filterValue) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @Given /^choice filter "([^"]*)" should have value "([^"]*)" selected$/ |
||
| 286 | */ |
||
| 287 | public function choiceFilterShouldHaveValueSelected($filterName, $choice) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Given /^I should see form with following fields$/ |
||
| 296 | */ |
||
| 297 | public function iShouldSeeFormWithFollowingFields(TableNode $table) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @When /^I fill all form field properly$/ |
||
| 307 | */ |
||
| 308 | public function iFillAllFormFieldProperly() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Given /^I press form "([^"]*)" button$/ |
||
| 338 | */ |
||
| 339 | public function iPressFormButton($button) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Given /^I should be redirected to "([^"]*)" page$/ |
||
| 346 | */ |
||
| 347 | public function iShouldBeRedirectedToPage($pageName) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @Given /^I press "([^"]*)" link in "([^"]*)" column of first element at list$/ |
||
| 354 | */ |
||
| 355 | public function iPressLinkInColumnOfFirstElementAtList($link, $columnName) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @When /^I change form "([^"]*)" field value$/ |
||
| 362 | */ |
||
| 363 | public function iChangeFormTitleFieldValue($field) |
||
| 364 | { |
||
| 365 | $generator = Factory::create(); |
||
| 366 | switch ($field) { |
||
| 367 | case 'Title': |
||
| 368 | $this->newsTitle = $generator->text(); |
||
| 369 | expect($this->newsTitle)->toNotBe($this->getElement('Form')->findField($field)->getValue()); |
||
| 370 | $this->getElement('Form')->fillField($field, $this->newsTitle); |
||
| 371 | break; |
||
| 372 | case 'Email': |
||
| 373 | $this->subscriberEmail = $generator->email(); |
||
| 374 | expect($this->subscriberEmail)->toNotBe($this->getElement('Form')->findField($field)->getValue()); |
||
| 375 | $this->getElement('Form')->fillField($field, $this->subscriberEmail); |
||
| 376 | break; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @When /^I fill the "([^"]*)" field with invalid data$/ |
||
| 382 | */ |
||
| 383 | public function iFillFormValueWithInvalidData($field) |
||
| 384 | { |
||
| 385 | switch ($field) { |
||
| 386 | case 'Email': |
||
| 387 | expect($this->invalidEmail)->toNotBe($this->getElement('Form')->findField($field)->getValue()); |
||
| 388 | $this->getElement('Form')->fillField($field, $this->invalidEmail); |
||
| 389 | break; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @Then /^news with id (\d+) should have changed title$/ |
||
| 395 | */ |
||
| 396 | public function newsWithIdShouldHaveChangedTitle($id) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @Then /^subscriber with id (\d+) should have changed email$/ |
||
| 403 | */ |
||
| 404 | public function subscriberWithIdShouldHaveChangedEmail($id) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @Then /^subscriber with id (\d+) should not have his email changed to invalid one$/ |
||
| 411 | */ |
||
| 412 | public function subscriberWithIdShouldNotHaveChangedEmailToInvalid($id) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @Then /^I should see customized "([^"]*)" view$/ |
||
| 428 | */ |
||
| 429 | public function iShouldSeeCustomizedView($crudElement) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @Then /^I should see actions dropdown with following options$/ |
||
| 446 | */ |
||
| 447 | public function iShouldSeeActionsDropdownWithFollowingOptions(TableNode $actions) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @Given /^I should see confirmation button "([^"]*)"$/ |
||
| 458 | */ |
||
| 459 | public function iShouldSeeConfirmationButton($button) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @When /^I press checkbox in first column in first row$/ |
||
| 466 | */ |
||
| 467 | public function iPressCheckboxInFirstColumnInFirstRow() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @Given /^I choose action "([^"]*)" from actions$/ |
||
| 474 | */ |
||
| 475 | public function iChooseActionFromActions($action) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Given /^I press confirmation button "Ok"$/ |
||
| 482 | */ |
||
| 483 | public function iPressConfirmationButton() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @Then /^I should be redirected to confirmation page with message$/ |
||
| 490 | */ |
||
| 491 | public function iShouldBeRedirectedToConfirmationPageWithMessage(PyStringNode $message) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @When /^I press "Yes"$/ |
||
| 499 | */ |
||
| 500 | public function iPress() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @When /^I press checkbox in first column header$/ |
||
| 507 | */ |
||
| 508 | public function iPressCheckboxInFirstColumnHeader() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @Given /^"([^"]*)" column is editable$/ |
||
| 515 | */ |
||
| 516 | public function columnIsEditable($columnHeader) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @Given /^I clicked "([^"]*)" in "([^"]*)" column in first row$/ |
||
| 523 | * @When /^I click "([^"]*)" in "([^"]*)" column in first row$/ |
||
| 524 | */ |
||
| 525 | public function iClickInColumnInFirstRow($link, $columnHeader) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @Given /^I clicked edit in "([^"]*)" column in first row$/ |
||
| 532 | * @When /^I click edit in "([^"]*)" column in first row$/ |
||
| 533 | */ |
||
| 534 | public function iClickEditInColumnInFirstRow($columnHeader) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @Then /^popover with "([^"]*)" field in form should appear$/ |
||
| 543 | */ |
||
| 544 | public function popoverWithFieldInFormShouldAppear($newsTitle) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @Then /^popover with empty date field in form should appear$/ |
||
| 553 | */ |
||
| 554 | public function popoverWithEmptyDateFieldInFormShouldAppear() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @When /^I click X at popover$/ |
||
| 563 | */ |
||
| 564 | public function iClickXAtPopover() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @Then /^popover should not be visible anymore$/ |
||
| 578 | */ |
||
| 579 | public function popoverShouldNotBeVisibleAnymore() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @When /^I fill "([^"]*)" field at popover with "([^"]*)" value$/ |
||
| 586 | */ |
||
| 587 | public function iFillFieldAtPopoverWithValue($field, $value) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @Given /^I press "([^"]*)" at popover$/ |
||
| 594 | */ |
||
| 595 | public function iPressAtPopover($button) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @Then /^"([^"]*)" news title should be changed to "([^"]*)"$/ |
||
| 602 | */ |
||
| 603 | public function newsTitleShouldBeChangedTo($arg1, $arg2) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param \FSi\Bundle\AdminBundle\Admin\CRUD\ListElement $adminElement |
||
| 610 | * @return \FSi\Component\DataGrid\DataGrid |
||
| 611 | */ |
||
| 612 | protected function getDataGrid(ListElement $adminElement) |
||
| 613 | { |
||
| 614 | if (!array_key_exists($adminElement->getId(), $this->datagrids)) { |
||
| 615 | $this->datagrids[$adminElement->getId()] = $adminElement->createDataGrid(); |
||
| 616 | } |
||
| 617 | |||
| 618 | return $this->datagrids[$adminElement->getId()]; |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param \FSi\Bundle\AdminBundle\Admin\CRUD\ListElement $adminElement |
||
| 623 | * @return \FSi\Component\DataSource\DataSource |
||
| 624 | */ |
||
| 625 | protected function getDataSource(ListElement $adminElement) |
||
| 626 | { |
||
| 627 | if (!array_key_exists($adminElement->getId(), $this->datasources)) { |
||
| 628 | $this->datasources[$adminElement->getId()] = $adminElement->createDataSource(); |
||
| 629 | } |
||
| 630 | |||
| 631 | return $this->datasources[$adminElement->getId()]; |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param $optionValue |
||
| 636 | * @return mixed |
||
| 637 | */ |
||
| 638 | protected function prepareColumnOptionValue($optionValue) |
||
| 646 | } |
||
| 647 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.