/**
* Generates decorated methods by picking those available in the parent class.
*
* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class
*
* @return string
*/
private function generateMethods(ClassMetadata $class)
{
$methods = '';
$methodNames = array();
$reflectionMethods = $class->getReflectionClass()->getMethods(\ReflectionMethod::IS_PUBLIC);
$skippedMethods = array('__sleep' => true, '__clone' => true, '__wakeup' => true, '__get' => true, '__set' => true, '__isset' => true);
foreach ($reflectionMethods as $method) {
$name = $method->getName();
if ($method->isConstructor() || isset($skippedMethods[strtolower($name)]) || isset($methodNames[$name]) || $method->isFinal() || $method->isStatic() || !$method->isPublic()) {
continue;
}
$methodNames[$name] = true;
$methods .= "\n /**\n" . " * {@inheritDoc}\n" . " */\n" . ' public function ';
if ($method->returnsReference()) {
$methods .= '&';
}
$methods .= $name . '(';
$firstParam = true;
$parameterString = '';
$argumentString = '';
$parameters = array();
foreach ($method->getParameters() as $param) {
if ($firstParam) {
$firstParam = false;
} else {
$parameterString .= ', ';
$argumentString .= ', ';
}
try {
$paramClass = $param->getClass();
} catch (\ReflectionException $previous) {
throw UnexpectedValueException::invalidParameterTypeHint($class->getName(), $method->getName(), $param->getName(), $previous);
}
// We need to pick the type hint class too
if (null !== $paramClass) {
$parameterString .= '\\' . $paramClass->getName() . ' ';
} elseif ($param->isArray()) {
$parameterString .= 'array ';
} elseif (method_exists($param, 'isCallable') && $param->isCallable()) {
$parameterString .= 'callable ';
}
if ($param->isPassedByReference()) {
$parameterString .= '&';
}
$parameters[] = '$' . $param->getName();
$parameterString .= '$' . $param->getName();
$argumentString .= '$' . $param->getName();
if ($param->isDefaultValueAvailable()) {
$parameterString .= ' = ' . var_export($param->getDefaultValue(), true);
}
}
$methods .= $parameterString . ')';
$methods .= "\n" . ' {' . "\n";
if ($this->isShortIdentifierGetter($method, $class)) {
$identifier = lcfirst(substr($name, 3));
$fieldType = $class->getTypeOfField($identifier);
$cast = in_array($fieldType, array('integer', 'smallint')) ? '(int) ' : '';
$methods .= ' if ($this->__isInitialized__ === false) {' . "\n";
$methods .= ' return ' . $cast . ' parent::' . $method->getName() . "();\n";
$methods .= ' }' . "\n\n";
}
$methods .= "\n \$this->__initializer__ " . "&& \$this->__initializer__->__invoke(\$this, " . var_export($name, true) . ", array(" . implode(', ', $parameters) . "));" . "\n\n return parent::" . $name . '(' . $argumentString . ');' . "\n" . ' }' . "\n";
}
return $methods;
}