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

ScenarioStateHookDispatcher::dispatchScopeHooks()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 24
cp 0
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 25
nc 14
nop 1
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Hook\Dispatcher;
13
14
use Behat\Testwork\Call\CallCenter;
15
use Behat\Testwork\Call\CallResults;
16
use Behat\Testwork\Environment\Call\EnvironmentCall;
17
use Behat\Testwork\Hook\Call\HookCall;
18
use Behat\Testwork\Hook\HookRepository;
19
use Behat\Testwork\Hook\Scope\HookScope;
20
use Doctrine\Common\Annotations\Reader;
21
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
22
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer;
23
24
/**
25
 * @author Vincent Chalamon <[email protected]>
26
 */
27
class ScenarioStateHookDispatcher
28
{
29
    /**
30
     * @var HookRepository
31
     */
32
    private $repository;
33
34
    /**
35
     * @var CallCenter
36
     */
37
    private $callCenter;
38
39
    /**
40
     * @var Reader
41
     */
42
    private $reader;
43
44
    /**
45
     * @var ScenarioStateInitializer
46
     */
47
    private $store;
48
49
    /**
50
     * Initializes scenario state hook dispatcher.
51
     *
52
     * @param HookRepository           $repository
53
     * @param CallCenter               $callCenter
54
     * @param ScenarioStateInitializer $store
55
     * @param Reader                   $reader
56
     */
57
    public function __construct(HookRepository $repository, CallCenter $callCenter, ScenarioStateInitializer $store, Reader $reader)
58
    {
59
        $this->repository = $repository;
60
        $this->callCenter = $callCenter;
61
        $this->reader = $reader;
62
        $this->store = $store;
63
    }
64
65
    /**
66
     * Dispatches hooks for a specified event.
67
     *
68
     * @param HookScope $scope
69
     *
70
     * @return CallResults
71
     */
72
    public function dispatchScopeHooks(HookScope $scope)
73
    {
74
        $results = array();
75
        foreach ($this->repository->getScopeHooks($scope) as $hook) {
76
            /** @var \ReflectionMethod $function */
77
            $function = $hook->getReflection();
78
79
            // No `@ScenarioStateArgument` annotation found
80
            if (null === $this->reader->getMethodAnnotation($function, ScenarioStateArgument::class)) {
81
                $results[] = $this->callCenter->makeCall(new HookCall($scope, $hook));
82
                continue;
83
            }
84
85
            $paramsKeys = array_map(function ($element) {
86
                return $element->name;
87
            }, $function->getParameters());
88
            $store = $this->store->getStore();
89
            $params = $arguments = [];
90
91
            // Prepare arguments from annotations
92
            /** @var ScenarioStateArgument[] $annotations */
93
            $annotations = $this->reader->getMethodAnnotations($function);
94
            foreach ($annotations as $annotation) {
95
                if ($annotation instanceof ScenarioStateArgument &&
96
                    in_array($annotation->getArgument(), $paramsKeys) &&
97
                    $store->hasStateFragment($annotation->getName())
98
                ) {
99
                    $params[$annotation->getArgument()] = $store->getStateFragment($annotation->getName());
100
                }
101
            }
102
103
            // Manage `scope` argument
104
            foreach ($function->getParameters() as $parameter) {
105
                if (null !== $parameter->getClass() && get_class($scope) === $parameter->getClass()->getName()) {
0 ignored issues
show
Bug introduced by
Consider using $parameter->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
106
                    $arguments[$parameter->getName()] = $scope;
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
107
                } elseif (isset($params[$parameter->getName()])) {
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
108
                    $arguments[$parameter->getName()] = $params[$parameter->getName()];
0 ignored issues
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
109
                }
110
            }
111
112
            $results[] = $this->callCenter->makeCall(new EnvironmentCall($scope->getEnvironment(), $hook, $arguments));
113
        }
114
115
        return new CallResults($results);
116
    }
117
}
118