Complex classes like ApiContext 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 ApiContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ApiContext extends RawPageContext |
||
12 | { |
||
13 | protected $response; |
||
14 | |||
15 | public function getResponse() |
||
19 | |||
20 | protected function getHeaders() |
||
36 | |||
37 | /** |
||
38 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on "(?<page>[^"].*)?"$/ |
||
39 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on the (.*) (?<hasPage>page|resource)$/ |
||
40 | * @Given /^I prepare a (?<method>[A-Za-z]+) request on the (.*) (?<hasPage>page|resource) with:?$/ |
||
41 | */ |
||
42 | public function iPrepareRequest($method, $page, $hasPage = false, TableNode $table = null) |
||
57 | |||
58 | /** |
||
59 | * @Given /^I specified the following request http (?<scheme>[a-z]+) credentials:?$/ |
||
60 | */ |
||
61 | public function iSpecifiedTheFollowingHttpAuthentication(TableNode $credentialsTable, $scheme) |
||
69 | |||
70 | /** |
||
71 | * @Given /^I specified the following request oauth credentials:?$/ |
||
72 | */ |
||
73 | public function iSpecifiedTheFollowingOauthCredentials(TableNode $credentialsTable) |
||
81 | |||
82 | /** |
||
83 | * @Given /^I specified the following request headers:?$/ |
||
84 | */ |
||
85 | public function iSpecifiedHeaders(TableNode $table) |
||
89 | |||
90 | /** |
||
91 | * @Given /^I specified the following request queries:?$/ |
||
92 | */ |
||
93 | public function iSpecifiedQueries(TableNode $table) |
||
97 | |||
98 | /** |
||
99 | * @Given /^I specified the following request body:?$/ |
||
100 | */ |
||
101 | public function iSpecifiedTheBody($data) |
||
113 | |||
114 | /** |
||
115 | * @Given /^I specified the following request data:?$/ |
||
116 | */ |
||
117 | public function iSpecifiedData(TableNode $dataTable) |
||
127 | |||
128 | /** |
||
129 | * @Given /^I specified the following request files:?$/ |
||
130 | */ |
||
131 | public function iSpecifiedFiles(TableNode $fileTable) |
||
143 | |||
144 | /** |
||
145 | * @Given /^I specified the following request cookies:?$/ |
||
146 | */ |
||
147 | public function iSpecifiedCookies(TableNode $cookiesTable) |
||
151 | |||
152 | /** |
||
153 | * @Given /^I specified the following request options:?$/ |
||
154 | */ |
||
155 | public function iSpecifiedOptions(TableNode $optionsTable) |
||
159 | |||
160 | /** |
||
161 | * @When /^I send the request$/ |
||
162 | */ |
||
163 | public function iSendTheRequest() |
||
171 | |||
172 | /** |
||
173 | * @Then /^I should receive a (?<httpCode>[0-9]+) response$/ |
||
174 | * @Then /^I should receive a (?<httpCode>[0-9]+) (?<shortType>[a-zA-Z]+) response$/ |
||
175 | */ |
||
176 | public function iShouldReceiveResponse($httpCode, $shortType = null) |
||
219 | |||
220 | /** |
||
221 | * @Then /^the response should contains? the following headers:?$/ |
||
222 | */ |
||
223 | public function theResponseShouldContainsHeaders(TableNode $headerTable) |
||
235 | |||
236 | /** |
||
237 | * @Then /^the response should contains? the following json:?$/ |
||
238 | */ |
||
239 | public function theResponsShouldContainsJson($jsonData) |
||
274 | |||
275 | /** |
||
276 | * @Then /^the response should contains?:?$/ |
||
277 | */ |
||
278 | public function theResponseShouldContains(PyStringNode $bodyNode) |
||
282 | } |
||
283 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.