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

ValidateHookDispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 32.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 13
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 4
A callValidator() 0 9 3
A getValidators() 13 13 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\CommandError;
8
use Consolidation\AnnotatedCommand\Hooks\HookManager;
9
use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface;
10
11
/**
12
 * Call hooks
13
 */
14
class ValidateHookDispatcher extends HookDispatcher implements ValidatorInterface
15
{
16
    public function validate(CommandData $commandData)
17
    {
18
        $validators = $this->getValidators($commandData->annotationData());
19
        foreach ($validators as $validator) {
20
            $validated = $this->callValidator($validator, $commandData);
21
            if ($validated === false) {
22
                return new CommandError();
23
            }
24
            if (is_object($validated)) {
25
                return $validated;
26
            }
27
        }
28
    }
29
30
    protected function callValidator($validator, CommandData $commandData)
31
    {
32
        if ($validator instanceof ValidatorInterface) {
33
            return $validator->validate($commandData);
34
        }
35
        if (is_callable($validator)) {
36
            return $validator($commandData);
37
        }
38
    }
39
40 View Code Duplication
    protected function getValidators(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...
41
    {
42
        return $this->getHooks(
43
            [
44
                HookManager::PRE_ARGUMENT_VALIDATOR,
45
                HookManager::ARGUMENT_VALIDATOR,
46
                HookManager::POST_ARGUMENT_VALIDATOR,
47
                HookManager::PRE_COMMAND_HOOK,
48
                HookManager::COMMAND_HOOK,
49
            ],
50
            $annotationData
51
        );
52
    }
53
}
54