Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
4.55% |
3 / 66 |
| MethodDefinitionPass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
404.56 | |
4.55% |
3 / 66 |
| apply($code, MockConfiguration $config) | |
0.00% |
0 / 1 |
26.11 | |
17.65% |
3 / 17 |
|||
| renderParams(Method $method, $config) | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 18 |
|||
| appendToClass($class, $code) | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| renderMethodBody($method, $config) | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 28 |
|||
| <?php | |
| namespace Mockery\Generator\StringManipulation\Pass; | |
| use Mockery\Generator\MockConfiguration; | |
| use Mockery\Generator\Method; | |
| class MethodDefinitionPass implements Pass | |
| { | |
| public function apply($code, MockConfiguration $config) | |
| { | |
| foreach ($config->getMethodsToMock() as $method) { | |
| if ($method->isPublic()) { | |
| $methodDef = 'public'; | |
| } elseif($method->isProtected()) { | |
| $methodDef = 'protected'; | |
| } else { | |
| $methodDef = 'private'; | |
| } | |
| if ($method->isStatic()) { | |
| $methodDef .= ' static'; | |
| } | |
| $methodDef .= ' function '; | |
| $methodDef .= $method->returnsReference() ? ' & ' : ''; | |
| $methodDef .= $method->getName(); | |
| $methodDef .= $this->renderParams($method, $config); | |
| $methodDef .= $this->renderMethodBody($method, $config); | |
| $code = $this->appendToClass($code, $methodDef); | |
| } | |
| return $code; | |
| } | |
| protected function renderParams(Method $method, $config) | |
| { | |
| $class = $method->getDeclaringClass(); | |
| if ($class->isInternal()) { | |
| $overrides = $config->getParameterOverrides(); | |
| if (isset($overrides[strtolower($class->getName())][$method->getName()])) { | |
| return '(' . implode(',', $overrides[strtolower($class->getName())][$method->getName()]) . ')'; | |
| } | |
| } | |
| $methodParams = array(); | |
| $params = $method->getParameters(); | |
| foreach ($params as $param) { | |
| $paramDef = $param->getTypeHintAsString(); | |
| $paramDef .= $param->isPassedByReference() ? '&' : ''; | |
| $paramDef .= '$' . $param->getName(); | |
| if (false !== $param->isDefaultValueAvailable()) { | |
| $paramDef .= ' = ' . var_export($param->getDefaultValue(), true); | |
| } else if ($param->isOptional()) { | |
| $paramDef .= ' = null'; | |
| } | |
| $methodParams[] = $paramDef; | |
| } | |
| return '(' . implode(', ', $methodParams) . ')'; | |
| } | |
| protected function appendToClass($class, $code) | |
| { | |
| $lastBrace = strrpos($class, "}"); | |
| $class = substr($class, 0, $lastBrace) . $code . "\n }\n"; | |
| return $class; | |
| } | |
| private function renderMethodBody($method, $config) | |
| { | |
| $invoke = $method->isStatic() ? 'static::__callStatic' : '$this->__call'; | |
| $body = <<<BODY | |
| { | |
| \$argc = func_num_args(); | |
| \$argv = func_get_args(); | |
| BODY; | |
| // Fix up known parameters by reference - used func_get_args() above | |
| // in case more parameters are passed in than the function definition | |
| // says - eg varargs. | |
| $class = $method->getDeclaringClass(); | |
| $class_name = strtolower($class->getName()); | |
| $overrides = $config->getParameterOverrides(); | |
| if (isset($overrides[$class_name][$method->getName()])) { | |
| $params = array_values($overrides[$class_name][$method->getName()]); | |
| $paramCount = count($params); | |
| for ($i = 0; $i < $paramCount; ++$i) { | |
| $param = $params[$i]; | |
| if (strpos($param, '&') !== false) { | |
| $body .= <<<BODY | |
| if (\$argc > $i) { | |
| \$argv[$i] = {$param}; | |
| } | |
| BODY; | |
| } | |
| } | |
| } else { | |
| $params = array_values($method->getParameters()); | |
| $paramCount = count($params); | |
| for ($i = 0; $i < $paramCount; ++$i) { | |
| $param = $params[$i]; | |
| if (!$param->isPassedByReference()) { | |
| continue; | |
| } | |
| $body .= <<<BODY | |
| if (\$argc > $i) { | |
| \$argv[$i] =& \${$param->getName()}; | |
| } | |
| BODY; | |
| } | |
| } | |
| $body .= <<<BODY | |
| \$ret = {$invoke}(__FUNCTION__, \$argv); | |
| return \$ret; | |
| } | |
| BODY; | |
| return $body; | |
| } | |
| } |