vendor/symfony/property-info/Extractor/ReflectionExtractor.php line 86

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\PropertyInfo\Extractor;
  11. use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
  12. use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;
  13. use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
  14. use Symfony\Component\PropertyInfo\PropertyReadInfo;
  15. use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
  16. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  17. use Symfony\Component\PropertyInfo\PropertyWriteInfo;
  18. use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
  19. use Symfony\Component\PropertyInfo\Type;
  20. use Symfony\Component\String\Inflector\EnglishInflector;
  21. use Symfony\Component\String\Inflector\InflectorInterface;
  22. /**
  23.  * Extracts data using the reflection API.
  24.  *
  25.  * @author Kévin Dunglas <dunglas@gmail.com>
  26.  *
  27.  * @final
  28.  */
  29. class ReflectionExtractor implements PropertyListExtractorInterfacePropertyTypeExtractorInterfacePropertyAccessExtractorInterfacePropertyInitializableExtractorInterfacePropertyReadInfoExtractorInterfacePropertyWriteInfoExtractorInterfaceConstructorArgumentTypeExtractorInterface
  30. {
  31.     /**
  32.      * @internal
  33.      */
  34.     public static $defaultMutatorPrefixes = ['add''remove''set'];
  35.     /**
  36.      * @internal
  37.      */
  38.     public static $defaultAccessorPrefixes = ['get''is''has''can'];
  39.     /**
  40.      * @internal
  41.      */
  42.     public static $defaultArrayMutatorPrefixes = ['add''remove'];
  43.     public const ALLOW_PRIVATE 1;
  44.     public const ALLOW_PROTECTED 2;
  45.     public const ALLOW_PUBLIC 4;
  46.     /** @var int Allow none of the magic methods */
  47.     public const DISALLOW_MAGIC_METHODS 0;
  48.     /** @var int Allow magic __get methods */
  49.     public const ALLOW_MAGIC_GET << 0;
  50.     /** @var int Allow magic __set methods */
  51.     public const ALLOW_MAGIC_SET << 1;
  52.     /** @var int Allow magic __call methods */
  53.     public const ALLOW_MAGIC_CALL << 2;
  54.     private const MAP_TYPES = [
  55.         'integer' => Type::BUILTIN_TYPE_INT,
  56.         'boolean' => Type::BUILTIN_TYPE_BOOL,
  57.         'double' => Type::BUILTIN_TYPE_FLOAT,
  58.     ];
  59.     private $mutatorPrefixes;
  60.     private $accessorPrefixes;
  61.     private $arrayMutatorPrefixes;
  62.     private $enableConstructorExtraction;
  63.     private $methodReflectionFlags;
  64.     private $magicMethodsFlags;
  65.     private $propertyReflectionFlags;
  66.     private $inflector;
  67.     private $arrayMutatorPrefixesFirst;
  68.     private $arrayMutatorPrefixesLast;
  69.     /**
  70.      * @param string[]|null $mutatorPrefixes
  71.      * @param string[]|null $accessorPrefixes
  72.      * @param string[]|null $arrayMutatorPrefixes
  73.      */
  74.     public function __construct(array $mutatorPrefixes null, array $accessorPrefixes null, array $arrayMutatorPrefixes nullbool $enableConstructorExtraction trueint $accessFlags self::ALLOW_PUBLICInflectorInterface $inflector nullint $magicMethodsFlags self::ALLOW_MAGIC_GET self::ALLOW_MAGIC_SET)
  75.     {
  76.         $this->mutatorPrefixes $mutatorPrefixes ?? self::$defaultMutatorPrefixes;
  77.         $this->accessorPrefixes $accessorPrefixes ?? self::$defaultAccessorPrefixes;
  78.         $this->arrayMutatorPrefixes $arrayMutatorPrefixes ?? self::$defaultArrayMutatorPrefixes;
  79.         $this->enableConstructorExtraction $enableConstructorExtraction;
  80.         $this->methodReflectionFlags $this->getMethodsFlags($accessFlags);
  81.         $this->propertyReflectionFlags $this->getPropertyFlags($accessFlags);
  82.         $this->magicMethodsFlags $magicMethodsFlags;
  83.         $this->inflector $inflector ?? new EnglishInflector();
  84.         $this->arrayMutatorPrefixesFirst array_merge($this->arrayMutatorPrefixesarray_diff($this->mutatorPrefixes$this->arrayMutatorPrefixes));
  85.         $this->arrayMutatorPrefixesLast array_reverse($this->arrayMutatorPrefixesFirst);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function getProperties(string $class, array $context = []): ?array
  91.     {
  92.         try {
  93.             $reflectionClass = new \ReflectionClass($class);
  94.         } catch (\ReflectionException $e) {
  95.             return null;
  96.         }
  97.         $reflectionProperties $reflectionClass->getProperties();
  98.         $properties = [];
  99.         foreach ($reflectionProperties as $reflectionProperty) {
  100.             if ($reflectionProperty->getModifiers() & $this->propertyReflectionFlags) {
  101.                 $properties[$reflectionProperty->name] = $reflectionProperty->name;
  102.             }
  103.         }
  104.         foreach ($reflectionClass->getMethods($this->methodReflectionFlags) as $reflectionMethod) {
  105.             if ($reflectionMethod->isStatic()) {
  106.                 continue;
  107.             }
  108.             $propertyName $this->getPropertyName($reflectionMethod->name$reflectionProperties);
  109.             if (!$propertyName || isset($properties[$propertyName])) {
  110.                 continue;
  111.             }
  112.             if ($reflectionClass->hasProperty($lowerCasedPropertyName lcfirst($propertyName)) || (!$reflectionClass->hasProperty($propertyName) && !preg_match('/^[A-Z]{2,}/'$propertyName))) {
  113.                 $propertyName $lowerCasedPropertyName;
  114.             }
  115.             $properties[$propertyName] = $propertyName;
  116.         }
  117.         return $properties array_values($properties) : null;
  118.     }
  119.     /**
  120.      * {@inheritdoc}
  121.      */
  122.     public function getTypes(string $classstring $property, array $context = []): ?array
  123.     {
  124.         if ($fromMutator $this->extractFromMutator($class$property)) {
  125.             return $fromMutator;
  126.         }
  127.         if ($fromAccessor $this->extractFromAccessor($class$property)) {
  128.             return $fromAccessor;
  129.         }
  130.         if (
  131.             ($context['enable_constructor_extraction'] ?? $this->enableConstructorExtraction) &&
  132.             $fromConstructor $this->extractFromConstructor($class$property)
  133.         ) {
  134.             return $fromConstructor;
  135.         }
  136.         if ($fromPropertyDeclaration $this->extractFromPropertyDeclaration($class$property)) {
  137.             return $fromPropertyDeclaration;
  138.         }
  139.         return null;
  140.     }
  141.     /**
  142.      * {@inheritdoc}
  143.      */
  144.     public function getTypesFromConstructor(string $classstring $property): ?array
  145.     {
  146.         try {
  147.             $reflection = new \ReflectionClass($class);
  148.         } catch (\ReflectionException $e) {
  149.             return null;
  150.         }
  151.         if (!$reflectionConstructor $reflection->getConstructor()) {
  152.             return null;
  153.         }
  154.         if (!$reflectionParameter $this->getReflectionParameterFromConstructor($property$reflectionConstructor)) {
  155.             return null;
  156.         }
  157.         if (!$reflectionType $reflectionParameter->getType()) {
  158.             return null;
  159.         }
  160.         if (!$types $this->extractFromReflectionType($reflectionType$reflectionConstructor->getDeclaringClass())) {
  161.             return null;
  162.         }
  163.         return $types;
  164.     }
  165.     private function getReflectionParameterFromConstructor(string $property\ReflectionMethod $reflectionConstructor): ?\ReflectionParameter
  166.     {
  167.         $reflectionParameter null;
  168.         foreach ($reflectionConstructor->getParameters() as $reflectionParameter) {
  169.             if ($reflectionParameter->getName() === $property) {
  170.                 return $reflectionParameter;
  171.             }
  172.         }
  173.         return null;
  174.     }
  175.     /**
  176.      * {@inheritdoc}
  177.      */
  178.     public function isReadable(string $classstring $property, array $context = []): ?bool
  179.     {
  180.         if ($this->isAllowedProperty($class$property)) {
  181.             return true;
  182.         }
  183.         return null !== $this->getReadInfo($class$property$context);
  184.     }
  185.     /**
  186.      * {@inheritdoc}
  187.      */
  188.     public function isWritable(string $classstring $property, array $context = []): ?bool
  189.     {
  190.         if ($this->isAllowedProperty($class$property)) {
  191.             return true;
  192.         }
  193.         [$reflectionMethod] = $this->getMutatorMethod($class$property);
  194.         return null !== $reflectionMethod;
  195.     }
  196.     /**
  197.      * {@inheritdoc}
  198.      */
  199.     public function isInitializable(string $classstring $property, array $context = []): ?bool
  200.     {
  201.         try {
  202.             $reflectionClass = new \ReflectionClass($class);
  203.         } catch (\ReflectionException $e) {
  204.             return null;
  205.         }
  206.         if (!$reflectionClass->isInstantiable()) {
  207.             return false;
  208.         }
  209.         if ($constructor $reflectionClass->getConstructor()) {
  210.             foreach ($constructor->getParameters() as $parameter) {
  211.                 if ($property === $parameter->name) {
  212.                     return true;
  213.                 }
  214.             }
  215.         } elseif ($parentClass $reflectionClass->getParentClass()) {
  216.             return $this->isInitializable($parentClass->getName(), $property);
  217.         }
  218.         return false;
  219.     }
  220.     /**
  221.      * {@inheritdoc}
  222.      */
  223.     public function getReadInfo(string $classstring $property, array $context = []): ?PropertyReadInfo
  224.     {
  225.         try {
  226.             $reflClass = new \ReflectionClass($class);
  227.         } catch (\ReflectionException $e) {
  228.             return null;
  229.         }
  230.         $allowGetterSetter $context['enable_getter_setter_extraction'] ?? false;
  231.         $magicMethods $context['enable_magic_methods_extraction'] ?? $this->magicMethodsFlags;
  232.         $allowMagicCall = (bool) ($magicMethods self::ALLOW_MAGIC_CALL);
  233.         $allowMagicGet = (bool) ($magicMethods self::ALLOW_MAGIC_GET);
  234.         if (isset($context['enable_magic_call_extraction'])) {
  235.             trigger_deprecation('symfony/property-info''5.2''Using the "enable_magic_call_extraction" context option in "%s()" is deprecated. Use "enable_magic_methods_extraction" instead.'__METHOD__);
  236.             $allowMagicCall $context['enable_magic_call_extraction'] ?? false;
  237.         }
  238.         $hasProperty $reflClass->hasProperty($property);
  239.         $camelProp $this->camelize($property);
  240.         $getsetter lcfirst($camelProp); // jQuery style, e.g. read: last(), write: last($item)
  241.         foreach ($this->accessorPrefixes as $prefix) {
  242.             $methodName $prefix.$camelProp;
  243.             if ($reflClass->hasMethod($methodName) && $reflClass->getMethod($methodName)->getModifiers() & $this->methodReflectionFlags && !$reflClass->getMethod($methodName)->getNumberOfRequiredParameters()) {
  244.                 $method $reflClass->getMethod($methodName);
  245.                 return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD$methodName$this->getReadVisiblityForMethod($method), $method->isStatic(), false);
  246.             }
  247.         }
  248.         if ($allowGetterSetter && $reflClass->hasMethod($getsetter) && ($reflClass->getMethod($getsetter)->getModifiers() & $this->methodReflectionFlags)) {
  249.             $method $reflClass->getMethod($getsetter);
  250.             return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD$getsetter$this->getReadVisiblityForMethod($method), $method->isStatic(), false);
  251.         }
  252.         if ($allowMagicGet && $reflClass->hasMethod('__get') && ($reflClass->getMethod('__get')->getModifiers() & $this->methodReflectionFlags)) {
  253.             return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY$propertyPropertyReadInfo::VISIBILITY_PUBLICfalsefalse);
  254.         }
  255.         if ($hasProperty && ($reflClass->getProperty($property)->getModifiers() & $this->propertyReflectionFlags)) {
  256.             $reflProperty $reflClass->getProperty($property);
  257.             return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY$property$this->getReadVisiblityForProperty($reflProperty), $reflProperty->isStatic(), true);
  258.         }
  259.         if ($allowMagicCall && $reflClass->hasMethod('__call') && ($reflClass->getMethod('__call')->getModifiers() & $this->methodReflectionFlags)) {
  260.             return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD'get'.$camelPropPropertyReadInfo::VISIBILITY_PUBLICfalsefalse);
  261.         }
  262.         return null;
  263.     }
  264.     /**
  265.      * {@inheritdoc}
  266.      */
  267.     public function getWriteInfo(string $classstring $property, array $context = []): ?PropertyWriteInfo
  268.     {
  269.         try {
  270.             $reflClass = new \ReflectionClass($class);
  271.         } catch (\ReflectionException $e) {
  272.             return null;
  273.         }
  274.         $allowGetterSetter $context['enable_getter_setter_extraction'] ?? false;
  275.         $magicMethods $context['enable_magic_methods_extraction'] ?? $this->magicMethodsFlags;
  276.         $allowMagicCall = (bool) ($magicMethods self::ALLOW_MAGIC_CALL);
  277.         $allowMagicSet = (bool) ($magicMethods self::ALLOW_MAGIC_SET);
  278.         if (isset($context['enable_magic_call_extraction'])) {
  279.             trigger_deprecation('symfony/property-info''5.2''Using the "enable_magic_call_extraction" context option in "%s()" is deprecated. Use "enable_magic_methods_extraction" instead.'__METHOD__);
  280.             $allowMagicCall $context['enable_magic_call_extraction'] ?? false;
  281.         }
  282.         $allowConstruct $context['enable_constructor_extraction'] ?? $this->enableConstructorExtraction;
  283.         $allowAdderRemover $context['enable_adder_remover_extraction'] ?? true;
  284.         $camelized $this->camelize($property);
  285.         $constructor $reflClass->getConstructor();
  286.         $singulars $this->inflector->singularize($camelized);
  287.         $errors = [];
  288.         if (null !== $constructor && $allowConstruct) {
  289.             foreach ($constructor->getParameters() as $parameter) {
  290.                 if ($parameter->getName() === $property) {
  291.                     return new PropertyWriteInfo(PropertyWriteInfo::TYPE_CONSTRUCTOR$property);
  292.                 }
  293.             }
  294.         }
  295.         [$adderAccessName$removerAccessName$adderAndRemoverErrors] = $this->findAdderAndRemover($reflClass$singulars);
  296.         if ($allowAdderRemover && null !== $adderAccessName && null !== $removerAccessName) {
  297.             $adderMethod $reflClass->getMethod($adderAccessName);
  298.             $removerMethod $reflClass->getMethod($removerAccessName);
  299.             $mutator = new PropertyWriteInfo(PropertyWriteInfo::TYPE_ADDER_AND_REMOVER);
  300.             $mutator->setAdderInfo(new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD$adderAccessName$this->getWriteVisiblityForMethod($adderMethod), $adderMethod->isStatic()));
  301.             $mutator->setRemoverInfo(new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD$removerAccessName$this->getWriteVisiblityForMethod($removerMethod), $removerMethod->isStatic()));
  302.             return $mutator;
  303.         }
  304.         $errors[] = $adderAndRemoverErrors;
  305.         foreach ($this->mutatorPrefixes as $mutatorPrefix) {
  306.             $methodName $mutatorPrefix.$camelized;
  307.             [$accessible$methodAccessibleErrors] = $this->isMethodAccessible($reflClass$methodName1);
  308.             if (!$accessible) {
  309.                 $errors[] = $methodAccessibleErrors;
  310.                 continue;
  311.             }
  312.             $method $reflClass->getMethod($methodName);
  313.             if (!\in_array($mutatorPrefix$this->arrayMutatorPrefixestrue)) {
  314.                 return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD$methodName$this->getWriteVisiblityForMethod($method), $method->isStatic());
  315.             }
  316.         }
  317.         $getsetter lcfirst($camelized);
  318.         if ($allowGetterSetter) {
  319.             [$accessible$methodAccessibleErrors] = $this->isMethodAccessible($reflClass$getsetter1);
  320.             if ($accessible) {
  321.                 $method $reflClass->getMethod($getsetter);
  322.                 return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD$getsetter$this->getWriteVisiblityForMethod($method), $method->isStatic());
  323.             }
  324.             $errors[] = $methodAccessibleErrors;
  325.         }
  326.         if ($reflClass->hasProperty($property) && ($reflClass->getProperty($property)->getModifiers() & $this->propertyReflectionFlags)) {
  327.             $reflProperty $reflClass->getProperty($property);
  328.             return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY$property$this->getWriteVisiblityForProperty($reflProperty), $reflProperty->isStatic());
  329.         }
  330.         if ($allowMagicSet) {
  331.             [$accessible$methodAccessibleErrors] = $this->isMethodAccessible($reflClass'__set'2);
  332.             if ($accessible) {
  333.                 return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY$propertyPropertyWriteInfo::VISIBILITY_PUBLICfalse);
  334.             }
  335.             $errors[] = $methodAccessibleErrors;
  336.         }
  337.         if ($allowMagicCall) {
  338.             [$accessible$methodAccessibleErrors] = $this->isMethodAccessible($reflClass'__call'2);
  339.             if ($accessible) {
  340.                 return new PropertyWriteInfo(PropertyWriteInfo::TYPE_METHOD'set'.$camelizedPropertyWriteInfo::VISIBILITY_PUBLICfalse);
  341.             }
  342.             $errors[] = $methodAccessibleErrors;
  343.         }
  344.         if (!$allowAdderRemover && null !== $adderAccessName && null !== $removerAccessName) {
  345.             $errors[] = [sprintf(
  346.                 'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
  347.                 'the new value must be an array or an instance of \Traversable',
  348.                 $property,
  349.                 $reflClass->getName(),
  350.                 implode('()", "', [$adderAccessName$removerAccessName])
  351.             )];
  352.         }
  353.         $noneProperty = new PropertyWriteInfo();
  354.         $noneProperty->setErrors(array_merge([], ...$errors));
  355.         return $noneProperty;
  356.     }
  357.     /**
  358.      * @return Type[]|null
  359.      */
  360.     private function extractFromMutator(string $classstring $property): ?array
  361.     {
  362.         [$reflectionMethod$prefix] = $this->getMutatorMethod($class$property);
  363.         if (null === $reflectionMethod) {
  364.             return null;
  365.         }
  366.         $reflectionParameters $reflectionMethod->getParameters();
  367.         $reflectionParameter $reflectionParameters[0];
  368.         if (!$reflectionType $reflectionParameter->getType()) {
  369.             return null;
  370.         }
  371.         $type $this->extractFromReflectionType($reflectionType$reflectionMethod->getDeclaringClass());
  372.         if (=== \count($type) && \in_array($prefix$this->arrayMutatorPrefixes)) {
  373.             $type = [new Type(Type::BUILTIN_TYPE_ARRAYfalsenulltrue, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
  374.         }
  375.         return $type;
  376.     }
  377.     /**
  378.      * Tries to extract type information from accessors.
  379.      *
  380.      * @return Type[]|null
  381.      */
  382.     private function extractFromAccessor(string $classstring $property): ?array
  383.     {
  384.         [$reflectionMethod$prefix] = $this->getAccessorMethod($class$property);
  385.         if (null === $reflectionMethod) {
  386.             return null;
  387.         }
  388.         if ($reflectionType $reflectionMethod->getReturnType()) {
  389.             return $this->extractFromReflectionType($reflectionType$reflectionMethod->getDeclaringClass());
  390.         }
  391.         if (\in_array($prefix, ['is''can''has'])) {
  392.             return [new Type(Type::BUILTIN_TYPE_BOOL)];
  393.         }
  394.         return null;
  395.     }
  396.     /**
  397.      * Tries to extract type information from constructor.
  398.      *
  399.      * @return Type[]|null
  400.      */
  401.     private function extractFromConstructor(string $classstring $property): ?array
  402.     {
  403.         try {
  404.             $reflectionClass = new \ReflectionClass($class);
  405.         } catch (\ReflectionException $e) {
  406.             return null;
  407.         }
  408.         $constructor $reflectionClass->getConstructor();
  409.         if (!$constructor) {
  410.             return null;
  411.         }
  412.         foreach ($constructor->getParameters() as $parameter) {
  413.             if ($property !== $parameter->name) {
  414.                 continue;
  415.             }
  416.             $reflectionType $parameter->getType();
  417.             return $reflectionType $this->extractFromReflectionType($reflectionType$constructor->getDeclaringClass()) : null;
  418.         }
  419.         if ($parentClass $reflectionClass->getParentClass()) {
  420.             return $this->extractFromConstructor($parentClass->getName(), $property);
  421.         }
  422.         return null;
  423.     }
  424.     private function extractFromPropertyDeclaration(string $classstring $property): ?array
  425.     {
  426.         try {
  427.             $reflectionClass = new \ReflectionClass($class);
  428.             if (\PHP_VERSION_ID >= 70400) {
  429.                 $reflectionProperty $reflectionClass->getProperty($property);
  430.                 $reflectionPropertyType $reflectionProperty->getType();
  431.                 if (null !== $reflectionPropertyType && $types $this->extractFromReflectionType($reflectionPropertyType$reflectionProperty->getDeclaringClass())) {
  432.                     return $types;
  433.                 }
  434.             }
  435.         } catch (\ReflectionException $e) {
  436.             return null;
  437.         }
  438.         $defaultValue $reflectionClass->getDefaultProperties()[$property] ?? null;
  439.         if (null === $defaultValue) {
  440.             return null;
  441.         }
  442.         $type \gettype($defaultValue);
  443.         $type = static::MAP_TYPES[$type] ?? $type;
  444.         return [new Type($type$this->isNullableProperty($class$property), nullType::BUILTIN_TYPE_ARRAY === $type)];
  445.     }
  446.     private function extractFromReflectionType(\ReflectionType $reflectionType\ReflectionClass $declaringClass): array
  447.     {
  448.         $types = [];
  449.         $nullable $reflectionType->allowsNull();
  450.         foreach (($reflectionType instanceof \ReflectionUnionType || $reflectionType instanceof \ReflectionIntersectionType) ? $reflectionType->getTypes() : [$reflectionType] as $type) {
  451.             if (!$type instanceof \ReflectionNamedType) {
  452.                 // Nested composite types are not supported yet.
  453.                 return [];
  454.             }
  455.             $phpTypeOrClass $type->getName();
  456.             if ('null' === $phpTypeOrClass || 'mixed' === $phpTypeOrClass || 'never' === $phpTypeOrClass) {
  457.                 continue;
  458.             }
  459.             if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
  460.                 $types[] = new Type(Type::BUILTIN_TYPE_ARRAY$nullablenulltrue);
  461.             } elseif ('void' === $phpTypeOrClass) {
  462.                 $types[] = new Type(Type::BUILTIN_TYPE_NULL$nullable);
  463.             } elseif ($type->isBuiltin()) {
  464.                 $types[] = new Type($phpTypeOrClass$nullable);
  465.             } else {
  466.                 $types[] = new Type(Type::BUILTIN_TYPE_OBJECT$nullable$this->resolveTypeName($phpTypeOrClass$declaringClass));
  467.             }
  468.         }
  469.         return $types;
  470.     }
  471.     private function resolveTypeName(string $name\ReflectionClass $declaringClass): string
  472.     {
  473.         if ('self' === $lcName strtolower($name)) {
  474.             return $declaringClass->name;
  475.         }
  476.         if ('parent' === $lcName && $parent $declaringClass->getParentClass()) {
  477.             return $parent->name;
  478.         }
  479.         return $name;
  480.     }
  481.     private function isNullableProperty(string $classstring $property): bool
  482.     {
  483.         try {
  484.             $reflectionProperty = new \ReflectionProperty($class$property);
  485.             if (\PHP_VERSION_ID >= 70400) {
  486.                 $reflectionPropertyType $reflectionProperty->getType();
  487.                 return null !== $reflectionPropertyType && $reflectionPropertyType->allowsNull();
  488.             }
  489.             return false;
  490.         } catch (\ReflectionException $e) {
  491.             // Return false if the property doesn't exist
  492.         }
  493.         return false;
  494.     }
  495.     private function isAllowedProperty(string $classstring $property): bool
  496.     {
  497.         try {
  498.             $reflectionProperty = new \ReflectionProperty($class$property);
  499.             return (bool) ($reflectionProperty->getModifiers() & $this->propertyReflectionFlags);
  500.         } catch (\ReflectionException $e) {
  501.             // Return false if the property doesn't exist
  502.         }
  503.         return false;
  504.     }
  505.     /**
  506.      * Gets the accessor method.
  507.      *
  508.      * Returns an array with a the instance of \ReflectionMethod as first key
  509.      * and the prefix of the method as second or null if not found.
  510.      */
  511.     private function getAccessorMethod(string $classstring $property): ?array
  512.     {
  513.         $ucProperty ucfirst($property);
  514.         foreach ($this->accessorPrefixes as $prefix) {
  515.             try {
  516.                 $reflectionMethod = new \ReflectionMethod($class$prefix.$ucProperty);
  517.                 if ($reflectionMethod->isStatic()) {
  518.                     continue;
  519.                 }
  520.                 if (=== $reflectionMethod->getNumberOfRequiredParameters()) {
  521.                     return [$reflectionMethod$prefix];
  522.                 }
  523.             } catch (\ReflectionException $e) {
  524.                 // Return null if the property doesn't exist
  525.             }
  526.         }
  527.         return null;
  528.     }
  529.     /**
  530.      * Returns an array with a the instance of \ReflectionMethod as first key
  531.      * and the prefix of the method as second or null if not found.
  532.      */
  533.     private function getMutatorMethod(string $classstring $property): ?array
  534.     {
  535.         $ucProperty ucfirst($property);
  536.         $ucSingulars $this->inflector->singularize($ucProperty);
  537.         $mutatorPrefixes \in_array($ucProperty$ucSingularstrue) ? $this->arrayMutatorPrefixesLast $this->arrayMutatorPrefixesFirst;
  538.         foreach ($mutatorPrefixes as $prefix) {
  539.             $names = [$ucProperty];
  540.             if (\in_array($prefix$this->arrayMutatorPrefixes)) {
  541.                 $names array_merge($names$ucSingulars);
  542.             }
  543.             foreach ($names as $name) {
  544.                 try {
  545.                     $reflectionMethod = new \ReflectionMethod($class$prefix.$name);
  546.                     if ($reflectionMethod->isStatic()) {
  547.                         continue;
  548.                     }
  549.                     // Parameter can be optional to allow things like: method(array $foo = null)
  550.                     if ($reflectionMethod->getNumberOfParameters() >= 1) {
  551.                         return [$reflectionMethod$prefix];
  552.                     }
  553.                 } catch (\ReflectionException $e) {
  554.                     // Try the next prefix if the method doesn't exist
  555.                 }
  556.             }
  557.         }
  558.         return null;
  559.     }
  560.     private function getPropertyName(string $methodName, array $reflectionProperties): ?string
  561.     {
  562.         $pattern implode('|'array_merge($this->accessorPrefixes$this->mutatorPrefixes));
  563.         if ('' !== $pattern && preg_match('/^('.$pattern.')(.+)$/i'$methodName$matches)) {
  564.             if (!\in_array($matches[1], $this->arrayMutatorPrefixes)) {
  565.                 return $matches[2];
  566.             }
  567.             foreach ($reflectionProperties as $reflectionProperty) {
  568.                 foreach ($this->inflector->singularize($reflectionProperty->name) as $name) {
  569.                     if (strtolower($name) === strtolower($matches[2])) {
  570.                         return $reflectionProperty->name;
  571.                     }
  572.                 }
  573.             }
  574.             return $matches[2];
  575.         }
  576.         return null;
  577.     }
  578.     /**
  579.      * Searches for add and remove methods.
  580.      *
  581.      * @param \ReflectionClass $reflClass The reflection class for the given object
  582.      * @param array            $singulars The singular form of the property name or null
  583.      *
  584.      * @return array An array containing the adder and remover when found and errors
  585.      */
  586.     private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars): array
  587.     {
  588.         if (!\is_array($this->arrayMutatorPrefixes) && !== \count($this->arrayMutatorPrefixes)) {
  589.             return [nullnull, []];
  590.         }
  591.         [$addPrefix$removePrefix] = $this->arrayMutatorPrefixes;
  592.         $errors = [];
  593.         foreach ($singulars as $singular) {
  594.             $addMethod $addPrefix.$singular;
  595.             $removeMethod $removePrefix.$singular;
  596.             [$addMethodFound$addMethodAccessibleErrors] = $this->isMethodAccessible($reflClass$addMethod1);
  597.             [$removeMethodFound$removeMethodAccessibleErrors] = $this->isMethodAccessible($reflClass$removeMethod1);
  598.             $errors[] = $addMethodAccessibleErrors;
  599.             $errors[] = $removeMethodAccessibleErrors;
  600.             if ($addMethodFound && $removeMethodFound) {
  601.                 return [$addMethod$removeMethod, []];
  602.             }
  603.             if ($addMethodFound && !$removeMethodFound) {
  604.                 $errors[] = [sprintf('The add method "%s" in class "%s" was found, but the corresponding remove method "%s" was not found'$addMethod$reflClass->getName(), $removeMethod)];
  605.             } elseif (!$addMethodFound && $removeMethodFound) {
  606.                 $errors[] = [sprintf('The remove method "%s" in class "%s" was found, but the corresponding add method "%s" was not found'$removeMethod$reflClass->getName(), $addMethod)];
  607.             }
  608.         }
  609.         return [nullnullarray_merge([], ...$errors)];
  610.     }
  611.     /**
  612.      * Returns whether a method is public and has the number of required parameters and errors.
  613.      */
  614.     private function isMethodAccessible(\ReflectionClass $classstring $methodNameint $parameters): array
  615.     {
  616.         $errors = [];
  617.         if ($class->hasMethod($methodName)) {
  618.             $method $class->getMethod($methodName);
  619.             if (\ReflectionMethod::IS_PUBLIC === $this->methodReflectionFlags && !$method->isPublic()) {
  620.                 $errors[] = sprintf('The method "%s" in class "%s" was found but does not have public access.'$methodName$class->getName());
  621.             } elseif ($method->getNumberOfRequiredParameters() > $parameters || $method->getNumberOfParameters() < $parameters) {
  622.                 $errors[] = sprintf('The method "%s" in class "%s" requires %d arguments, but should accept only %d.'$methodName$class->getName(), $method->getNumberOfRequiredParameters(), $parameters);
  623.             } else {
  624.                 return [true$errors];
  625.             }
  626.         }
  627.         return [false$errors];
  628.     }
  629.     /**
  630.      * Camelizes a given string.
  631.      */
  632.     private function camelize(string $string): string
  633.     {
  634.         return str_replace(' '''ucwords(str_replace('_'' '$string)));
  635.     }
  636.     /**
  637.      * Return allowed reflection method flags.
  638.      */
  639.     private function getMethodsFlags(int $accessFlags): int
  640.     {
  641.         $methodFlags 0;
  642.         if ($accessFlags self::ALLOW_PUBLIC) {
  643.             $methodFlags |= \ReflectionMethod::IS_PUBLIC;
  644.         }
  645.         if ($accessFlags self::ALLOW_PRIVATE) {
  646.             $methodFlags |= \ReflectionMethod::IS_PRIVATE;
  647.         }
  648.         if ($accessFlags self::ALLOW_PROTECTED) {
  649.             $methodFlags |= \ReflectionMethod::IS_PROTECTED;
  650.         }
  651.         return $methodFlags;
  652.     }
  653.     /**
  654.      * Return allowed reflection property flags.
  655.      */
  656.     private function getPropertyFlags(int $accessFlags): int
  657.     {
  658.         $propertyFlags 0;
  659.         if ($accessFlags self::ALLOW_PUBLIC) {
  660.             $propertyFlags |= \ReflectionProperty::IS_PUBLIC;
  661.         }
  662.         if ($accessFlags self::ALLOW_PRIVATE) {
  663.             $propertyFlags |= \ReflectionProperty::IS_PRIVATE;
  664.         }
  665.         if ($accessFlags self::ALLOW_PROTECTED) {
  666.             $propertyFlags |= \ReflectionProperty::IS_PROTECTED;
  667.         }
  668.         return $propertyFlags;
  669.     }
  670.     private function getReadVisiblityForProperty(\ReflectionProperty $reflectionProperty): string
  671.     {
  672.         if ($reflectionProperty->isPrivate()) {
  673.             return PropertyReadInfo::VISIBILITY_PRIVATE;
  674.         }
  675.         if ($reflectionProperty->isProtected()) {
  676.             return PropertyReadInfo::VISIBILITY_PROTECTED;
  677.         }
  678.         return PropertyReadInfo::VISIBILITY_PUBLIC;
  679.     }
  680.     private function getReadVisiblityForMethod(\ReflectionMethod $reflectionMethod): string
  681.     {
  682.         if ($reflectionMethod->isPrivate()) {
  683.             return PropertyReadInfo::VISIBILITY_PRIVATE;
  684.         }
  685.         if ($reflectionMethod->isProtected()) {
  686.             return PropertyReadInfo::VISIBILITY_PROTECTED;
  687.         }
  688.         return PropertyReadInfo::VISIBILITY_PUBLIC;
  689.     }
  690.     private function getWriteVisiblityForProperty(\ReflectionProperty $reflectionProperty): string
  691.     {
  692.         if ($reflectionProperty->isPrivate()) {
  693.             return PropertyWriteInfo::VISIBILITY_PRIVATE;
  694.         }
  695.         if ($reflectionProperty->isProtected()) {
  696.             return PropertyWriteInfo::VISIBILITY_PROTECTED;
  697.         }
  698.         return PropertyWriteInfo::VISIBILITY_PUBLIC;
  699.     }
  700.     private function getWriteVisiblityForMethod(\ReflectionMethod $reflectionMethod): string
  701.     {
  702.         if ($reflectionMethod->isPrivate()) {
  703.             return PropertyWriteInfo::VISIBILITY_PRIVATE;
  704.         }
  705.         if ($reflectionMethod->isProtected()) {
  706.             return PropertyWriteInfo::VISIBILITY_PROTECTED;
  707.         }
  708.         return PropertyWriteInfo::VISIBILITY_PUBLIC;
  709.     }
  710. }