CookieTrait::setCookie()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Zumba\GastonJS\Cookie;
6
7
/**
8
 * Trait CookieTrait
9
 * @package Zumba\Mink\Driver
10
 */
11
trait CookieTrait {
12
13
  /**
14
   * Sets a cookie on the browser, if null value then delete it
15
   * @param string $name
16
   * @param string $value
17
   */
18
  public function setCookie($name, $value = null) {
19
    if ($value === null) {
20
      $this->browser->removeCookie($name);
0 ignored issues
show
Bug introduced by
The property browser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
    }
22
    //TODO: set the cookie with domain, not with url, meaning www.aaa.com or .aaa.com
23
    if ($value !== null) {
24
      $urlData = parse_url($this->getCurrentUrl());
0 ignored issues
show
Bug introduced by
It seems like getCurrentUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
      $cookie = array("name" => $name, "value" => $value, "domain" => $urlData["host"]);
26
      $this->browser->setCookie($cookie);
27
    }
28
  }
29
30
  /**
31
   * Gets a cookie by its name if exists, else it will return null
32
   * @param string $name
33
   * @return string
34
   */
35
  public function getCookie($name) {
36
    $cookies = $this->browser->cookies();
37
    foreach ($cookies as $cookie) {
38
      if ($cookie instanceof Cookie && strcmp($cookie->getName(), $name) === 0) {
39
        return $cookie->getValue();
40
      }
41
    }
42
    return null;
43
  }
44
45
}
46