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) { |
|
|
|
|
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() { |
|
|
|
|
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
|
|
|
} |
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.