* @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class Metadata for the original class.
* @param string|bool $fileName Filename (full path) for the generated class. If none is given, eval() is used.
*
+ * @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function generateProxyClass(ClassMetadata $class, $fileName = false)
{
+ $this->verifyClassCanBeProxied($class);
+
preg_match_all('(<([a-zA-Z]+)>)', $this->proxyClassTemplate, $placeholderMatches);
$placeholderMatches = array_combine($placeholderMatches[0], $placeholderMatches[1]);
rename($tmpFileName, $fileName);
}
+ /**
+ * @param ClassMetadata $class
+ *
+ * @throws InvalidArgumentException
+ */
+ private function verifyClassCanBeProxied(ClassMetadata $class)
+ {
+ if ($class->getReflectionClass()->isFinal()) {
+ throw InvalidArgumentException::classMustNotBeFinal($class->getName());
+ }
+
+ if ($class->getReflectionClass()->isAbstract()) {
+ throw InvalidArgumentException::classMustNotBeAbstract($class->getName());
+ }
+ }
+
/**
* Generates the proxy short class name to be used in the template.
*
$methods .= '&';
}
- $methods .= $name . '(' . $this->buildParametersString($class, $method, $method->getParameters()) . ')';
+ $methods .= $name . '(' . $this->buildParametersString($method->getParameters()) . ')';
$methods .= $this->getMethodReturnType($method);
$methods .= "\n" . ' {' . "\n";
}
/**
- * @param ClassMetadata $class
- * @param \ReflectionMethod $method
* @param \ReflectionParameter[] $parameters
*
* @return string
*/
- private function buildParametersString(ClassMetadata $class, \ReflectionMethod $method, array $parameters)
+ private function buildParametersString(array $parameters)
{
$parameterDefinitions = [];
foreach ($parameters as $param) {
$parameterDefinition = '';
- if ($parameterType = $this->getParameterType($class, $method, $param)) {
+ if ($parameterType = $this->getParameterType($param)) {
$parameterDefinition .= $parameterType . ' ';
}
$parameterDefinition .= '&';
}
- if (method_exists($param, 'isVariadic') && $param->isVariadic()) {
+ if ($param->isVariadic()) {
$parameterDefinition .= '...';
}
- $parameters[] = '$' . $param->getName();
$parameterDefinition .= '$' . $param->getName();
if ($param->isDefaultValueAvailable()) {
*
* @return string|null
*/
- private function getParameterType(ClassMetadata $class, \ReflectionMethod $method, \ReflectionParameter $parameter)
+ private function getParameterType(\ReflectionParameter $parameter)
{
- if (method_exists($parameter, 'hasType')) {
- if ( ! $parameter->hasType()) {
- return '';
- }
-
- return $this->formatType($parameter->getType(), $parameter->getDeclaringFunction(), $parameter);
- }
-
- // For PHP 5.x, we need to pick the type hint in the old way (to be removed for PHP 7.0+)
- if ($parameter->isArray()) {
- return 'array';
- }
-
- if ($parameter->isCallable()) {
- return 'callable';
+ if ( ! $parameter->hasType()) {
+ return null;
}
- try {
- $parameterClass = $parameter->getClass();
-
- if ($parameterClass) {
- return '\\' . $parameterClass->getName();
- }
- } catch (\ReflectionException $previous) {
- throw UnexpectedValueException::invalidParameterTypeHint(
- $class->getName(),
- $method->getName(),
- $parameter->getName(),
- $previous
- );
- }
-
- return null;
+ return $this->formatType($parameter->getType(), $parameter->getDeclaringFunction(), $parameter);
}
/**
function (\ReflectionParameter $parameter) {
$name = '';
- if (method_exists($parameter, 'isVariadic') && $parameter->isVariadic()) {
+ if ($parameter->isVariadic()) {
$name .= '...';
}
}
/**
- * @Param \ReflectionMethod $method
+ * @param \ReflectionMethod $method
*
* @return string
*/
private function getMethodReturnType(\ReflectionMethod $method)
{
- if ( ! method_exists($method, 'hasReturnType') || ! $method->hasReturnType()) {
+ if ( ! $method->hasReturnType()) {
return '';
}
*/
private function shouldProxiedMethodReturn(\ReflectionMethod $method)
{
- if ( ! method_exists($method, 'hasReturnType') || ! $method->hasReturnType()) {
+ if ( ! $method->hasReturnType()) {
return true;
}
\ReflectionMethod $method,
\ReflectionParameter $parameter = null
) {
- $name = method_exists($type, 'getName') ? $type->getName() : (string) $type;
+ $name = (string) $type;
$nameLower = strtolower($name);
if ('self' === $nameLower) {