Completed
Push — master ( e3df42...7db57b )
by Nikolas
03:09
created

GenericMethodAction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 18.99 %

Coupling/Cohesion

Components 5
Dependencies 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
c 4
b 0
f 1
lcom 5
cbo 2
dl 15
loc 79
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setAfterExecute() 0 4 1
A execute() 0 7 2
A setCaption() 0 4 1
A caption() 0 3 2
A setDescription() 0 4 1
A description() 0 3 2
A setFill() 0 4 1
A fill() 7 7 2
A mapParameter() 0 4 1
A parameters() 8 8 2

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
namespace rtens\domin\reflection;
3
4
use rtens\domin\Parameter;
5
6
class GenericMethodAction extends MethodAction {
7
8
    private $afterExecute;
9
    private $caption;
10
    private $fill;
11
    private $description;
12
    private $paramMap = [];
13
14
    /**
15
     * @param callable $callback Filter for return value of execute()
16
     * @return static
17
     */
18
    public function setAfterExecute(callable $callback) {
19
        $this->afterExecute = $callback;
20
        return $this;
21
    }
22
23
    public function execute(array $parameters) {
24
        $return = parent::execute($parameters);
25
        if ($this->afterExecute) {
26
            $return = call_user_func($this->afterExecute, $return);
27
        }
28
        return $return;
29
    }
30
31
    public function setCaption($caption) {
32
        $this->caption = $caption;
33
        return $this;
34
    }
35
36
    public function caption() {
37
        return $this->caption ?: parent::caption();
38
    }
39
40
    public function setDescription($description) {
41
        $this->description = $description;
42
        return $this;
43
    }
44
45
    public function description() {
46
        return $this->description ?: parent::description();
47
    }
48
49
    public function setFill(callable $callback) {
50
        $this->fill = $callback;
51
        return $this;
52
    }
53
54 View Code Duplication
    public function fill(array $parameters) {
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...
55
        $parameters = parent::fill($parameters);
56
        if ($this->fill) {
57
            $parameters = call_user_func($this->fill, $parameters);
58
        }
59
        return $parameters;
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param callable $map Receives Parameter and returns Parameter
65
     * @return static
66
     */
67
    public function mapParameter($name, callable $map) {
68
        $this->paramMap[$name] = $map;
69
        return $this;
70
    }
71
72
    /**
73
     * @return \rtens\domin\Parameter[]
74
     * @throws \Exception
75
     */
76 View Code Duplication
    public function parameters() {
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...
77
        return array_map(function (Parameter $parameter) {
78
            if (array_key_exists($parameter->getName(), $this->paramMap)) {
79
                return call_user_func($this->paramMap[$parameter->getName()], $parameter);
80
            }
81
            return $parameter;
82
        }, parent::parameters());
83
    }
84
}