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
Pull Request — master (#24)
by Vincent
04:48
created

ScenarioStateHookDispatcher::dispatchScopeHooks()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 37
Code Lines 21

Duplication

Lines 9
Ratio 24.32 %

Importance

Changes 0
Metric Value
dl 9
loc 37
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ScenarioStateBehatExtension project.
5
 *
6
 * (c) Rodrigue Villetard <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gorghoa\ScenarioStateBehatExtension;
13
14
use Behat\Testwork\Call\CallCenter;
15
use Behat\Testwork\Call\CallResult;
16
use Behat\Testwork\Call\CallResults;
17
use Behat\Testwork\Hook\Call\HookCall;
18
use Behat\Testwork\Hook\Hook;
19
use Behat\Testwork\Hook\HookRepository;
20
use Behat\Testwork\Hook\Scope\HookScope;
21
use Doctrine\Common\Annotations\Reader;
22
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
23
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer;
24
25
/**
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class ScenarioStateHookDispatcher
29
{
30
    /**
31
     * @var HookRepository
32
     */
33
    private $repository;
34
35
    /**
36
     * @var CallCenter
37
     */
38
    private $callCenter;
39
40
    /**
41
     * @var Reader
42
     */
43
    private $reader;
44
45
    /**
46
     * @var ScenarioStateInitializer
47
     */
48
    private $store;
49
50
    /**
51
     * Initializes scenario state hook dispatcher.
52
     *
53
     * @param HookRepository           $repository
54
     * @param CallCenter               $callCenter
55
     * @param ScenarioStateInitializer $store
56
     * @param Reader                   $reader
57
     */
58
    public function __construct(HookRepository $repository, CallCenter $callCenter, ScenarioStateInitializer $store, Reader $reader)
59
    {
60
        $this->repository = $repository;
61
        $this->callCenter = $callCenter;
62
        $this->reader = $reader;
63
        $this->store = $store;
64
    }
65
66
    /**
67
     * Dispatches hooks for a specified event.
68
     *
69
     * @param HookScope $scope
70
     *
71
     * @return CallResults
72
     */
73
    public function dispatchScopeHooks(HookScope $scope)
74
    {
75
        $results = array();
76
        foreach ($this->repository->getScopeHooks($scope) as $hook) {
77
            $function = $hook->getReflection();
78
79
            // No `@ScenarioStateArgument` annotation found
80
            if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) {
0 ignored issues
show
Compatibility introduced by
$function of type object<ReflectionFunctionAbstract> is not a sub-type of object<ReflectionMethod>. It seems like you assume a child class of the class ReflectionFunctionAbstract to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
81
                $results[] = $this->callCenter->makeCall(new HookCall($scope, $hook));
82
                continue;
83
            }
84
85
//            $match = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
//            $i = array_slice(array_keys($match), -1, 1)[0];
87
            $paramsKeys = array_map(function($element) {
88
                return $element->name;
89
            }, $function->getParameters());
90
            var_dump($function->getParameters());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($function->getParameters()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
91
            $store = $this->store->getStore();
92
93
            /** @var ScenarioStateArgument[] $annotations */
94
            $annotations = $this->reader->getMethodAnnotations($function);
0 ignored issues
show
Compatibility introduced by
$function of type object<ReflectionFunctionAbstract> is not a sub-type of object<ReflectionMethod>. It seems like you assume a child class of the class ReflectionFunctionAbstract to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95 View Code Duplication
            foreach ($annotations as $annotation) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                if ($annotation instanceof ScenarioStateArgument &&
97
                    in_array($annotation->getArgument(), $paramsKeys) &&
98
                    $store->hasStateFragment($annotation->getName())
99
                ) {
100
                    $match[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
0 ignored issues
show
Coding Style Comprehensibility introduced by
$match was never initialized. Although not strictly required by PHP, it is generally a good practice to add $match = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
101
                    $match[strval(++$i)] = $store->getStateFragment($annotation->getName());
0 ignored issues
show
Bug introduced by
The variable $match does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $i does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
102
                }
103
            }
104
105
            $results[] = $this->callCenter->makeCall(new ScenarioStateCall($scope, $hook, ['foo', 'bar']));
106
        }
107
108
        return new CallResults($results);
109
    }
110
111
    /**
112
     * Dispatches single event hook.
113
     *
114
     * @param HookScope $scope
115
     * @param Hook      $hook
116
     *
117
     * @return CallResult
118
     */
119
    private function dispatchHook(HookScope $scope, Hook $hook)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
120
    {
121
        return $this->callCenter->makeCall(new HookCall($scope, $hook));
122
    }
123
}
124