KeywordsTest::translationTestDataProvider()   B
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 19
nop 0
dl 0
loc 99
rs 7.0884
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Behat\Gherkin\Keywords;
4
5
use Behat\Gherkin\Keywords\KeywordsDumper;
6
use Behat\Gherkin\Lexer;
7
use Behat\Gherkin\Node\BackgroundNode;
8
use Behat\Gherkin\Node\ExampleTableNode;
9
use Behat\Gherkin\Node\FeatureNode;
10
use Behat\Gherkin\Node\OutlineNode;
11
use Behat\Gherkin\Node\ScenarioNode;
12
use Behat\Gherkin\Parser;
13
use PHPUnit\Framework\TestCase;
14
15
abstract class KeywordsTest extends TestCase
16
{
17
    abstract protected function getKeywords();
18
    abstract protected function getKeywordsArray();
19
    abstract protected function getSteps($keywords, $text, &$line, $keywordType);
20
21
    public function translationTestDataProvider()
22
    {
23
        $keywords = $this->getKeywords();
24
        $dumper = new KeywordsDumper($keywords);
25
        $keywordsArray = $this->getKeywordsArray();
26
27
        // Remove languages with repeated keywords
28
        unset($keywordsArray['en-old'], $keywordsArray['uz']);
29
30
        $data = array();
31
        foreach ($keywordsArray as $lang => $i18nKeywords) {
32
            $features = array();
33
            foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
34
                $line = 1;
35
                if ('en' !== $lang) {
36
                    $line = 2;
37
                }
38
39
                $featureLine = $line;
40
                $line += 5;
41
42
                $keywords = explode('|', $i18nKeywords['background']);
43
                $backgroundLine = $line;
44
                $line += 1;
45
                $background = new BackgroundNode(null, array_merge(
46
                    $this->getSteps($i18nKeywords['given'], 'there is agent A', $line, 'Given'),
47
                    $this->getSteps($i18nKeywords['and'], 'there is agent B', $line, 'Given')
48
                ), $keywords[0], $backgroundLine);
49
50
                $line += 1;
51
52
                $scenarios = array();
53
54
                foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
55
                    $scenarioLine = $line;
56
                    $line += 1;
57
58
                    $steps = array_merge(
59
                        $this->getSteps($i18nKeywords['given'], 'there is agent J', $line, 'Given'),
60
                        $this->getSteps($i18nKeywords['and'], 'there is agent K', $line, 'Given'),
61
                        $this->getSteps($i18nKeywords['when'], 'I erase agent K\'s memory', $line, 'When'),
62
                        $this->getSteps($i18nKeywords['then'], 'there should be agent J', $line, 'Then'),
63
                        $this->getSteps($i18nKeywords['but'], 'there should not be agent K', $line, 'Then')
64
                    );
65
66
                    $scenarios[] = new ScenarioNode('Erasing agent memory', array(), $steps, $scenarioKeyword, $scenarioLine);
67
                    $line += 1;
68
                }
69
                foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
70
                    $outlineLine = $line;
71
                    $line += 1;
72
73
                    $steps = array_merge(
74
                        $this->getSteps($i18nKeywords['given'], 'there is agent <agent1>', $line, 'Given'),
75
                        $this->getSteps($i18nKeywords['and'], 'there is agent <agent2>', $line, 'Given'),
76
                        $this->getSteps($i18nKeywords['when'], 'I erase agent <agent2>\'s memory', $line, 'When'),
77
                        $this->getSteps($i18nKeywords['then'], 'there should be agent <agent1>', $line, 'Then'),
78
                        $this->getSteps($i18nKeywords['but'], 'there should not be agent <agent2>', $line, 'Then')
79
                    );
80
                    $line += 1;
81
82
                    $keywords = explode('|', $i18nKeywords['examples']);
83
                    $table = new ExampleTableNode(array(
84
                        ++$line => array('agent1', 'agent2'),
85
                        ++$line => array('D', 'M')
86
                    ), $keywords[0]);
87
                    $line += 1;
88
89
                    $scenarios[] = new OutlineNode('Erasing other agents\' memory', array(), $steps, $table, $outlineKeyword, $outlineLine);
90
                    $line += 1;
91
                }
92
93
                $features[] = new FeatureNode(
94
                    'Internal operations',
95
                    <<<DESC
96
In order to stay secret
97
As a secret organization
98
We need to be able to erase past agents' memory
99
DESC
100
                    ,
101
                    array(),
102
                    $background,
103
                    $scenarios,
104
                    $featureKeyword,
105
                    $lang,
106
                    __DIR__ . DIRECTORY_SEPARATOR . $lang . '_' . ($transNum + 1) . '.feature',
107
                    $featureLine
108
                );
109
            }
110
111
            $dumped = $dumper->dump($lang, false, true);
112
113
            foreach ($dumped as $num => $dumpedFeature) {
0 ignored issues
show
Bug introduced by
The expression $dumped of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
114
                $data[$lang . "_" . $num] = array($lang, $num, $features[$num], $dumpedFeature);
115
            }
116
        }
117
118
        return $data;
119
    }
120
121
    /**
122
     * @dataProvider translationTestDataProvider
123
     *
124
     * @param string      $language language name
125
     * @param int         $num      Fixture index for that language
126
     * @param FeatureNode $etalon   etalon features (to test against)
127
     * @param string      $source   gherkin source
128
     */
129
    public function testTranslation($language, $num, FeatureNode $etalon, $source)
130
    {
131
        $keywords = $this->getKeywords();
132
        $lexer = new Lexer($keywords);
133
        $parser = new Parser($lexer);
134
135
        try {
136
            $parsed = $parser->parse($source, __DIR__ . DIRECTORY_SEPARATOR . $language . '_' . ($num + 1) . '.feature');
137
        } catch (\Exception $e) {
138
            throw new \Exception($e->getMessage() . ":\n" . $source, 0, $e);
139
        }
140
141
        $this->assertEquals($etalon, $parsed);
142
    }
143
}
144