FormstoneContext   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 16.3 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 15
loc 92
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A iFillInSelecter() 0 4 1
A iFillInDropdown() 0 4 1
A selectComponentValue() 0 18 4
A selectValue() 0 7 1
A openComponent() 15 15 3

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 Novaway\CommonContexts\Context;
4
5
use Behat\Mink\Element\DocumentElement;
6
7
/**
8
 * Context for Formstone components
9
 * http://formstone.it/
10
 */
11
class FormstoneContext extends BaseContext
12
{
13
    /**
14
     * Choose an option in selecter field
15
     *
16
     * @When /^(?:|I )fill in selecter "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
17
     * @When /^(?:|I )fill in selecter "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
18
     */
19
    public function iFillInSelecter($field, $value)
20
    {
21
        $this->selectValue('selecter', $field, $value);
22
    }
23
24
    /**
25
     * Choose an option in dropdown component field
26
     *
27
     * @When /^(?:|I )fill in dropdown "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
28
     * @When /^(?:|I )fill in dropdown "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
29
     */
30
    public function iFillInDropdown($field, $value)
31
    {
32
        $this->selectValue('dropdown', $field, $value);
33
    }
34
35
    /**
36
     * Select value in component
37
     *
38
     * @param string $component
39
     * @param string $field
40
     * @param string $value
41
     * @throws \Exception
42
     */
43
    private function selectValue($component, $field, $value)
44
    {
45
        $page = $this->getSession()->getPage();
46
47
        $this->openComponent($page, $component, $field);
48
        $this->selectComponentValue($page, $component, $field, $value);
49
    }
50
51
    /**
52
     * Open component choice list
53
     *
54
     * @param DocumentElement $page
55
     * @param string          $component
56
     * @param string          $field
57
     * @throws \Exception
58
     */
59 View Code Duplication
    private function openComponent(DocumentElement $page, $component, $field)
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
    {
61
        $select = $page->find('css', sprintf('#%s', $field));
62
        if (!$select) {
63
            throw new \Exception(sprintf('No select "%s" found', $field));
64
        }
65
66
        $fieldName = sprintf('.fs-%1$s .fs-%1$s-selected', $component);
67
        $choices   = $select->getParent()->find('css', $fieldName);
68
        if (!$choices) {
69
            throw new \Exception(sprintf('No field "%s" found', $field));
70
        }
71
72
        $choices->press();
73
    }
74
75
    /**
76
     * Select value in choice list
77
     *
78
     * @param DocumentElement $page
79
     * @param string          $component
80
     * @param string          $field
81
     * @param string          $value
82
     * @throws \Exception
83
     */
84
    private function selectComponentValue(DocumentElement $page, $component, $field, $value)
85
    {
86
        $select = $page->find('css', sprintf('#%s', $field));
87
        if (!$select) {
88
            throw new \Exception(sprintf('No select "%s" found', $field));
89
        }
90
91
        $selector = sprintf('.fs-%1$s button.fs-%1$s-item', $component);
92
        $choices  = $page->findAll('css', $selector);
93
        foreach ($choices as $choice) {
94
            if ($choice->getText() == $value) {
95
                $choice->click();
96
                return;
97
            }
98
        }
99
100
        throw new \Exception(sprintf('Value "%s" not found for "%s"', $value, $field));
101
    }
102
}
103