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

ProcessResultHookDispatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 19.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 3
dl 12
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 13 3
A callProcessor() 0 14 4
A getProcessResultHooks() 0 11 1
A getAlterResultHooks() 12 12 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\AnnotationData;
6
use Consolidation\AnnotatedCommand\CommandData;
7
use Consolidation\AnnotatedCommand\Hooks\HookManager;
8
use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface;
9
10
/**
11
 * Call hooks
12
 */
13
class ProcessResultHookDispatcher extends HookDispatcher implements ProcessResultInterface
14
{
15
    /**
16
     * Process result and decide what to do with it.
17
     * Allow client to add transformation / interpretation
18
     * callbacks.
19
     */
20
    public function process($result, CommandData $commandData)
21
    {
22
        $processors = $this->getProcessResultHooks($commandData->annotationData());
23
        foreach ($processors as $processor) {
24
            $result = $this->callProcessor($processor, $result, $commandData);
25
        }
26
        $alterers = $this->getAlterResultHooks($commandData->annotationData());
27
        foreach ($alterers as $alterer) {
28
            $result = $this->callProcessor($alterer, $result, $commandData);
29
        }
30
31
        return $result;
32
    }
33
34
    protected function callProcessor($processor, $result, CommandData $commandData)
35
    {
36
        $processed = null;
37
        if ($processor instanceof ProcessResultInterface) {
38
            $processed = $processor->process($result, $commandData);
39
        }
40
        if (is_callable($processor)) {
41
            $processed = $processor($result, $commandData);
42
        }
43
        if (isset($processed)) {
44
            return $processed;
45
        }
46
        return $result;
47
    }
48
49
    protected function getProcessResultHooks(AnnotationData $annotationData)
50
    {
51
        return $this->getHooks(
52
            [
53
                HookManager::PRE_PROCESS_RESULT,
54
                HookManager::PROCESS_RESULT,
55
                HookManager::POST_PROCESS_RESULT
56
            ],
57
            $annotationData
58
        );
59
    }
60
61 View Code Duplication
    protected function getAlterResultHooks(AnnotationData $annotationData)
0 ignored issues
show
Duplication introduced by
This method 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...
62
    {
63
        return $this->getHooks(
64
            [
65
                HookManager::PRE_ALTER_RESULT,
66
                HookManager::ALTER_RESULT,
67
                HookManager::POST_ALTER_RESULT,
68
                HookManager::POST_COMMAND_HOOK,
69
            ],
70
            $annotationData
71
        );
72
    }
73
}
74