1 | <?php |
||
27 | class MethodDefinitionPass implements Pass |
||
28 | { |
||
29 | 407 | public function apply($code, MockConfiguration $config) |
|
30 | { |
||
31 | 407 | foreach ($config->getMethodsToMock() as $method) { |
|
32 | 117 | if ($method->isPublic()) { |
|
33 | 113 | $methodDef = 'public'; |
|
34 | 11 | } elseif ($method->isProtected()) { |
|
35 | 11 | $methodDef = 'protected'; |
|
36 | } else { |
||
37 | 2 | $methodDef = 'private'; |
|
38 | } |
||
39 | |||
40 | 117 | if ($method->isStatic()) { |
|
41 | 8 | $methodDef .= ' static'; |
|
42 | } |
||
43 | |||
44 | 117 | $methodDef .= ' function '; |
|
45 | 117 | $methodDef .= $method->returnsReference() ? ' & ' : ''; |
|
46 | 117 | $methodDef .= $method->getName(); |
|
47 | 117 | $methodDef .= $this->renderParams($method, $config); |
|
48 | 117 | $methodDef .= $this->renderReturnType($method); |
|
49 | 117 | $methodDef .= $this->renderMethodBody($method, $config); |
|
50 | |||
51 | 117 | $code = $this->appendToClass($code, $methodDef); |
|
52 | } |
||
53 | |||
54 | 407 | return $code; |
|
55 | } |
||
56 | |||
57 | 117 | protected function renderParams(Method $method, $config) |
|
58 | { |
||
59 | 117 | $class = $method->getDeclaringClass(); |
|
60 | 117 | if ($class->isInternal()) { |
|
61 | 21 | $overrides = $config->getParameterOverrides(); |
|
62 | |||
63 | 21 | if (isset($overrides[strtolower($class->getName())][$method->getName()])) { |
|
64 | 2 | return '(' . implode(',', $overrides[strtolower($class->getName())][$method->getName()]) . ')'; |
|
65 | } |
||
66 | } |
||
67 | |||
68 | 117 | $methodParams = array(); |
|
69 | 117 | $params = $method->getParameters(); |
|
70 | 117 | foreach ($params as $param) { |
|
71 | 48 | $paramDef = $this->renderTypeHint($param); |
|
72 | 48 | $paramDef .= $param->isPassedByReference() ? '&' : ''; |
|
73 | 48 | $paramDef .= $param->isVariadic() ? '...' : ''; |
|
74 | 48 | $paramDef .= '$' . $param->getName(); |
|
75 | |||
76 | 48 | if (!$param->isVariadic()) { |
|
77 | 48 | if (false !== $param->isDefaultValueAvailable()) { |
|
78 | 5 | $paramDef .= ' = ' . var_export($param->getDefaultValue(), true); |
|
79 | 47 | } elseif ($param->isOptional()) { |
|
80 | 12 | $paramDef .= ' = null'; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 48 | $methodParams[] = $paramDef; |
|
85 | } |
||
86 | 117 | return '(' . implode(', ', $methodParams) . ')'; |
|
87 | } |
||
88 | |||
89 | 117 | protected function renderReturnType(Method $method) |
|
90 | { |
||
91 | 117 | $type = $method->getReturnType(); |
|
92 | 117 | return $type ? sprintf(': %s', $type) : ''; |
|
93 | } |
||
94 | |||
95 | 117 | protected function appendToClass($class, $code) |
|
101 | |||
102 | 48 | protected function renderTypeHint(Parameter $param) |
|
103 | { |
||
104 | $languageTypeHints = array( |
||
105 | 48 | 'self', |
|
106 | 'array', |
||
107 | 'callable', |
||
108 | // Up to php 7 |
||
109 | 'bool', |
||
110 | 'float', |
||
111 | 'int', |
||
112 | 'string', |
||
113 | 'iterable', |
||
114 | ); |
||
115 | 48 | $typeHint = trim($param->getTypeHintAsString()); |
|
116 | |||
117 | 48 | if (!empty($typeHint)) { |
|
118 | |||
119 | 19 | if (!in_array($typeHint, $languageTypeHints)) { |
|
120 | 6 | $typeHint = '\\'.$typeHint; |
|
121 | } |
||
122 | |||
123 | 19 | if (version_compare(PHP_VERSION, '7.1.0-dev') >= 0 && $param->allowsNull()) { |
|
|
|||
124 | $typeHint = "?".$typeHint; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | 48 | return $typeHint .= ' '; |
|
129 | } |
||
130 | |||
131 | 117 | private function renderMethodBody($method, $config) |
|
187 | } |
||
188 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: