TagFilter   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 131
Duplicated Lines 7.63 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 10
loc 131
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B filterFeature() 5 43 7
A isFeatureMatch() 0 4 1
A isScenarioMatch() 5 14 5
B isTagsMatchCondition() 0 23 9

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
/*
4
 * This file is part of the Behat Gherkin.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Gherkin\Filter;
12
13
use Behat\Gherkin\Node\FeatureNode;
14
use Behat\Gherkin\Node\OutlineNode;
15
use Behat\Gherkin\Node\ScenarioInterface;
16
17
/**
18
 * Filters scenarios by feature/scenario tag.
19
 *
20
 * @author Konstantin Kudryashov <[email protected]>
21
 */
22
class TagFilter extends ComplexFilter
23
{
24
    protected $filterString;
25
26
    /**
27
     * Initializes filter.
28
     *
29
     * @param string $filterString Name filter string
30
     */
31 4
    public function __construct($filterString)
32
    {
33 4
        $this->filterString = trim($filterString);
34 4
    }
35
36
    /**
37
     * Filters feature according to the filter.
38
     *
39
     * @param FeatureNode $feature
40
     *
41
     * @return FeatureNode
42
     */
43 2
    public function filterFeature(FeatureNode $feature)
44
    {
45 2
        $scenarios = array();
46 2
        foreach ($feature->getScenarios() as $scenario) {
47 2
            if (!$this->isScenarioMatch($feature, $scenario)) {
48 1
                continue;
49
            }
50
51 2
            if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
52
53 1
                $exampleTables = array();
54
55 1 View Code Duplication
                foreach ($scenario->getExampleTables() as $exampleTable) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56 1
                    if ($this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags(), $exampleTable->getTags()))) {
57 1
                        $exampleTables[] = $exampleTable;
58
                    }
59
                }
60
61 1
                $scenario = new OutlineNode(
62 1
                    $scenario->getTitle(),
63 1
                    $scenario->getTags(),
64 1
                    $scenario->getSteps(),
65 1
                    $exampleTables,
66 1
                    $scenario->getKeyword(),
67 1
                    $scenario->getLine()
68
                );
69
            }
70
71 2
            $scenarios[] = $scenario;
72
        }
73
74 2
        return new FeatureNode(
75 2
            $feature->getTitle(),
76 2
            $feature->getDescription(),
77 2
            $feature->getTags(),
78 2
            $feature->getBackground(),
79 2
            $scenarios,
80 2
            $feature->getKeyword(),
81 2
            $feature->getLanguage(),
82 2
            $feature->getFile(),
83 2
            $feature->getLine()
84
        );
85
    }
86
87
    /**
88
     * Checks if Feature matches specified filter.
89
     *
90
     * @param FeatureNode $feature Feature instance
91
     *
92
     * @return Boolean
93
     */
94 1
    public function isFeatureMatch(FeatureNode $feature)
95
    {
96 1
        return $this->isTagsMatchCondition($feature->getTags());
97
    }
98
99
    /**
100
     * Checks if scenario or outline matches specified filter.
101
     *
102
     * @param FeatureNode $feature Feature node instance
103
     * @param ScenarioInterface $scenario Scenario or Outline node instance
104
     *
105
     * @return Boolean
106
     */
107 3
    public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenario)
108
    {
109 3
        if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
110 2 View Code Duplication
            foreach ($scenario->getExampleTables() as $example) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
111 2
                if ($this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags(), $example->getTags()))) {
112 2
                    return true;
113
                }
114
            }
115
116 1
            return false;
117
        }
118
119 2
        return $this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags()));
120
    }
121
122
    /**
123
     * Checks that node matches condition.
124
     *
125
     * @param string[] $tags
126
     *
127
     * @return Boolean
128
     */
129 4
    protected function isTagsMatchCondition($tags)
130
    {
131 4
        $satisfies = true;
132
133 4
        foreach (explode('&&', $this->filterString) as $andTags) {
134 4
            $satisfiesComma = false;
135
136 4
            foreach (explode(',', $andTags) as $tag) {
137 4
                $tag = str_replace('@', '', trim($tag));
138
139 4
                if ('~' === $tag[0]) {
140 4
                    $tag = mb_substr($tag, 1, mb_strlen($tag, 'utf8') - 1, 'utf8');
141 4
                    $satisfiesComma = !in_array($tag, $tags) || $satisfiesComma;
142
                } else {
143 4
                    $satisfiesComma = in_array($tag, $tags) || $satisfiesComma;
144
                }
145
            }
146
147 4
            $satisfies = (false !== $satisfiesComma && $satisfies && $satisfiesComma) || false;
148
        }
149
150 4
        return $satisfies;
151
    }
152
}
153