Completed
Push — master ( 782892...5bf53f )
by Juan Francisco
03:56
created

KeyboardTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 22.35 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 1
dl 19
loc 85
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B normalizeCharForKeyEvent() 0 15 6
A keyEventModifierControl() 0 10 3
A keyDown() 6 6 1
A keyPress() 6 6 1
A keyUp() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Behat\Mink\Exception\DriverException;
6
7
/**
8
 * Class KeyboardTrait
9
 * @package Zumba\Mink\Driver
10
 */
11
trait KeyboardTrait {
12
13
  /**
14
   * Does some normalization for the char we want to do keyboard events with.
15
   * @param $char
16
   * @throws DriverException
17
   * @return string
18
   */
19
  protected function normalizeCharForKeyEvent($char) {
20
    if (!is_int($char) && !is_string($char)) {
21
      throw new DriverException("Unsupported key type, can only be integer or string");
22
    }
23
24
    if (is_string($char) && strlen($char) !== 1) {
25
      throw new DriverException("Key can only have ONE character");
26
    }
27
28
    $key = $char;
29
    if (is_int($char)) {
30
      $key = chr($char);
31
    }
32
    return $key;
33
  }
34
35
  /**
36
   * Does some control and normalization for the key event modifier
37
   * @param $modifier
38
   * @return string
39
   * @throws DriverException
40
   */
41
  protected function keyEventModifierControl($modifier) {
42
    if ($modifier === null) {
43
      $modifier = "none";
44
    }
45
46
    if (!in_array($modifier, array("none", "alt", "ctrl", "shift", "meta"))) {
47
      throw new DriverException("Unsupported key modifier $modifier");
48
    }
49
    return $modifier;
50
  }
51
52
  /**
53
   * Send a key-down event to the browser element
54
   * @param        $xpath
55
   * @param        $char
56
   * @param string $modifier
57
   * @throws DriverException
58
   */
59 View Code Duplication
  public function keyDown($xpath, $char, $modifier = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() 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...
61
    $key = $this->normalizeCharForKeyEvent($char);
62
    $modifier = $this->keyEventModifierControl($modifier);
63
    return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keydown", $key, $modifier);
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...
64
  }
65
66
  /**
67
   * @param string $xpath
68
   * @param string $char
69
   * @param string $modifier
70
   * @throws DriverException
71
   */
72 View Code Duplication
  public function keyPress($xpath, $char, $modifier = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
73
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() 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...
74
    $key = $this->normalizeCharForKeyEvent($char);
75
    $modifier = $this->keyEventModifierControl($modifier);
76
    return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keypress", $key, $modifier);
77
  }
78
79
  /**
80
   * Pressed up specific keyboard key.
81
   *
82
   * @param string         $xpath
83
   * @param string|integer $char could be either char ('b') or char-code (98)
84
   * @param string         $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
85
   *
86
   * @throws DriverException                  When the operation cannot be done
87
   */
88 View Code Duplication
  public function keyUp($xpath, $char, $modifier = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
89
    $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() 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...
90
    $element = $this->findElement($xpath, 1);
0 ignored issues
show
Bug introduced by
It seems like findElement() 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...
91
    $key = $this->normalizeCharForKeyEvent($char);
92
    $modifier = $this->keyEventModifierControl($modifier);
93
    return $this->browser->keyEvent($element["page_id"], $element["ids"][0], "keyup", $key, $modifier);
94
  }
95
}
96