/** * Type string * * @param string $string * @throws \InvalidArgumentException * @return ArrayType|BoolType|CallableType|FloatType|IntType|MixedType|ResourceType|StringType|Type */ public static function fromString($string) { $lowercaseString = trim(strtolower($string), "\\"); // TODO: variant types if ($lowercaseString === "mixed") { return MixedType::getInstance(); } elseif ($lowercaseString === "scalar") { return ScalarType::getInstance(); } elseif ($lowercaseString === "object") { return ObjectType::getInstance(); } elseif ($lowercaseString === "void" || $lowercaseString === "null") { return VoidType::getInstance(); } elseif ($lowercaseString === "numeric" || $lowercaseString === "number") { return NumericType::getInstance(); } elseif ($lowercaseString === "int" || $lowercaseString === "integer") { return IntType::getInstance(); } elseif ($lowercaseString === "float" || $lowercaseString === "double") { return FloatType::getInstance(); } elseif ($lowercaseString === "bool" || $lowercaseString === "boolean") { return BoolType::getInstance(); } elseif ($lowercaseString === "string") { return StringType::getInstance(); } elseif ($lowercaseString === "resource" || $lowercaseString === "stream") { return ResourceType::getInstance(); } elseif ($lowercaseString === "callable" || $lowercaseString === "callback" || trim($lowercaseString, "\\") === "closure") { return CallableType::getInstance(); } elseif (strncmp($lowercaseString, "array", 5) === 0) { return ArrayType::create(MixedType::getInstance()); } else { if (func_num_args() > 1) { $stack = func_get_arg(1); } else { $stack = new \ArrayObject(); } if (func_num_args() > 2) { $reader = func_get_arg(2); } else { $reader = new AnnotationReader(); } if (func_num_args() > 3) { $phpParser = func_get_arg(3); } else { $phpParser = new PhpParser(); } if (func_num_args() > 4 && func_get_arg(4) !== null) { /** @var Type $declaringType */ $declaringType = func_get_arg(4); $useStatements = $declaringType->getUseStatements(); } else { $declaringType = null; $useStatements = array(); } if ($lowercaseString === "\$this" || $lowercaseString === "self" || $lowercaseString === "static") { if ($declaringType === null) { throw new \InvalidArgumentException("Type string references declaring class, but no declaring class given."); } return $declaringType; } elseif (substr($string, -2) === "[]") { $baseString = substr($string, 0, strlen($string) - 2); return ArrayType::create(MixedType::fromString($baseString, $stack, $reader, $phpParser, $declaringType)); } else { if ($string[0] === "\\") { $typeName = trim($string, "\\"); } elseif (isset($useStatements[$lowercaseString])) { $typeName = $useStatements[$lowercaseString]; // TODO: `use` with namespace (e.g. `use Doctrine\Mapping as ORM;`) } elseif ($declaringType !== null) { $typeName = $declaringType->getNamespaceName() . "\\" . $string; } else { $typeName = $string; } return Type::fromReflection(new \ReflectionClass($typeName), $stack, $reader, $phpParser); } } }
public static function fromReflection(ReflectionParameter $reflection = NULL) { if (!defined('PHP_VERSION_ID')) { $v = explode('.', PHP_VERSION); define('PHP_VERSION_ID', $v[0] * 10000 + $v[1] * 100 + $v[2]); } if ($reflection === null) { return null; } if (func_num_args() > 1) { $stack = func_get_arg(1); } else { $stack = new \ArrayObject(); } $stackExpression = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getDeclaringFunction()->getName() . '(' . $reflection->getPosition() . ')'; if (isset($stack[$stackExpression])) { return $stack[$stackExpression]; } $stack[$stackExpression] = $instance = new Parameter($reflection); if (func_num_args() > 2) { $reader = func_get_arg(2); } else { $reader = new AnnotationReader(); } if (func_num_args() > 3) { $phpParser = func_get_arg(3); } else { $phpParser = new PhpParser(); } $instance->name = $reflection->getName(); $instance->passedByReference = $reflection->isPassedByReference(); $instance->array = $reflection->isArray(); $instance->callable = PHP_VERSION_ID >= 50400 ? $reflection->isCallable() : null; $instance->position = $reflection->getPosition(); $instance->optional = $reflection->isOptional(); $instance->defaultValueAvailable = $reflection->isDefaultValueAvailable(); $instance->defaultValue = $reflection->isDefaultValueAvailable() ? $reflection->getDefaultValue() : null; $instance->defaultValueConstant = PHP_VERSION_ID >= 50500 && $reflection->isDefaultValueAvailable() ? $reflection->isDefaultValueConstant() : null; $instance->declaringFunction = Method::fromReflection($reflection->getDeclaringFunction() ? $reflection->getDeclaringFunction() : null, $stack, $reader, $phpParser); $instance->declaringClass = Type::fromReflection($reflection->getDeclaringClass() ? $reflection->getDeclaringClass() : null, $stack, $reader, $phpParser); if (preg_match('/@param\\s+([a-zA-Z0-9\\\\\\[\\]_]+)\\s+\\$' . preg_quote($instance->name) . '/', $instance->declaringFunction->getDocComment(), $m)) { $typeString = $m[1]; } if (isset($typeString)) { $instance->type = MixedType::fromString($typeString, $stack, $reader, $phpParser, $instance->declaringClass); } elseif ($reflection->getClass()) { $instance->type = Type::fromReflection($reflection->getClass(), $stack, $reader, $phpParser, $instance->declaringClass); } elseif ($reflection->isArray()) { $instance->type = MixedType::fromString('array', $stack, $reader, $phpParser, $instance->declaringClass); } else { $instance->type = MixedType::getInstance(); } return $instance; }
public static function fromReflection(ReflectionMethod $reflection = NULL) { if (!defined('PHP_VERSION_ID')) { $v = explode('.', PHP_VERSION); define('PHP_VERSION_ID', $v[0] * 10000 + $v[1] * 100 + $v[2]); } if ($reflection === null) { return null; } if (func_num_args() > 1) { $stack = func_get_arg(1); } else { $stack = new \ArrayObject(); } $stackExpression = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName(); if (isset($stack[$stackExpression])) { return $stack[$stackExpression]; } $stack[$stackExpression] = $instance = new Method($reflection); if (func_num_args() > 2) { $reader = func_get_arg(2); } else { $reader = new AnnotationReader(); } if (func_num_args() > 3) { $phpParser = func_get_arg(3); } else { $phpParser = new PhpParser(); } $instance->public = $reflection->isPublic(); $instance->private = $reflection->isPrivate(); $instance->protected = $reflection->isProtected(); $instance->abstract = $reflection->isAbstract(); $instance->final = $reflection->isFinal(); $instance->static = $reflection->isStatic(); $instance->constructor = $reflection->isConstructor(); $instance->destructor = $reflection->isDestructor(); $instance->modifiers = $reflection->getModifiers(); $instance->closure = $reflection->isClosure(); $instance->deprecated = $reflection->isDeprecated(); $instance->internal = $reflection->isInternal(); $instance->userDefined = $reflection->isUserDefined(); $instance->generator = PHP_VERSION_ID >= 50500 ? $reflection->isGenerator() : null; $instance->docComment = $reflection->getDocComment(); $instance->endLine = $reflection->getEndLine(); $instance->extensionName = $reflection->getExtensionName(); $instance->fileName = $reflection->getFileName(); $instance->name = $reflection->getName(); $instance->namespaceName = $reflection->getNamespaceName(); $instance->numberOfParameters = $reflection->getNumberOfParameters(); $instance->numberOfRequiredParameters = $reflection->getNumberOfRequiredParameters(); $instance->shortName = $reflection->getShortName(); $instance->startLine = $reflection->getStartLine(); $instance->staticVariables = $reflection->getStaticVariables(); $instance->annotations = $reader->getMethodAnnotations($reflection); $instance->declaringClass = Type::fromReflection($reflection->getDeclaringClass() ? $reflection->getDeclaringClass() : null, $stack, $reader, $phpParser); if (preg_match('/@return\\s+([a-zA-Z0-9\\\\\\[\\]_]+)/', $instance->docComment, $m)) { $typeString = $m[1]; } if (isset($typeString)) { $instance->type = MixedType::fromString($typeString, $stack, $reader, $phpParser, $instance->declaringClass); } else { $instance->type = VoidType::getInstance(); } return $instance; }
public static function fromReflection(ReflectionProperty $reflection = NULL) { if (!defined('PHP_VERSION_ID')) { $v = explode('.', PHP_VERSION); define('PHP_VERSION_ID', $v[0] * 10000 + $v[1] * 100 + $v[2]); } if ($reflection === null) { return null; } if (func_num_args() > 1) { $stack = func_get_arg(1); } else { $stack = new \ArrayObject(); } $stackExpression = $reflection->getDeclaringClass()->getName() . '::' . $reflection->getName(); if (isset($stack[$stackExpression])) { return $stack[$stackExpression]; } $stack[$stackExpression] = $instance = new Property($reflection); if (func_num_args() > 2) { $reader = func_get_arg(2); } else { $reader = new AnnotationReader(); } if (func_num_args() > 3) { $phpParser = func_get_arg(3); } else { $phpParser = new PhpParser(); } $instance->name = $reflection->getName(); $instance->public = $reflection->isPublic(); $instance->private = $reflection->isPrivate(); $instance->protected = $reflection->isProtected(); $instance->static = $reflection->isStatic(); $instance->default = $reflection->isDefault(); $instance->modifiers = $reflection->getModifiers(); $instance->docComment = $reflection->getDocComment(); $instance->annotations = $reader->getPropertyAnnotations($reflection); $instance->declaringClass = Type::fromReflection($reflection->getDeclaringClass() ? $reflection->getDeclaringClass() : null, $stack, $reader, $phpParser); $defaultProperties = $reflection->getDeclaringClass()->getDefaultProperties(); if (isset($defaultProperties[$instance->name])) { $instance->defaultValue = $defaultProperties[$instance->name]; } if (preg_match('/@var\\s+([a-zA-Z0-9\\\\\\[\\]_]+)/', $instance->docComment, $m)) { $typeString = $m[1]; } if (isset($typeString)) { $instance->type = MixedType::fromString($typeString, $stack, $reader, $phpParser, $instance->declaringClass); } else { $instance->type = MixedType::getInstance(); } return $instance; }