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

GenericObjectAction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 16.3 %

Coupling/Cohesion

Components 5
Dependencies 2

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 16
c 6
b 0
f 2
lcom 5
cbo 2
dl 15
loc 92
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCaption() 0 4 1
A caption() 0 3 2
A setDescription() 0 4 1
A description() 0 3 2
A setExecute() 0 4 1
A setFill() 0 4 1
A setAfterExecute() 0 6 1
A mapParameter() 0 4 1
A parameters() 8 8 2
A executeWith() 0 3 1
A fill() 7 7 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
use rtens\domin\reflection\types\TypeFactory;
6
7
class GenericObjectAction extends ObjectAction {
8
9
    private $execute;
10
    private $fill;
11
    private $caption;
12
    private $description;
13
    private $paramMap = [];
14
15
    public function __construct($class, TypeFactory $types, CommentParser $parser, callable $execute) {
16
        parent::__construct($class, $types, $parser);
17
        $this->execute = $execute;
18
    }
19
20
    public function setCaption($caption) {
21
        $this->caption = $caption;
22
        return $this;
23
    }
24
25
    public function caption() {
26
        return $this->caption ?: parent::caption();
27
    }
28
29
    public function setDescription($description) {
30
        $this->description = $description;
31
        return $this;
32
    }
33
34
    public function description() {
35
        return $this->description ?: parent::description();
36
    }
37
38
    public function setExecute(callable $execute) {
39
        $this->execute = $execute;
40
        return $this;
41
    }
42
43
    public function setFill(callable $fill) {
44
        $this->fill = $fill;
45
        return $this;
46
    }
47
48
    /**
49
     * @param callable $callback Filters the return value of execute
50
     */
51
    public function setAfterExecute(callable $callback) {
52
        $oldExecute = $this->execute;
53
        $this->execute = function ($object) use ($oldExecute, $callback) {
54
            return $callback(call_user_func($oldExecute, $object));
55
        };
56
    }
57
58
    /**
59
     * @param string $name
60
     * @param callable $map Receives Parameter and returns Parameter
61
     * @return static
62
     */
63
    public function mapParameter($name, callable $map) {
64
        $this->paramMap[$name] = $map;
65
        return $this;
66
    }
67
68
    /**
69
     * @return \rtens\domin\Parameter[]
70
     * @throws \Exception
71
     */
72 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...
73
        return array_map(function (Parameter $parameter) {
74
            if (array_key_exists($parameter->getName(), $this->paramMap)) {
75
                return call_user_func($this->paramMap[$parameter->getName()], $parameter);
76
            }
77
            return $parameter;
78
        }, parent::parameters());
79
    }
80
81
    /**
82
     * Called by execute() with the instantiated object
83
     *
84
     * @param object $object
85
     * @return mixed
86
     */
87
    protected function executeWith($object) {
88
        return call_user_func($this->execute, $object);
89
    }
90
91 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...
92
        $parameters = parent::fill($parameters);
93
        if ($this->fill) {
94
            return call_user_func($this->fill, $parameters);
95
        }
96
        return $parameters;
97
    }
98
}