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.

RuntimePlaceholdersTransformation   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getPattern() 0 3 1
A replaceStringPlaceholders() 0 9 2
B transformArgument() 0 19 5
A replaceTablePlaceholders() 0 3 1
B supportsArgument() 0 18 7
1
<?php
2
3
4
namespace Ciandt\Behat\PlaceholdersExtension\Transformation;
5
6
use Behat\Behat\Definition\Call\DefinitionCall;
7
use Behat\Gherkin\Node\PyStringNode;
8
use Behat\Gherkin\Node\TableNode;
9
use Behat\Testwork\Call\RuntimeCallee;
10
use Ciandt\Behat\PlaceholdersExtension\Config\PlaceholdersRepository;
11
12
/**
13
 *
14
 */
15
 class RuntimePlaceholdersTransformation extends RuntimeCallee implements PlaceholdersTransformation {
16
17
    public function __toString() {
18
        return 'UserDefinedPlaceholderTransform';
19
    }
20
21
    public function getPattern() {
22
        return PlaceholdersRepository::PLACEHOLDER_REGEX;
23
    }
24
25
    protected static function replaceStringPlaceholders(&$string, PlaceholdersRepository $repository) {
26
        preg_match_all(PlaceholdersRepository::PLACEHOLDER_REGEX, $string, $matches, PREG_SET_ORDER);
27
        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...
28
            $placeholder = $match['placeholder'];
29
            $replacement = $repository->getReplacement($placeholder);
30
            $string = str_replace('${' . $placeholder . '}', $replacement, $string);
31
        }
32
        return $string;
33
    }
34
35
    public static function transformArgument($argument, $repository) {
36
        if (is_string($argument)) {
37
            return self::replaceStringPlaceholders($argument, $repository);
38
        }
39
40
        if ($argument instanceof PyStringNode) {
41
            $strings = $argument->getStrings();
42
            foreach ($strings as &$string){
43
                self::replaceStringPlaceholders($string, $repository);
44
            }
45
            return new PyStringNode($strings, $argument->getLine());
46
        }
47
        
48
        if ($argument instanceof TableNode) {
49
            $table = $argument->getTable();
50
            array_walk_recursive($table, 'self::replaceTablePlaceholders', $repository);
51
            return new TableNode($table);
52
        }
53
    }
54
    
55
    private static function replaceTablePlaceholders(&$item, $key, $repository){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Unused Code introduced by
The parameter $key 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...
56
        $item = self::replaceStringPlaceholders($item, $repository);
57
    }
58
    
59
60
    public static function supportsArgument($argument)
61
    {
62
        if (is_string($argument) && preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument) === 1) {
63
            return true;
64
        }
65
66
        if ($argument instanceof PyStringNode &&
67
            preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument->getRaw()) === 1) {
68
            return true;
69
        }
70
        
71
        if ($argument instanceof TableNode &&
72
            preg_match(PlaceholdersRepository::PLACEHOLDER_REGEX, $argument->getTableAsString()) === 1) {
73
            return true;
74
        }
75
        
76
        return false;
77
    }
78
}
79