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 ( dc1b83...0c9d28 )
by Bruno
02:10
created

PlaceholdersReplacer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 145
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setEnvironment() 0 4 1
A setUp() 0 13 3
A test() 0 10 2
A reconstructStep() 0 8 1
A replacePlaceholders() 0 10 2
A getReplacement() 0 14 1
B recursivePlaceholderSearch() 0 14 5
A tearDown() 0 4 1
1
<?php
2
namespace Ciandt\Behat\PlaceholdersExtension\Tester;
3
4
use Ciandt\Behat\PlaceholdersExtension\Config\ConfigsRepository;
5
use Behat\Behat\Tester\Result\StepResult;
6
use Behat\Behat\Tester\StepTester;
7
use Behat\Gherkin\Node\FeatureNode;
8
use Behat\Gherkin\Node\StepNode;
9
use Behat\Testwork\Environment\Environment;
10
use Behat\Testwork\Tester\Setup\SuccessfulSetup;
11
use Behat\Testwork\Tester\Setup\SuccessfulTeardown;
12
use Behat\Behat\Tester\ServiceContainer\TesterExtension;
13
14
/**
15
 * Tester executing step tests in the runtime.
16
 *
17
 */
18
class PlaceholdersReplacer implements StepTester
19
{
20
21
    /**
22
     * @var StepTester
23
     */
24
    private $baseTester;
25
26
    /**
27
     * @var array
28
     */
29
    private $variantTags;
30
31
    /**
32
     * @var array
33
     */
34
    private $variant;
35
36
    /**
37
     * @var array
38
     */
39
    private $configsRepo;
40
41
    /**
42
     * @var array
43
     */
44
    private $placeholders = FALSE;
45
46
    /**
47
     * @var string
48
     */
49
    private $configPath;
50
51
    /**
52
     * @var string
53
     */
54
    private $configSection;
55
56
    /**
57
     * @var string
58
     */
59
    private $environment;
60
61
    public function __construct(StepTester $baseTester, $variantTags, ConfigsRepository $configsRepo)
62
    {
63
        $this->baseTester = $baseTester;
64
        $this->configsRepo = $configsRepo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configsRepo of type object<Ciandt\Behat\Plac...nfig\ConfigsRepository> is incompatible with the declared type array of property $configsRepo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
        $this->variantTags = $variantTags;
66
    }
67
68
    public function setEnvironment($environment)
69
    {
70
        $this->environment = $environment;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     * @todo use the tag to get correct section, not always the default one
76
     */
77
    public function setUp(Environment $env, FeatureNode $feature, StepNode $step, $skip)
78
    {
79
        if ($this->configsRepo->hasTag($step->configTag)) {
0 ignored issues
show
Bug introduced by
The method hasTag cannot be called on $this->configsRepo (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
80
            $this->configSection = 'default';
81
            $this->placeholders = $this->configsRepo->getConfigSection($step->configTag, $this->configSection)['placeholders'];
0 ignored issues
show
Bug introduced by
The property configTag does not seem to exist in Behat\Gherkin\Node\StepNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The method getConfigSection cannot be called on $this->configsRepo (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
82
            $this->configPath = $this->configsRepo->getFilePath($step->configTag);
0 ignored issues
show
Bug introduced by
The method getFilePath cannot be called on $this->configsRepo (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
83
            if ($step->variant) {
84
                $this->variant = $step->variant;
0 ignored issues
show
Bug introduced by
The property variant does not seem to exist in Behat\Gherkin\Node\StepNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
            }
86
        }
87
88
        return new SuccessfulSetup();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function test(Environment $env, FeatureNode $feature, StepNode $step, $skip = false)
95
    {
96
        $tester = $this->baseTester;
97
        if ($this->placeholders) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->placeholders of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
98
            $result = $tester->test($env, $feature, $this->reconstructStep($step), $skip);
99
            return $result;
100
        } else {
101
            return $tester->test($env, $feature, $step, $skip);
102
        }
103
    }
104
105
    private function reconstructStep(StepNode $step)
106
    {
107
        //@todo replace placeholders on arguments (tablenode)        
108
        $arguments = $step->getArguments();
109
        $text = $this->replacePlaceholders($step->getText(), $step->variant, $this->environment);
0 ignored issues
show
Bug introduced by
The property variant does not seem to exist in Behat\Gherkin\Node\StepNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
        return new StepNode(
111
            $step->getKeyword(), $text, $arguments, $step->getLine(), $step->getKeywordType());
112
    }
113
114
    private function replacePlaceholders($string, $var, $env)
0 ignored issues
show
Unused Code introduced by
The parameter $var is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $env is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        preg_match_all('/\${(?P<key>[^}]+)}/i', $string, $placeholders, PREG_SET_ORDER);
117
        foreach ($placeholders as $placeholder) {
0 ignored issues
show
Bug introduced by
The expression $placeholders of type null|array<integer,array<integer,string>> 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...
118
            $key = $placeholder['key'];
119
            $value = $this->getReplacement($key);
120
            $string = str_replace('${' . $key . '}', $value, $string);
121
        }
122
        return $string;
123
    }
124
125
    private function getReplacement($placeholderKey)
126
    {
127
128
        $values = $this->placeholders;
129
        $configPath = $this->configPath;
130
        $section = $this->configSection;
131
        $variant = $this->variant;
132
        $environment = $this->environment;
133
134
        $keys = array('$' . $variant, '$' . $environment, $placeholderKey);
135
        $treePosition = "$configPath>$section>placeholders";
136
137
        return $this->recursivePlaceholderSearch($keys, $values, $treePosition);
138
    }
139
140
    private function recursivePlaceholderSearch($keys, $values, $treePosition)
141
    {
142
        if (empty($keys) || !is_array($values)) {
143
            return $values;
144
        }
145
        $key = array_pop($keys);
146
        if (key_exists($key, $values)) {
147
            return $this->recursivePlaceholderSearch($keys, $values[$key], "$treePosition>$key");
148
        } elseif (key_exists('$default', $values)) {
149
            return $this->recursivePlaceholderSearch($keys, $values['$default'], $treePosition . '>$default');
150
        } else {
151
            throw new \Exception("no placeholder is defined on $treePosition>[$key or \$default]");
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function tearDown(Environment $env, FeatureNode $feature, StepNode $step, $skip, StepResult $result)
159
    {
160
        return new SuccessfulTeardown();
161
    }
162
}
163