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 ( e88f87...0f9eb6 )
by Bruno
02:27
created

RuntimePlaceholdersTransformation   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 51
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getPattern() 0 3 1
A replaceStringPlaceholders() 0 9 2
A replaceTablePlaceholders() 0 3 1
A supportsDefinitionAndArgument() 0 9 4
A tableHasPlaceholders() 0 3 1
A transformArgument() 0 9 3
1
<?php
2
3
4
namespace Ciandt\Behat\PlaceholdersExtension\Transformation;
5
6
use Behat\Behat\Definition\Call\DefinitionCall;
7
use Behat\Gherkin\Node\TableNode;
8
use Behat\Testwork\Call\RuntimeCallee;
9
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository;
10
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainer;
11
use Ciandt\Behat\PlaceholdersExtension\PlaceholderContainer\PlaceholderContainerStepNode;
12
13
/**
14
 *
15
 */
16
final class RuntimePlaceholdersTransformation extends RuntimeCallee implements PlaceholdersTransformation {
17
18
    const USER_DEFINED_PLACEHOLDER_REGEX = '/\${(?P<placeholder>[a-zA-Z0-9_-]+)}/';
19
20
    public function __toString() {
21
        return 'UserDefinedPlaceholderTransform';
22
    }
23
24
    public function getPattern() {
25
        return self::USER_DEFINED_PLACEHOLDER_REGEX;
26
    }
27
28
    private static function replaceStringPlaceholders($string, PlaceholdersRepository $repository, $tags) {
29
        preg_match_all(self::USER_DEFINED_PLACEHOLDER_REGEX, $string, $matches, PREG_SET_ORDER);
30
        foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches 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...
31
            $placeholder = $match['placeholder'];
32
            $replacement = $repository->getReplacement($placeholder, $tags);
33
            $string = str_replace('${' . $placeholder . '}', $replacement, $string);
34
        }
35
        return $string;
36
    }
37
38
    private static function replaceTablePlaceholders(TableNode $table, PlaceholdersRepository $repository, PlaceholderContainerStepNode $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $table 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 $repository 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 $container 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...
39
        //@todo
40
    }
41
42
    public static function supportsDefinitionAndArgument($tags, $argumentValue) {
43
            if (is_string($argumentValue) && preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $argumentValue) === 1) {
44
                return true;
45
            }
46
            if ($argumentValue instanceof TableNode) {
47
                return false;
48
            }
49
        return false;
50
    }
51
52
    private static function tableHasPlaceholders(TableNode $table) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
53
        return (preg_match(self::USER_DEFINED_PLACEHOLDER_REGEX, $table->getTableAsString()) === 1);
54
    }
55
56
    public static function transformArgument($argumentValue, PlaceholdersRepository $repository, $tags) {
57
        if (is_string($argumentValue)) {
58
            return self::replaceStringPlaceholders($argumentValue, $repository, $tags);
59
        }
60
61
        if ($argumentValue instanceof TableNode) {
62
            return self::replaceTablePlaceholders($table, $repository, $tags);
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
        }
64
    }
65
66
}
67