Taggable::hasTag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Dictionary;
4
5
use Behat\Behat\Hook\Scope\ScenarioScope;
6
7
trait Taggable
8
{
9
    protected $tags = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
10
    protected $tagLoaded = false;
11
12
    /**
13
     * @BeforeScenario
14
     */
15
    public function storeTags($event)
16
    {
17
        if (false === $this->tagLoaded) {
18
            if ($event instanceof ScenarioScope) {
19
                if (null !== $feature = $event->getFeature()) {
20
                    $this->tags = array_merge($this->tags, $feature->getTags());
21
                }
22
                if (null !== $scenario = $event->getScenario()) {
23
                    $this->tags = array_merge($this->tags, $scenario->getTags());
24
                }
25
            }
26
            $this->tagLoaded = true;
27
        }
28
    }
29
30
    protected function hasTag($name)
31
    {
32
        return in_array($name, $this->tags);
33
    }
34
35
    protected function hasTags(array $names)
36
    {
37
        foreach ($names as $name) {
38
            if (!(0 === strpos($name, '~')) !== $this->hasTag(str_replace('~', '', $name))) {
39
                return false;
40
            }
41
        }
42
43
        return true;
44
    }
45
46
    protected function getTagContent($name)
47
    {
48
        $content = [];
49
50
        foreach ($this->tags as $tag) {
51
            $matches = [];
52
            if (preg_match(sprintf('/^%s\((.*)\)$/', $name), $tag, $matches)) {
53
                $content[] = end($matches);
54
            }
55
        }
56
57
        return $content;
58
    }
59
60
    protected function getTags()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
    {
62
        return $this->tags;
63
    }
64
}
65