Complex classes like StoryTeller 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 StoryTeller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 151 | class StoryTeller |
||
| 152 | { |
||
| 153 | /** |
||
| 154 | * singleton instance of StoryTeller |
||
| 155 | * @var StoryTeller |
||
| 156 | */ |
||
| 157 | static protected $self = null; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * the story that is being played |
||
| 161 | * @var Story |
||
| 162 | */ |
||
| 163 | private $story = null; |
||
| 164 | |||
| 165 | private $pageContext = null; |
||
| 166 | private $checkpoint = null; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * the script that is being played |
||
| 170 | */ |
||
| 171 | private $scriptFilename = null; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * |
||
| 175 | * @var PhaseLoader |
||
| 176 | */ |
||
| 177 | private $phaseLoader = null; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * |
||
| 181 | * @var Prose_Loader |
||
| 182 | */ |
||
| 183 | private $proseLoader = null; |
||
| 184 | |||
| 185 | // support for the current runtime config |
||
| 186 | private $runtimeConfig = null; |
||
| 187 | private $runtimeConfigManager = null; |
||
| 188 | |||
| 189 | // our output |
||
| 190 | private $output = null; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * |
||
| 194 | * @var \Datasift\Storyplayer\PlayerLib\Action_Logger |
||
| 195 | */ |
||
| 196 | private $actionLogger; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * which of the steps is currently being executed? |
||
| 200 | * @var \DataSift\Storyplayer\Phases\Phase |
||
| 201 | */ |
||
| 202 | private $currentPhase = null; |
||
| 203 | |||
| 204 | // test device support |
||
| 205 | private $deviceDetails = null; |
||
| 206 | private $deviceName = null; |
||
| 207 | private $deviceAdapter = null; |
||
| 208 | private $persistDevice = false; |
||
| 209 | |||
| 210 | // the config that Storyplayer is running with |
||
| 211 | private $config = null; |
||
| 212 | |||
| 213 | // the environment we are testing |
||
| 214 | private $persistTestEnvironment = false; |
||
| 215 | |||
| 216 | // story / template params |
||
| 217 | private $defines = []; |
||
|
|
|||
| 218 | |||
| 219 | // our data formatter |
||
| 220 | private $dataFormatter = null; |
||
| 221 | |||
| 222 | // process support |
||
| 223 | private $persistProcesses = false; |
||
| 224 | |||
| 225 | public function __construct(Injectables $injectables) |
||
| 260 | |||
| 261 | public static function instance() |
||
| 265 | |||
| 266 | // ================================================================== |
||
| 267 | // |
||
| 268 | // Getters and setters go here |
||
| 269 | // |
||
| 270 | // ------------------------------------------------------------------ |
||
| 271 | |||
| 272 | /** |
||
| 273 | * |
||
| 274 | * |
||
| 275 | * @return Action_Logger |
||
| 276 | */ |
||
| 277 | public function getActionLogger() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * |
||
| 283 | * |
||
| 284 | * @param Action_Logger $actionLogger |
||
| 285 | * @return StoryTeller |
||
| 286 | */ |
||
| 287 | public function setActionLogger(Action_Logger $actionLogger) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * |
||
| 295 | * |
||
| 296 | * @return Story_Checkpoint |
||
| 297 | */ |
||
| 298 | public function getCheckpoint() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | * |
||
| 305 | * @param Story_Checkpoint $checkpoint |
||
| 306 | * @return StoryTeller |
||
| 307 | */ |
||
| 308 | public function setCheckpoint(Story_Checkpoint $checkpoint) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * |
||
| 316 | * |
||
| 317 | * @return PageContext |
||
| 318 | */ |
||
| 319 | public function getPageContext() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * |
||
| 325 | * |
||
| 326 | * @param PageContext $pageContext |
||
| 327 | * @return StoryTeller |
||
| 328 | */ |
||
| 329 | public function setPageContext(PageContext $pageContext) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * |
||
| 337 | * |
||
| 338 | * @return Story |
||
| 339 | */ |
||
| 340 | public function getStory() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * track the story that we are testing |
||
| 347 | * |
||
| 348 | * NOTE: setting the story also creates a new Story_Result object |
||
| 349 | * so that we can track how the story is getting on |
||
| 350 | * |
||
| 351 | * @param Story $story |
||
| 352 | * @return StoryTeller |
||
| 353 | */ |
||
| 354 | public function setStory(Story $story) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function getScriptFilename() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function setScriptFilename($filename) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * |
||
| 386 | * |
||
| 387 | * @return RuntimeConfigManager |
||
| 388 | */ |
||
| 389 | public function getRuntimeConfigManager() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * |
||
| 395 | * |
||
| 396 | * @param RuntimeConfigManager $runtimeConfigManager |
||
| 397 | * @return StoryTeller |
||
| 398 | */ |
||
| 399 | public function setRuntimeConfigManager(RuntimeConfigManager $runtimeConfigManager) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * |
||
| 407 | * @return Phase |
||
| 408 | */ |
||
| 409 | public function getCurrentPhase() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getCurrentPhaseName() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * |
||
| 425 | * @param Phase $newPhase |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function setCurrentPhase(Phase $newPhase) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | public function setProseLoader($proseLoader) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * |
||
| 443 | * @return PhaseLoader |
||
| 444 | */ |
||
| 445 | public function getPhaseLoader() |
||
| 449 | |||
| 450 | public function setPhaseLoader($phaseLoader) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * |
||
| 457 | * @return Output |
||
| 458 | */ |
||
| 459 | public function getOutput() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * |
||
| 466 | * @param Output $output |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public function setOutput(Output $output) |
||
| 473 | |||
| 474 | public function setDataFormatter($dataFormatter) |
||
| 478 | |||
| 479 | // ==================================================================== |
||
| 480 | // |
||
| 481 | // Helpers to get parts of the story's context go here |
||
| 482 | // |
||
| 483 | // -------------------------------------------------------------------- |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return object |
||
| 487 | */ |
||
| 488 | public function getConfig() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return \DataSift\Storyplayer\ConfigLib\ActiveConfig |
||
| 499 | */ |
||
| 500 | public function getActiveConfig() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return void |
||
| 507 | */ |
||
| 508 | public function setConfig($config) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return object |
||
| 515 | */ |
||
| 516 | public function getRuntimeConfig() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | public function setRuntimeConfig($runtimeConfig) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | public function saveRuntimeConfig() |
||
| 540 | |||
| 541 | // ================================================================== |
||
| 542 | // |
||
| 543 | // System under test support |
||
| 544 | // |
||
| 545 | // ------------------------------------------------------------------ |
||
| 546 | |||
| 547 | public function getSystemUnderTestConfig() |
||
| 551 | |||
| 552 | // ================================================================== |
||
| 553 | // |
||
| 554 | // Test environment support |
||
| 555 | // |
||
| 556 | // ------------------------------------------------------------------ |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @return object |
||
| 560 | */ |
||
| 561 | public function getTestEnvironmentConfig() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getTestEnvironmentName() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | public function getTestEnvironmentSignature() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @return bool |
||
| 586 | */ |
||
| 587 | public function getPersistTestEnvironment() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @return void |
||
| 594 | */ |
||
| 595 | public function setPersistTestEnvironment() |
||
| 599 | |||
| 600 | // ================================================================== |
||
| 601 | // |
||
| 602 | // Test user support |
||
| 603 | // |
||
| 604 | // ------------------------------------------------------------------ |
||
| 605 | |||
| 606 | /** |
||
| 607 | * have we loaded test users off disk at all? |
||
| 608 | * |
||
| 609 | * @var boolean |
||
| 610 | */ |
||
| 611 | private $hasTestUsers = false; |
||
| 612 | |||
| 613 | /** |
||
| 614 | * our list of test users |
||
| 615 | * |
||
| 616 | * @var null|\DataSift\Stone\ObjectLib\BaseObject |
||
| 617 | */ |
||
| 618 | private $testUsers = null; |
||
| 619 | |||
| 620 | /** |
||
| 621 | * should we treat the file we loaded test users from as read-only? |
||
| 622 | * |
||
| 623 | * @var boolean |
||
| 624 | */ |
||
| 625 | private $testUsersFileIsReadOnly = false; |
||
| 626 | |||
| 627 | /** |
||
| 628 | * which file did we load the test users from? |
||
| 629 | * |
||
| 630 | * @var string |
||
| 631 | */ |
||
| 632 | private $testUsersFilename = null; |
||
| 633 | |||
| 634 | /** |
||
| 635 | * return the list of test users |
||
| 636 | * |
||
| 637 | * @return \DataSift\Stone\ObjectLib\BaseObject |
||
| 638 | */ |
||
| 639 | public function getTestUsers() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * have we loaded any test users from disk? |
||
| 651 | * |
||
| 652 | * @return boolean |
||
| 653 | * TRUE if we have |
||
| 654 | */ |
||
| 655 | public function hasTestUsers() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * remember the test users that we have |
||
| 662 | * |
||
| 663 | * @param \DataSift\Stone\ObjectLib\BaseObject $users |
||
| 664 | * our test users |
||
| 665 | */ |
||
| 666 | public function setTestUsers($users) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * retrieve the filename we loaded test users from |
||
| 674 | * |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | public function getTestUsersFilename() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * remember the filename we loaded test users from |
||
| 684 | * |
||
| 685 | * we'll re-use this filename later on when it is time to save |
||
| 686 | * the test users back to disk |
||
| 687 | * |
||
| 688 | * @param string $filename |
||
| 689 | * the filename to remember |
||
| 690 | */ |
||
| 691 | public function setTestUsersFilename($filename) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * should we treat the file on disk where we loaded test user |
||
| 699 | * data from as read-only? |
||
| 700 | * |
||
| 701 | * NOTE: |
||
| 702 | * |
||
| 703 | * we never treat the loaded data as read-only. Stories are |
||
| 704 | * free to change this data, and these changes will persist |
||
| 705 | * between stories. We just won't save any changes back to |
||
| 706 | * disk if this method call returns TRUE. |
||
| 707 | * |
||
| 708 | * @return boolean |
||
| 709 | */ |
||
| 710 | public function getTestUsersFileIsReadOnly() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * set whether or not we treat the file on disk were we loaded |
||
| 717 | * test user data from as read-only |
||
| 718 | * |
||
| 719 | * NOTE: |
||
| 720 | * |
||
| 721 | * we never treat the loaded data as read-only. Stories are |
||
| 722 | * free to change this data, and these changes will persist |
||
| 723 | * between stories. We just won't save any changes back to |
||
| 724 | * disk if you set this to TRUE. |
||
| 725 | * |
||
| 726 | * @param boolean $readOnly |
||
| 727 | * TRUE if we should not save data back to this file |
||
| 728 | */ |
||
| 729 | public function setTestUsersFileIsReadOnly($readOnly = true) |
||
| 733 | |||
| 734 | // ================================================================== |
||
| 735 | // |
||
| 736 | // Per-story parameter support |
||
| 737 | // |
||
| 738 | // ------------------------------------------------------------------ |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @return array |
||
| 742 | */ |
||
| 743 | public function getParams() |
||
| 758 | |||
| 759 | // ================================================================== |
||
| 760 | // |
||
| 761 | // Accessors of other containers go here |
||
| 762 | // |
||
| 763 | // ------------------------------------------------------------------ |
||
| 764 | |||
| 765 | /** |
||
| 766 | * |
||
| 767 | * @param string $methodName |
||
| 768 | * @param array $methodArgs |
||
| 769 | * @return mixed |
||
| 770 | */ |
||
| 771 | public function __call($methodName, $methodArgs) |
||
| 788 | |||
| 789 | // ================================================================== |
||
| 790 | // |
||
| 791 | // Logging support |
||
| 792 | // |
||
| 793 | // ------------------------------------------------------------------ |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @return Action_LogItem |
||
| 797 | */ |
||
| 798 | public function startAction($text) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @return void |
||
| 805 | */ |
||
| 806 | public function closeAllOpenActions() |
||
| 810 | |||
| 811 | /** |
||
| 812 | * @return void |
||
| 813 | */ |
||
| 814 | public function convertDataForOutput($data) |
||
| 818 | |||
| 819 | // ================================================================== |
||
| 820 | // |
||
| 821 | // Device support |
||
| 822 | // |
||
| 823 | // ------------------------------------------------------------------ |
||
| 824 | |||
| 825 | /** |
||
| 826 | * @return bool |
||
| 827 | */ |
||
| 828 | public function getPersistDevice() |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @return void |
||
| 835 | */ |
||
| 836 | public function setPersistDevice() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * @return BaseObject |
||
| 843 | */ |
||
| 844 | public function getDeviceDetails() |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @return \DataSift\Storyplayer\DeviceLib\DeviceAdapter |
||
| 851 | */ |
||
| 852 | public function getDeviceAdapter() |
||
| 860 | |||
| 861 | /** |
||
| 862 | * @param DeviceLib\DeviceAdapter|null $adapter |
||
| 863 | */ |
||
| 864 | public function setDeviceAdapter($adapter) |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @return string |
||
| 873 | */ |
||
| 874 | public function getDeviceName() |
||
| 878 | |||
| 879 | /** |
||
| 880 | * @return \DataSift\WebDriver\WebDriverSession |
||
| 881 | */ |
||
| 882 | public function getRunningDevice() |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @param string $deviceName |
||
| 899 | * @param BaseObject $deviceDetails |
||
| 900 | */ |
||
| 901 | public function setDevice($deviceName, $deviceDetails) |
||
| 906 | |||
| 907 | /** |
||
| 908 | * @return void |
||
| 909 | */ |
||
| 910 | public function startDevice() |
||
| 948 | |||
| 949 | /** |
||
| 950 | * @return void |
||
| 951 | */ |
||
| 952 | public function stopDevice() |
||
| 993 | |||
| 994 | // ================================================================== |
||
| 995 | // |
||
| 996 | // Processes support |
||
| 997 | // |
||
| 998 | // ------------------------------------------------------------------ |
||
| 999 | |||
| 1000 | /** |
||
| 1001 | * @return bool |
||
| 1002 | */ |
||
| 1003 | public function getPersistProcesses() |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * @return void |
||
| 1010 | */ |
||
| 1011 | public function setPersistProcesses() |
||
| 1015 | |||
| 1016 | // ================================================================== |
||
| 1017 | // |
||
| 1018 | // Helpful methods that we can use to help with testing |
||
| 1019 | // |
||
| 1020 | // ------------------------------------------------------------------ |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * @return CommandRunner |
||
| 1024 | */ |
||
| 1025 | public function getNewCommandRunner() |
||
| 1029 | |||
| 1030 | // ================================================================== |
||
| 1031 | // |
||
| 1032 | // Features from v1 that we no longer support |
||
| 1033 | // |
||
| 1034 | // ------------------------------------------------------------------ |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * @return \Prose\FromEnvironment |
||
| 1038 | */ |
||
| 1039 | public function getEnvironment() |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * @return void |
||
| 1046 | */ |
||
| 1047 | public function getEnvironmentName() |
||
| 1054 | } |
||
| 1055 |
This check marks private properties in classes that are never used. Those properties can be removed.