WindowTrait::getWindowName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Behat\Mink\Exception\DriverException;
6
7
/**
8
 * Class WindowTrait
9
 * @package Zumba\Mink\Driver
10
 */
11
trait WindowTrait {
12
  /**
13
   * Returns the current page window name
14
   * @return string
15
   */
16
  public function getWindowName() {
17
    return $this->browser->windowName();
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...
18
  }
19
20
  /**
21
   * Return all the window handles currently present in phantomjs
22
   * @return array
23
   */
24
  public function getWindowNames() {
25
    return $this->browser->windowHandles();
26
  }
27
28
  /**
29
   * Switches to window by name if possible
30
   * @param $name
31
   * @throws DriverException
32
   */
33
  public function switchToWindow($name = null) {
34
    $handles = $this->browser->windowHandles();
35
    if ($name === null) {
36
      //null means back to the main window
37
      return $this->browser->switchToWindow($handles[0]);
38
    }
39
40
    $windowHandle = $this->browser->windowHandle($name);
41
    if (!empty($windowHandle)) {
42
      $this->browser->switchToWindow($windowHandle);
43
    } else {
44
      throw new DriverException("Could not find window handle by a given window name: $name");
45
    }
46
47
  }
48
49
  /**
50
   * Resizing a window with specified size
51
   * @param int    $width
52
   * @param int    $height
53
   * @param string $name
54
   * @throws DriverException
55
   */
56
  public function resizeWindow($width, $height, $name = null) {
57
    if ($name !== null) {
58
      //TODO: add this on the phantomjs stuff
59
      throw new DriverException("Resizing other window than the main one is not supported yet");
60
    }
61
    $this->browser->resize($width, $height);
62
  }
63
64
}
65