Conditions | 7 |
Paths | 34 |
Total Lines | 105 |
Code Lines | 71 |
Lines | 17 |
Ratio | 16.19 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
201 | public function testIntegration() |
||
202 | { |
||
203 | if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) |
||
204 | $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set'); |
||
205 | |||
206 | $credentials = $this->loadCredentialsFromFile(); |
||
207 | |||
208 | $optimizelyClient = new OptimizelyApiClient($credentials, 'v2'); |
||
209 | $this->assertTrue($optimizelyClient!=null); |
||
210 | |||
211 | // Create new project |
||
212 | $curDate = date('Y-m-d H:i:s'); |
||
213 | $newProject = new Project(array( |
||
214 | "name" => "Test Project $curDate", |
||
215 | "account_id" => 12345, |
||
216 | "confidence_threshold" => 0.9, |
||
217 | "platform" => "web", |
||
218 | "status" => "active", |
||
219 | "web_snippet" => array( |
||
220 | "enable_force_variation" => false, |
||
221 | "exclude_disabled_experiments" => false, |
||
222 | "exclude_names" => true, |
||
223 | "include_jquery" => true, |
||
224 | "ip_anonymization" => false, |
||
225 | "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$", |
||
226 | "library" => "jquery-1.11.3-trim", |
||
227 | "project_javascript" => "alert(\"Active Experiment\")" |
||
228 | ) |
||
229 | )); |
||
230 | |||
231 | $result = $optimizelyClient->projects()->create($newProject); |
||
232 | $createdProject = $result->getPayload(); |
||
233 | |||
234 | // Create new page in the project |
||
235 | $page = new Page(array( |
||
236 | "edit_url" => "https://www.optimizely.com", |
||
237 | "name" => "Home Page", |
||
238 | "project_id" => $createdProject->getId(), |
||
239 | "activation_code" => "string", |
||
240 | "activation_type" => "immediate", |
||
241 | "archived" => false, |
||
242 | "category" => "article", |
||
243 | "conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]", |
||
244 | "key" => "home_page", |
||
245 | "page_type" => "single_url", |
||
246 | "created" => "2016-10-18T05:07:04.113Z", |
||
247 | "id" => 4000, |
||
248 | "last_modified" => "2016-10-18T05:07:04.113Z" |
||
249 | )); |
||
250 | |||
251 | $result = $optimizelyClient->pages()->create($page); |
||
252 | $createdPage = $result->getPayload(); |
||
253 | |||
254 | $this->assertTrue($createdPage instanceOf Page); |
||
255 | $this->assertTrue($createdPage->getName()=='Home Page'); |
||
256 | |||
257 | // List all existing pages and try to find the created page |
||
258 | $pageFound = false; |
||
259 | try { |
||
260 | $page = 1; |
||
261 | View Code Duplication | for (;;) { |
|
262 | $result = $optimizelyClient->pages()->listAll($createdProject->getId(), $page); |
||
263 | |||
264 | $pages = $result->getPayload(); |
||
265 | |||
266 | foreach ($pages as $pageObject) { |
||
267 | if ($pageObject->getName()=="Home Page") { |
||
268 | $pageFound = true; |
||
269 | break; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | if ($result->getNextPage()==null) |
||
274 | break; |
||
275 | |||
276 | $page ++; |
||
277 | } |
||
278 | } |
||
279 | catch (Exception $e) { |
||
280 | // Handle error. |
||
281 | $code = $e->getCode(); |
||
282 | $httpCode = $e->getHttpCode(); |
||
283 | $message = $e->getMessage(); |
||
284 | $uuid = $e->getUuid(); |
||
285 | echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n"; |
||
286 | } |
||
287 | |||
288 | $this->assertTrue($pageFound); |
||
289 | |||
290 | // Update page |
||
291 | $createdPage->setName('Some new page name'); |
||
292 | $result = $optimizelyClient->pages()->update($createdPage->getId(), $createdPage); |
||
293 | $updatedPage = $result->getPayload(); |
||
294 | |||
295 | $this->assertTrue($updatedPage instanceOf Page); |
||
296 | $this->assertTrue($updatedPage->getName()=='Some new page name'); |
||
297 | |||
298 | // Make project archived |
||
299 | |||
300 | $createdProject->setStatus('archived'); |
||
301 | $result = $optimizelyClient->projects()->update($createdProject->getId(), $createdProject); |
||
302 | $updatedProject = $result->getPayload(); |
||
303 | |||
304 | $this->assertEquals('archived', $updatedProject->getStatus()); |
||
305 | } |
||
306 | } |
||
308 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.