Completed
Pull Request — master (#82)
by Greg
02:18
created

InitializeHookDispatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 34
loc 34
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 9 9 2
A callInitializeHook() 9 9 3
A getInitializeHooks() 11 11 1

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
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
4
5
use Consolidation\AnnotatedCommand\Hooks\HookManager;
6
use Consolidation\AnnotatedCommand\AnnotationData;
7
use Consolidation\AnnotatedCommand\Hooks\InitializeHookInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
11
/**
12
 * Call hooks
13
 */
14 View Code Duplication
class InitializeHookDispatcher extends HookDispatcher implements InitializeHookInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    public function initialize(
17
        InputInterface $input,
18
        AnnotationData $annotationData
19
    ) {
20
        $providers = $this->getInitializeHooks($annotationData);
21
        foreach ($providers as $provider) {
22
            $this->callInitializeHook($provider, $input, $annotationData);
23
        }
24
    }
25
26
    protected function callInitializeHook($provider, $input, AnnotationData $annotationData)
27
    {
28
        if ($provider instanceof InitializeHookInterface) {
29
            return $provider->initialize($input, $annotationData);
30
        }
31
        if (is_callable($provider)) {
32
            return $provider($input, $annotationData);
33
        }
34
    }
35
36
    protected function getInitializeHooks(AnnotationData $annotationData)
37
    {
38
        return $this->getHooks(
39
            [
40
                HookManager::PRE_INITIALIZE,
41
                HookManager::INITIALIZE,
42
                HookManager::POST_INITIALIZE
43
            ],
44
            $annotationData
45
        );
46
    }
47
}
48