Completed
Pull Request — master (#51)
by
unknown
04:10
created

TestableInput   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 16.5 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 17
loc 103
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A asAdvancedSearch() 9 9 1
A asTableFilter() 8 8 1
getSearchSelector() 0 1 ?
getFilterSelector() 0 1 ?
setValue() 0 1 ?
A getSelector() 0 4 1
A computeAuxName() 0 4 1
A isVisible() 0 4 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 hipanel\tests\_support\Page\Widget\Input;
4
5
use hipanel\tests\_support\AcceptanceTester;
6
7
/**
8
 * Class TestableInput
9
 *
10
 * Basic class for input elements.
11
 * The elements can be created in three different ways:
12
 * 1. Free input element
13
 *  In this case, instance should be created using a common constructor.
14
 *  The selector of the element should be put to the constructor.
15
 *      new Class(tester, selector)
16
 *
17
 * 2. As Advanced search element
18
 *      Class::asAdvancedSearch($tester, $title)
19
 *
20
 * 3. As Table filter element of indexes page
21
 *      Class::asTableFilter($tester, $columnName)
22
 *
23
 *  In last two cases, the element should be created with related named
24
 *  constructors.
25
 *  The second argument is title of element in Advanced search or
26
 *  column name in Table filter respectively.
27
 *
28
 * @package hipanel\tests\_support\Page\Widget\Input
29
 */
30
abstract class TestableInput
31
{
32
    protected $tester;
33
34
    /**
35
     * @var string $title
36
     */
37
    protected $title;
38
39
    /**
40
     * @var string $auxName
41
     */
42
    protected $auxName;
43
44
    /**
45
     * @var string $selector
46
     */
47
    protected $selector;
48
49
    const AS_BASE = 'div.advanced-search ';
50
51
    const TF_BASE = 'tr.filters ';
52
53
    /**
54
     * TestableInput constructor.
55
     * @param AcceptanceTester $tester
56
     * @param $selector
57
     */
58
    public function __construct(AcceptanceTester $tester, $selector)
59
    {
60
        $this->tester = $tester;
61
        $this->selector = $selector;
62
    }
63
64
    /**
65
     * @param AcceptanceTester $tester
66
     * @param $title
67
     * @return TestableInput
68
     */
69 View Code Duplication
    public static function asAdvancedSearch(AcceptanceTester $tester, $title): TestableInput
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...
70
    {
71
        $instance           = new static($tester, "");
72
        $instance->title    = $title;
73
        $instance->auxName  = $instance->computeAuxName($title);
74
        $instance->selector = $instance->getSearchSelector();
75
76
        return $instance;
77
    }
78
79
    /**
80
     * @param AcceptanceTester $tester
81
     * @param $name
82
     * @return TestableInput
83
     */
84 View Code Duplication
    public static function asTableFilter(AcceptanceTester $tester, $columnName): TestableInput
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...
85
    {
86
        $instance           = new static($tester, "");
87
        $instance->auxName  = $instance->computeAuxName($columnName);
88
        $instance->selector = $instance->getFilterSelector();
89
90
        return $instance;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    abstract protected function getSearchSelector(): string;
97
98
    /**
99
     * @return string
100
     */
101
    abstract protected function getFilterSelector(): string;
102
103
    /**
104
     * @param string $value
105
     */
106
    abstract public function setValue(string $value): void;
107
108
    /**
109
     * @return string
110
     */
111
    public function getSelector(): string
112
    {
113
        return $this->selector;
114
    }
115
116
    /**
117
     * @param string $name
118
     * @return string
119
     */
120
    private function computeAuxName(string $name): string
121
    {
122
        return substr(strtolower($name), 0, 5);
123
    }
124
125
    /**
126
     * Checks whether input is visible
127
     */
128
    public function isVisible(): void
129
    {
130
        $this->tester->seeElement($this->selector);
131
    }
132
}
133