1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Generator\Template; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class MethodTemplate |
10
|
|
|
* @package Moka\Generator\Template |
11
|
|
|
*/ |
12
|
|
|
class MethodTemplate implements TemplateInterface |
13
|
|
|
{ |
14
|
|
|
use VisibilityTrait; |
15
|
|
|
|
16
|
|
|
private const TEMPLATE = ' |
17
|
|
|
%s %s function %s %s(%s)%s |
18
|
|
|
{ |
19
|
|
|
%s $this->__call("%s", func_get_args()); |
20
|
|
|
} |
21
|
|
|
'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \Reflector|\ReflectionMethod $method |
25
|
|
|
* @return string |
26
|
|
|
* @throws \RuntimeException |
27
|
|
|
* @throws InvalidArgumentException |
28
|
|
|
*/ |
29
|
11 |
|
public static function generate(\Reflector $method): string |
30
|
|
|
{ |
31
|
11 |
|
return static::doGenerate($method); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \ReflectionMethod $method |
36
|
|
|
* @return string |
37
|
|
|
* @throws \RuntimeException |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
11 |
|
protected static function doGenerate(\ReflectionMethod $method): string |
41
|
|
|
{ |
42
|
11 |
|
$visibility = static::getVisibility($method); |
43
|
|
|
|
44
|
11 |
|
$static = $method->isStatic() ? 'static' : ''; |
45
|
11 |
|
$reference = $method->returnsReference() ? '&' : ''; |
46
|
|
|
|
47
|
11 |
|
$parameters = $method->getParameters(); |
48
|
11 |
|
$parametersCode = []; |
49
|
11 |
|
if (\is_array($parameters)) { |
50
|
11 |
|
foreach ($parameters as $parameter) { |
51
|
10 |
|
$parametersCode[] = ParameterTemplate::generate($parameter); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
$originalReturnType = $method->getReturnType(); |
56
|
11 |
|
$returnType = $originalReturnType |
57
|
5 |
|
? ReturnTypeTemplate::generate($method) |
58
|
11 |
|
: ''; |
59
|
|
|
|
60
|
11 |
|
$returnStatement = null === $originalReturnType || 'void' !== $originalReturnType->getName() |
61
|
11 |
|
? 'return' |
62
|
11 |
|
: ''; |
63
|
|
|
|
64
|
11 |
|
$methodName = $method->name; |
65
|
|
|
|
66
|
11 |
|
return sprintf( |
67
|
11 |
|
self::TEMPLATE, |
68
|
|
|
$visibility, |
69
|
|
|
$static, |
70
|
|
|
$reference, |
71
|
|
|
$methodName, |
72
|
11 |
|
implode(',', $parametersCode), |
73
|
|
|
$returnType, |
74
|
|
|
$returnStatement, |
75
|
|
|
$methodName |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|