| Conditions | 7 |
| Paths | 34 |
| Total Lines | 131 |
| Code Lines | 91 |
| Lines | 17 |
| Ratio | 12.98 % |
| 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 |
||
| 283 | public function testIntegration() |
||
| 284 | { |
||
| 285 | if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) |
||
| 286 | $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set'); |
||
| 287 | |||
| 288 | $credentials = $this->loadCredentialsFromFile(); |
||
| 289 | |||
| 290 | $optimizelyClient = new OptimizelyApiClient($credentials, 'v2'); |
||
| 291 | $this->assertTrue($optimizelyClient!=null); |
||
| 292 | |||
| 293 | // Create new project |
||
| 294 | $curDate = date('Y-m-d H:i:s'); |
||
| 295 | $newProject = new Project(array( |
||
| 296 | "name" => "Test Project $curDate", |
||
| 297 | "account_id" => 12345, |
||
| 298 | "confidence_threshold" => 0.9, |
||
| 299 | "platform" => "web", |
||
| 300 | "status" => "active", |
||
| 301 | "web_snippet" => array( |
||
| 302 | "enable_force_variation" => false, |
||
| 303 | "exclude_disabled_experiments" => false, |
||
| 304 | "exclude_names" => true, |
||
| 305 | "include_jquery" => true, |
||
| 306 | "ip_anonymization" => false, |
||
| 307 | "ip_filter" => "^206\\.23\\.100\\.([5-9][0-9]|1([0-4][0-9]|50))$", |
||
| 308 | "library" => "jquery-1.11.3-trim", |
||
| 309 | "project_javascript" => "alert(\"Active Experiment\")" |
||
| 310 | ) |
||
| 311 | )); |
||
| 312 | |||
| 313 | $result = $optimizelyClient->projects()->create($newProject); |
||
| 314 | $createdProject = $result->getPayload(); |
||
| 315 | |||
| 316 | // Create new page in the project |
||
| 317 | $page = new Page(array( |
||
| 318 | "edit_url" => "https://www.optimizely.com", |
||
| 319 | "name" => "Home Page", |
||
| 320 | "project_id" => $createdProject->getId(), |
||
| 321 | "activation_code" => "string", |
||
| 322 | "activation_type" => "immediate", |
||
| 323 | "archived" => false, |
||
| 324 | "category" => "article", |
||
| 325 | "conditions" => "[\"and\", {\"type\": \"url\", \"match_type\": \"substring\", \"value\": \"optimize\"}]", |
||
| 326 | "key" => "home_page", |
||
| 327 | "page_type" => "single_url", |
||
| 328 | "created" => "2016-10-18T05:07:04.113Z", |
||
| 329 | "id" => 4000, |
||
| 330 | "last_modified" => "2016-10-18T05:07:04.113Z" |
||
| 331 | )); |
||
| 332 | |||
| 333 | $result = $optimizelyClient->pages()->create($page); |
||
| 334 | $createdPage = $result->getPayload(); |
||
| 335 | |||
| 336 | $this->assertTrue($createdPage instanceOf Page); |
||
| 337 | $this->assertTrue($createdPage->getName()=='Home Page'); |
||
| 338 | |||
| 339 | // Create new event in the project |
||
| 340 | $event = new ClickEvent(array( |
||
| 341 | "event_filter" => array( |
||
| 342 | "filter_type" => "target_selector", |
||
| 343 | "selector" => ".menu-options" |
||
| 344 | ), |
||
| 345 | "name" => "Add to Cart", |
||
| 346 | "archived" => true, |
||
| 347 | "category" => "add_to_cart", |
||
| 348 | "description" => "Some simple event", |
||
| 349 | "event_type" => "click", |
||
| 350 | "key" => "add_to_cart", |
||
| 351 | "created" => "2016-10-18T05:07:04.153Z", |
||
| 352 | "id" => 0, |
||
| 353 | "is_classic" => false, |
||
| 354 | "is_editable" => true, |
||
| 355 | "page_id" => $createdPage->getId(), |
||
| 356 | "project_id" => $createdProject->getId() |
||
| 357 | )); |
||
| 358 | |||
| 359 | $result = $optimizelyClient->events()->createClickEvent($createdPage->getId(), $event); |
||
| 360 | $createdEvent = $result->getPayload(); |
||
| 361 | |||
| 362 | $this->assertTrue($createdEvent instanceOf ClickEvent); |
||
| 363 | $this->assertTrue($createdEvent->getName()=='Add to Cart'); |
||
| 364 | |||
| 365 | // List all existing events and try to find the created event |
||
| 366 | $eventFound = false; |
||
| 367 | try { |
||
| 368 | $page = 1; |
||
| 369 | View Code Duplication | for (;;) { |
|
| 370 | $result = $optimizelyClient->events()->listAll($createdEvent->getId(), $page); |
||
| 371 | |||
| 372 | $events = $result->getPayload(); |
||
| 373 | |||
| 374 | foreach ($events as $event) { |
||
| 375 | if ($event->getName()=="Add to Cart") { |
||
| 376 | $eventFound = true; |
||
| 377 | break; |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | if ($result->getNextPage()==null) |
||
| 382 | break; |
||
| 383 | |||
| 384 | $page ++; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | catch (Exception $e) { |
||
| 388 | // Handle error. |
||
| 389 | $code = $e->getCode(); |
||
| 390 | $httpCode = $e->getHttpCode(); |
||
| 391 | $message = $e->getMessage(); |
||
| 392 | $uuid = $e->getUuid(); |
||
| 393 | echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n"; |
||
| 394 | } |
||
| 395 | |||
| 396 | $this->assertTrue($eventFound); |
||
| 397 | |||
| 398 | // Update page |
||
| 399 | $createdEvent->setName('Add to Cart 2'); |
||
| 400 | $result = $optimizelyClient->events()->update($createdEvent->getId(), $createdEvent); |
||
| 401 | $updatedEvent = $result->getPayload(); |
||
| 402 | |||
| 403 | $this->assertTrue($updatedEvent instanceOf Event); |
||
| 404 | $this->assertTrue($updatedEvent->getName()=='Add to Cart 2'); |
||
| 405 | |||
| 406 | // Make project archived |
||
| 407 | |||
| 408 | $createdProject->setStatus('archived'); |
||
| 409 | $result = $optimizelyClient->projects()->update($createdProject->getId(), $createdProject); |
||
| 410 | $updatedProject = $result->getPayload(); |
||
| 411 | |||
| 412 | $this->assertEquals('archived', $updatedProject->getStatus()); |
||
| 413 | } |
||
| 414 | } |
||
| 416 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: