Complex classes like AbstractEntryPoint 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 AbstractEntryPoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class AbstractEntryPoint implements EPInterface { |
||
12 | |||
13 | protected $_AUTH_REQUIRED = true; |
||
14 | protected $_MODULE; |
||
15 | protected $_URL; |
||
16 | protected $_REQUIRED_DATA; |
||
17 | |||
18 | protected $url; |
||
19 | protected $Module; |
||
20 | protected $Options = array(); |
||
21 | protected $Data; |
||
22 | protected $Request; |
||
23 | protected $Response; |
||
24 | |||
25 | public function __construct($url,$options = array()){ |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | public function module($module){ |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public function options(array $options){ |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | public function data(array $data){ |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function execute(){ |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @inheritdoc |
||
84 | */ |
||
85 | public function authRequired() { |
||
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | public function getModule() { |
||
95 | |||
96 | /** |
||
97 | * |
||
98 | */ |
||
99 | public function getData(){ |
||
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | public function getURL(){ |
||
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | public function getResponse(){ |
||
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | public function getRequest(){ |
||
123 | |||
124 | /** |
||
125 | * Configures the URL, by updating any variable placeholders in the URL property on the EntryPoint |
||
126 | * - Replaces $module with $this->Module |
||
127 | * - Replcaes all other variables starting with $, with options in the order they were given |
||
128 | */ |
||
129 | protected function configureURL(){ |
||
159 | |||
160 | /** |
||
161 | * Setup the Request Object property, setup on initial Construct of EntryPoint |
||
162 | */ |
||
163 | protected function setupRequest(){ |
||
166 | |||
167 | /** |
||
168 | * Setup the Response Object Property, not called until after Request Execution |
||
169 | */ |
||
170 | protected function setupResponse(){ |
||
173 | |||
174 | /** |
||
175 | * Verify URL variables have been removed, and that valid number of options were passed. |
||
176 | * @return bool |
||
177 | * @throws EntryPointExecutionFailure |
||
178 | */ |
||
179 | protected function verifyURL(){ |
||
196 | |||
197 | /** |
||
198 | * @return bool |
||
199 | */ |
||
200 | protected function validateData(){ |
||
219 | |||
220 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.