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
05:11
created

ScenarioStateHookDispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 9
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C dispatchScopeHooks() 9 38 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Hook\Call\HookCall;
17
use Behat\Testwork\Hook\HookRepository;
18
use Behat\Testwork\Hook\Scope\HookScope;
19
use Doctrine\Common\Annotations\Reader;
20
use Gorghoa\ScenarioStateBehatExtension\Annotation\ScenarioStateArgument;
21
use Gorghoa\ScenarioStateBehatExtension\Context\Initializer\ScenarioStateInitializer;
22
use Gorghoa\ScenarioStateBehatExtension\Hook\Call\ScenarioStateCall;
23
24
/**
25
 * @author Vincent Chalamon <[email protected]>
26
 */
27
final 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
//            $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) {
0 ignored issues
show
Unused Code introduced by
$paramsKeys is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
                return $element->name;
89
            }, $function->getParameters());
90
            var_dump($function->getParameters());die;
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...
Coding Style Compatibility introduced by
The method dispatchScopeHooks() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
91
            $store = $this->store->getStore();
0 ignored issues
show
Unused Code introduced by
$store = $this->store->getStore(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
92
93
            /** @var ScenarioStateArgument[] $annotations */
94
            $annotations = $this->reader->getMethodAnnotations($function);
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());
101
                    $match[strval(++$i)] = $store->getStateFragment($annotation->getName());
102
                }
103
            }
104
105
            $results[] = $this->callCenter->makeCall(new ScenarioStateCall($scope, $hook, ['foo', 'bar']));
106
        }
107
108
        return new CallResults($results);
109
    }
110
}
111