GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

branchScenarioVariants()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.5806
cc 4
eloc 21
nc 4
nop 2
1
<?php
2
3
namespace Ciandt\Behat\PlaceholdersExtension\Gherkin;
4
5
use Behat\Gherkin\Cache\CacheInterface;
6
use Behat\Gherkin\Loader\GherkinFileLoader;
7
use Behat\Gherkin\Node\FeatureNode;
8
use Behat\Gherkin\Node\OutlineNode;
9
use Behat\Gherkin\Node\ScenarioInterface;
10
use Behat\Gherkin\Node\ScenarioNode;
11
use Behat\Gherkin\Parser;
12
use Ciandt\Behat\PlaceholdersExtension\Utils\PlaceholderUtils;
13
use phpDocumentor\Reflection\Types\Boolean;
14
15
/**
16
 * Gherkin *.feature files loader.
17
 * Modified to branch Scenarios per variant.
18
 *
19
 * @author Bruno Wowk <[email protected]>
20
 */
21
class ScenarioBranchingFileLoader extends GherkinFileLoader
22
{
23
   
24
    /**
25
     * Parses feature at provided absolute path.
26
     *
27
     * @param string $path Feature path
28
     *
29
     * @return FeatureNode
30
     */
31
    protected function parseFeature($path)
32
    {
33
        $filename = $this->findRelativePath($path);
34
        $content = file_get_contents($path);
35
        $feature = $this->parser->parse($content, $filename);
36
        return null === $feature? $feature : $this->branchFeatureVariants($feature);
37
    }
38
    
39
    /**
40
     * Branches Scenarios/Outlines with variant tags for each variant.
41
     *
42
     * @param FeatureNode $feature Parsed feature
43
     *
44
     * @return FeatureNode
45
     */
46
    private function branchFeatureVariants(FeatureNode $feature)
47
    {
48
        $scenarios = array();
49
        
50
        foreach ($feature->getScenarios() as $scenario) {
51
            $scenarios = array_merge($scenarios, $this->getBranchedScenario($scenario, $feature));
52
        }
53
54
        return new FeatureNode(
55
            $feature->getTitle(),
56
            $feature->getDescription(),
57
            $feature->getTags(),
58
            $feature->getBackground(),
59
            $scenarios,
60
            $feature->getKeyword(),
61
            $feature->getLanguage(),
62
            $feature->getFile(),
63
            $feature->getLine()
64
        );
65
    }
66
    
67
    private function getBranchedScenario(ScenarioInterface $scenarioLike, FeatureNode $feature)
68
    {
69
        $scenarioTags = $scenarioLike->getTags();
70
        $featureTags = $feature->getTags();
71
        $tags = array_merge($scenarioTags, $featureTags);
72
        
73
        $variants = PlaceholderUtils::filterVariantTags($tags, false);
74
        
75
        if (count($variants) <= 1) {
76
            return array($scenarioLike);
77
        } else {
78
            return $this->branchScenarioVariants($scenarioLike, $variants);
79
        }
80
        
81
    }
82
    
83
    private function branchScenarioVariants(ScenarioInterface $scenarioLike, $variants)
84
    {
85
        $scenarios = array();
86
        $nonVariantTags = PlaceholderUtils::filterVariantTags($scenarioLike->getTags(), true);
87
        foreach ($variants as $variant) {
88
            $tags = array_merge($nonVariantTags, array($variant));
89
            if ($scenarioLike instanceof ScenarioNode) {
90
                $scenarios[] = new ScenarioNode(
91
                    $scenarioLike->getTitle(),
92
                    $tags,
93
                    $scenarioLike->getSteps(),
94
                    $scenarioLike->getKeyword(),
95
                    $scenarioLike->getLine()
96
                );
97
            } elseif ($scenarioLike instanceof OutlineNode) {
98
                $scenarios[] = new OutlineNode(
99
                    $scenarioLike->getTitle(),
100
                    $tags,
101
                    $scenarioLike->getSteps(),
102
                    $scenarioLike->getExampleTable(),
103
                    $scenarioLike->getKeyword(),
104
                    $scenarioLike->getLine()
105
                );
106
            }
107
        }
108
        return $scenarios;
109
    }
110
}
111