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.
Completed
Push — master ( 04d873...1e92a2 )
by Bruno
03:39
created

ScenarioBranchingFileLoader::parseFeature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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
        $feature = $this->branchFeatureVariants($feature);
0 ignored issues
show
Bug introduced by
It seems like $feature can be null; however, branchFeatureVariants() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
37
                
38
        return $feature;
39
    }
40
    
41
    /**
42
     * Branches Scenarios/Outlines with variant tags for each variant.
43
     *
44
     * @param FeatureNode $feature Parsed feature
45
     *
46
     * @return FeatureNode
47
     */
48
    private function branchFeatureVariants(FeatureNode $feature)
49
    {
50
        $scenarios = array();
51
        
52
        foreach ($feature->getScenarios() as $scenario) {
53
            $scenarios = array_merge($scenarios, $this->getBranchedScenario($scenario, $feature));
54
        }
55
56
        return new FeatureNode(
57
            $feature->getTitle(),
58
            $feature->getDescription(),
59
            $feature->getTags(),
60
            $feature->getBackground(),
61
            $scenarios,
62
            $feature->getKeyword(),
63
            $feature->getLanguage(),
64
            $feature->getFile(),
65
            $feature->getLine()
66
        );
67
    }
68
    
69
    private function getBranchedScenario(ScenarioInterface $scenarioLike, FeatureNode $feature)
70
    {
71
        $scenarioTags = $scenarioLike->getTags();
72
        $featureTags = $feature->getTags();
73
        $tags = array_merge($scenarioTags, $featureTags);
74
        
75
        $variants = PlaceholderUtils::filterVariantTags($tags, false);
76
        
77
        if (count($variants) <= 1) {
78
            return array($scenarioLike);
79
        } else {
80
            return $this->branchScenarioVariants($scenarioLike, $variants);
81
        }
82
        
83
    }
84
    
85
    private function branchScenarioVariants(ScenarioInterface $scenarioLike, $variants)
86
    {
87
        $scenarios = array();
88
        $nonVariantTags = PlaceholderUtils::filterVariantTags($scenarioLike->getTags(), true);
89
        foreach ($variants as $variant) {
90
            $tags = array_merge($nonVariantTags, array($variant));
91
            if ($scenarioLike instanceof ScenarioNode) {
92
                $scenarios[] = new ScenarioNode(
93
                    $scenarioLike->getTitle(),
94
                    $tags,
95
                    $scenarioLike->getSteps(),
96
                    $scenarioLike->getKeyword(),
97
                    $scenarioLike->getLine()
98
                );
99
            } elseif ($scenarioLike instanceof OutlineNode) {
100
                $scenarios[] = new OutlineNode(
101
                    $scenarioLike->getTitle(),
102
                    $tags,
103
                    $scenarioLike->getSteps(),
104
                    $scenarioLike->getExampleTable(),
105
                    $scenarioLike->getKeyword(),
106
                    $scenarioLike->getLine()
107
                );
108
            }
109
        }
110
        return $scenarios;
111
    }
112
}
113