Completed
Push — master ( df4d09...dfc47a )
by Ciaran
08:37
created

CucumberKeywords   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 99
ccs 23
cts 28
cp 0.8214
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 6
A getGivenKeywords() 0 4 1
A getWhenKeywords() 0 4 1
A getThenKeywords() 0 4 1
A getAndKeywords() 0 4 1
A getButKeywords() 0 4 1
A prepareStepString() 0 8 2
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\Keywords;
12
13
use Symfony\Component\Yaml\Exception\ParseException;
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * Cucumber-translations reader.
18
 *
19
 * $keywords = new Behat\Gherkin\Keywords\CucumberKeywords($i18nYmlPath);
20
 *
21
 * @author Konstantin Kudryashov <[email protected]>
22
 */
23
class CucumberKeywords extends ArrayKeywords
24
{
25
    /**
26
     * Initializes holder with yaml string OR file.
27
     *
28
     * @param string $yaml Yaml string or file path
29
     */
30 63
    public function __construct($yaml)
31
    {
32
        // Handle filename explicitly for BC reasons, as Symfony Yaml 3.0 does not do it anymore
33 63
        $file = null;
34 63
        if (strpos($yaml, "\n") === false && is_file($yaml)) {
35 63
            if (false === is_readable($yaml)) {
36
                throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $yaml));
37
            }
38
39 63
            $file = $yaml;
40 63
            $yaml = file_get_contents($file);
41
        }
42
43
        try {
44 63
            $content = Yaml::parse($yaml);
45
        } catch (ParseException $e) {
46
            if ($file) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
                $e->setParsedFile($file);
48
            }
49
50
            throw $e;
51
        }
52
53 63
        parent::__construct($content);
54 63
    }
55
56
    /**
57
     * Returns Feature keywords (splitted by "|").
58
     *
59
     * @return string
60
     */
61 61
    public function getGivenKeywords()
62
    {
63 61
        return $this->prepareStepString(parent::getGivenKeywords());
64
    }
65
66
    /**
67
     * Returns When keywords (splitted by "|").
68
     *
69
     * @return string
70
     */
71 61
    public function getWhenKeywords()
72
    {
73 61
        return $this->prepareStepString(parent::getWhenKeywords());
74
    }
75
76
    /**
77
     * Returns Then keywords (splitted by "|").
78
     *
79
     * @return string
80
     */
81 61
    public function getThenKeywords()
82
    {
83 61
        return $this->prepareStepString(parent::getThenKeywords());
84
    }
85
86
    /**
87
     * Returns And keywords (splitted by "|").
88
     *
89
     * @return string
90
     */
91 61
    public function getAndKeywords()
92
    {
93 61
        return $this->prepareStepString(parent::getAndKeywords());
94
    }
95
96
    /**
97
     * Returns But keywords (splitted by "|").
98
     *
99
     * @return string
100
     */
101 61
    public function getButKeywords()
102
    {
103 61
        return $this->prepareStepString(parent::getButKeywords());
104
    }
105
106
    /**
107
     * Trim *| from the begining of the list.
108
     *
109
     * @param string $keywordsString Keywords string
110
     *
111
     * @return string
112
     */
113 61
    private function prepareStepString($keywordsString)
114
    {
115 61
        if (0 === mb_strpos($keywordsString, '*|', 0, 'UTF-8')) {
116 61
            $keywordsString = mb_substr($keywordsString, 2, mb_strlen($keywordsString, 'utf8') - 2, 'utf8');
117
        }
118
119 61
        return $keywordsString;
120
    }
121
}
122