* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\PropertyInfo\Util; use phpDocumentor\Reflection\Type as DocType; use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Collection; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Null_; use phpDocumentor\Reflection\Types\Nullable; use Symfony\Component\PropertyInfo\Type; /** * Transforms a php doc type to a {@link Type} instance. * * @author Kévin Dunglas * @author Guilhem N. */ final class PhpDocTypeHelper { /** * Creates a {@see Type} from a PHPDoc type. * * @return Type[] */ public function getTypes(DocType $varType): array { $types = []; $nullable = false; if ($varType instanceof Nullable) { $nullable = true; $varType = $varType->getActualType(); } if (!$varType instanceof Compound) { if ($varType instanceof Null_) { $nullable = true; } $type = $this->createType($varType, $nullable); if (null !== $type) { $types[] = $type; } return $types; } $varTypes = []; for ($typeIndex = 0; $varType->has($typeIndex); ++$typeIndex) { $type = $varType->get($typeIndex); // If null is present, all types are nullable if ($type instanceof Null_) { $nullable = true; continue; } if ($type instanceof Nullable) { $nullable = true; $type = $type->getActualType(); } $varTypes[] = $type; } foreach ($varTypes as $varType) { $type = $this->createType($varType, $nullable); if (null !== $type) { $types[] = $type; } } return $types; } /** * Creates a {@see Type} from a PHPDoc type. */ private function createType(DocType $type, bool $nullable, string $docType = null): ?Type { $docType = $docType ?? (string) $type; if ($type instanceof Collection) { [$phpType, $class] = $this->getPhpTypeAndClass((string) $type->getFqsen()); $key = $this->getTypes($type->getKeyType()); $value = $this->getTypes($type->getValueType()); // More than 1 type returned means it is a Compound type, which is // not handled by Type, so better use a null value. $key = 1 === \count($key) ? $key[0] : null; $value = 1 === \count($value) ? $value[0] : null; return new Type($phpType, $nullable, $class, true, $key, $value); } // Cannot guess if (!$docType || 'mixed' === $docType) { return null; } if ('[]' === substr($docType, -2)) { $collectionKeyType = new Type(Type::BUILTIN_TYPE_INT); $collectionValueType = $this->createType($type, false, substr($docType, 0, -2)); return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType); } if (0 === strpos($docType, 'array<') && $type instanceof Array_) { // array is converted to x[] which is handled above // so it's only necessary to handle array here $collectionKeyType = $this->getTypes($type->getKeyType())[0]; $collectionValueTypes = $this->getTypes($type->getValueType()); if (1 != \count($collectionValueTypes)) { // the Type class does not support union types yet, so assume that no type was defined $collectionValueType = null; } else { $collectionValueType = $collectionValueTypes[0]; } return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType); } $docType = $this->normalizeType($docType); [$phpType, $class] = $this->getPhpTypeAndClass($docType); if ('array' === $docType) { return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, null, null); } return new Type($phpType, $nullable, $class); } private function normalizeType(string $docType): string { switch ($docType) { case 'integer': return 'int'; case 'boolean': return 'bool'; // real is not part of the PHPDoc standard, so we ignore it case 'double': return 'float'; case 'callback': return 'callable'; case 'void': return 'null'; default: return $docType; } } private function getPhpTypeAndClass(string $docType): array { if (\in_array($docType, Type::$builtinTypes)) { return [$docType, null]; } if (\in_array($docType, ['parent', 'self', 'static'], true)) { return ['object', $docType]; } return ['object', substr($docType, 1)]; // substr to strip the namespace's `\`-prefix } } __halt_compiler();----SIGNATURE:----piH8maKULHxdt5hTEFambOkkWCfrGArgU+Wf375XXTQhnv+S5yGdp7fg12rA+4BTXWpAC4AQ7iqpddPzOtAWeeWLmD0/1GXSsTmmtaOgMSXx19FDE3ROhOt+gXAp5Umg5lgUa6fzZByCMvqlib6lQWX9VMLbPLWXO5iY9KcVMgLjOENkmZbVBHlkhI+MaV3Tbz2AFnSlWzCMTuazrOCgg0flMaWX08379emFh+sh4JNP2ka5QESxEkAPQuUa2VE+2ZqYul+o9MwAcfODLwyieGZKsr7n+UGwYtBR1CQEDE/TO966dVln0d9xJeG7Xu/Y9F/r5OFJ+zR8mvyZ3KT9YOJCNarQgSdTY1BstYvDF728DJB3BfHzdE3CvGLa/mn78gikus+4lhrqcQs3Pws3DqZ86d+8X5hh+QfM0IGony9cG5VV8ppIRViUekrj4XHOoiIgb4nOzJxRhMkIFDvzBOGbMhwQXahTolzFtFf/AViVIxZFBZdFtC8YBAZ3kLYYrWVL6e3A8r6uLqV3tlgxw3+I6cK3zM2E1HdjCawmYxbqPt3Z707CJQpV/Y29ep06/Myx6s9Vf1eBnu473U5CcS2TcsbRa51jGe1zzbww+lk/ztxj6es96olcGKtUQueTv7hVN5TGIo6XVVyW+IdlCg3oXuKewjJbCSap37m4bJM=----ATTACHMENT:----NzczMTM5MjEzNDEwMTIxOSA3MTYyNjg1MDg1MzA2MTc4IDkwMDcyMTYyMDAzMzQ1OA==