Repository navigation
Expand file tree
/
Copy pathActor.php
More file actions
194 lines (164 loc) · 5.21 KB
/
Copy pathActor.php
File metadata and controls
194 lines (164 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Codeception\Lib\Generator;
use Codeception\Configuration;
use Codeception\Lib\Di;
use Codeception\Lib\ModuleContainer;
use Codeception\Util\Template;
class Actor
{
protected $template = <<<EOF
<?php
{{hasNamespace}}
/**
* Inherited Methods
{{inheritedMethods}}
*
* @SuppressWarnings(PHPMD)
*/
class {{actor}} extends \Codeception\Actor
{
use _generated\{{actor}}Actions;
/**
* Define custom actions here
*/
}
EOF;
protected $inheritedMethodTemplate = ' * @method {{return}} {{method}}({{params}})';
protected $settings;
protected $modules;
protected $actions;
public function __construct($settings)
{
$this->settings = $settings;
$this->di = new Di();
$this->moduleContainer = new ModuleContainer($this->di, $settings);
$modules = Configuration::modules($this->settings);
foreach ($modules as $moduleName) {
$this->moduleContainer->create($moduleName);
}
$this->modules = $this->moduleContainer->all();
$this->actions = $this->moduleContainer->getActions();
}
public function produce()
{
$namespace = rtrim($this->settings['namespace'], '\\');
return (new Template($this->template))
->place('hasNamespace', $namespace ? "namespace $namespace;" : '')
->place('actor', $this->settings['class_name'])
->place('inheritedMethods', $this->prependAbstractActorDocBlocks())
->produce();
}
protected function prependAbstractActorDocBlocks()
{
$inherited = [];
$class = new \ReflectionClass('\Codeception\\Actor');
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
if ($method->name == '__call') {
continue;
} // skipping magic
if ($method->name == '__construct') {
continue;
} // skipping magic
$returnType = 'void';
if ($method->name == 'haveFriend') {
$returnType = '\Codeception\Lib\Friend';
}
$params = $this->getParamsString($method);
$inherited[] = (new Template($this->inheritedMethodTemplate))
->place('method', $method->name)
->place('params', $params)
->place('return', $returnType)
->produce();
}
return implode("\n", $inherited);
}
/**
* @param \ReflectionMethod $refMethod
* @return array
*/
protected function getParamsString(\ReflectionMethod $refMethod)
{
$params = [];
foreach ($refMethod->getParameters() as $param) {
if ($param->isOptional()) {
$params[] = '$' . $param->name . ' = '.$this->getDefaultValue($param);
} else {
$params[] = '$' . $param->name;
};
}
return implode(', ', $params);
}
public function getActorName()
{
return $this->settings['class_name'];
}
public function getModules()
{
return array_keys($this->modules);
}
/**
* Infer default parameter from the reflection object and format it as PHP (code) string
*
* @param \ReflectionParameter $param
*
* @return string
*/
private function getDefaultValue(\ReflectionParameter $param)
{
if ($param->isDefaultValueAvailable()) {
if (method_exists($param, 'isDefaultValueConstant') && $param->isDefaultValueConstant()) {
$constName = $param->getDefaultValueConstantName();
if (false !== strpos($constName, '::')) {
list($class, $const) = explode('::', $constName);
if (in_array($class, ['self', 'static'])) {
$constName = $param->getDeclaringClass()->getName().'::'.$const;
}
}
return $constName;
}
return $this->phpEncodeValue($param->getDefaultValue());
}
return 'null';
}
/**
* PHP encoded a value
*
* @param mixed $value
*
* @return string
*/
private function phpEncodeValue($value)
{
if (is_array($value)) {
return $this->phpEncodeArray($value);
}
if (is_string($value)) {
return json_encode($value);
}
return var_export($value, true);
}
/**
* Recursively PHP encode an array
*
* @param array $array
*
* @return string
*/
private function phpEncodeArray(array $array)
{
$isPlainArray = function (array $value) {
return ((count($value) === 0)
|| (
(array_keys($value) === range(0, count($value) - 1))
&& (0 === count(array_filter(array_keys($value), 'is_string'))))
);
};
if ($isPlainArray($array)) {
return '['.implode(', ', array_map([$this, 'phpEncodeValue'], $array)).']';
}
return '['.implode(', ', array_map(function ($key) use ($array) {
return $this->phpEncodeValue($key).' => '.$this->phpEncodeValue($array[$key]);
}, array_keys($array))).']';
}
}