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.             $phpTypeOrClass $type->getName();
  452.             if ('null' === $phpTypeOrClass || 'mixed' === $phpTypeOrClass || 'never' === $phpTypeOrClass) {
  453.                 continue;
  454.             }
  455.             if (Type::BUILTIN_TYPE_ARRAY === $phpTypeOrClass) {
  456.                 $types[] = new Type(Type::BUILTIN_TYPE_ARRAY$nullablenulltrue);
  457.             } elseif ('void' === $phpTypeOrClass) {
  458.                 $types[] = new Type(Type::BUILTIN_TYPE_NULL$nullable);
  459.             } elseif ($type->isBuiltin()) {
  460.                 $types[] = new Type($phpTypeOrClass$nullable);
  461.             } else {
  462.                 $types[] = new Type(Type::BUILTIN_TYPE_OBJECT$nullable$this->resolveTypeName($phpTypeOrClass$declaringClass));
  463.             }
  464.         }
  465.         return $types;
  466.     }
  467.     private function resolveTypeName(string $name\ReflectionClass $declaringClass): string
  468.     {
  469.         if ('self' === $lcName strtolower($name)) {
  470.             return $declaringClass->name;
  471.         }
  472.         if ('parent' === $lcName && $parent $declaringClass->getParentClass()) {
  473.             return $parent->name;
  474.         }
  475.         return $name;
  476.     }
  477.     private function isNullableProperty(string $classstring $property): bool
  478.     {
  479.         try {
  480.             $reflectionProperty = new \ReflectionProperty($class$property);
  481.             if (\PHP_VERSION_ID >= 70400) {
  482.                 $reflectionPropertyType $reflectionProperty->getType();
  483.                 return null !== $reflectionPropertyType && $reflectionPropertyType->allowsNull();
  484.             }
  485.             return false;
  486.         } catch (\ReflectionException $e) {
  487.             // Return false if the property doesn't exist
  488.         }
  489.         return false;
  490.     }
  491.     private function isAllowedProperty(string $classstring $property): bool
  492.     {
  493.         try {
  494.             $reflectionProperty = new \ReflectionProperty($class$property);
  495.             return (bool) ($reflectionProperty->getModifiers() & $this->propertyReflectionFlags);
  496.         } catch (\ReflectionException $e) {
  497.             // Return false if the property doesn't exist
  498.         }
  499.         return false;
  500.     }
  501.     /**
  502.      * Gets the accessor method.
  503.      *
  504.      * Returns an array with a the instance of \ReflectionMethod as first key
  505.      * and the prefix of the method as second or null if not found.
  506.      */
  507.     private function getAccessorMethod(string $classstring $property): ?array
  508.     {
  509.         $ucProperty ucfirst($property);
  510.         foreach ($this->accessorPrefixes as $prefix) {
  511.             try {
  512.                 $reflectionMethod = new \ReflectionMethod($class$prefix.$ucProperty);
  513.                 if ($reflectionMethod->isStatic()) {
  514.                     continue;
  515.                 }
  516.                 if (=== $reflectionMethod->getNumberOfRequiredParameters()) {
  517.                     return [$reflectionMethod$prefix];
  518.                 }
  519.             } catch (\ReflectionException $e) {
  520.                 // Return null if the property doesn't exist
  521.             }
  522.         }
  523.         return null;
  524.     }
  525.     /**
  526.      * Returns an array with a the instance of \ReflectionMethod as first key
  527.      * and the prefix of the method as second or null if not found.
  528.      */
  529.     private function getMutatorMethod(string $classstring $property): ?array
  530.     {
  531.         $ucProperty ucfirst($property);
  532.         $ucSingulars $this->inflector->singularize($ucProperty);
  533.         $mutatorPrefixes \in_array($ucProperty$ucSingularstrue) ? $this->arrayMutatorPrefixesLast $this->arrayMutatorPrefixesFirst;
  534.         foreach ($mutatorPrefixes as $prefix) {
  535.             $names = [$ucProperty];
  536.             if (\in_array($prefix$this->arrayMutatorPrefixes)) {
  537.                 $names array_merge($names$ucSingulars);
  538.             }
  539.             foreach ($names as $name) {
  540.                 try {
  541.                     $reflectionMethod = new \ReflectionMethod($class$prefix.$name);
  542.                     if ($reflectionMethod->isStatic()) {
  543.                         continue;
  544.                     }
  545.                     // Parameter can be optional to allow things like: method(array $foo = null)
  546.                     if ($reflectionMethod->getNumberOfParameters() >= 1) {
  547.                         return [$reflectionMethod$prefix];
  548.                     }
  549.                 } catch (\ReflectionException $e) {
  550.                     // Try the next prefix if the method doesn't exist
  551.                 }
  552.             }
  553.         }
  554.         return null;
  555.     }
  556.     private function getPropertyName(string $methodName, array $reflectionProperties): ?string
  557.     {
  558.         $pattern implode('|'array_merge($this->accessorPrefixes$this->mutatorPrefixes));
  559.         if ('' !== $pattern && preg_match('/^('.$pattern.')(.+)$/i'$methodName$matches)) {
  560.             if (!\in_array($matches[1], $this->arrayMutatorPrefixes)) {
  561.                 return $matches[2];
  562.             }
  563.             foreach ($reflectionProperties as $reflectionProperty) {
  564.                 foreach ($this->inflector->singularize($reflectionProperty->name) as $name) {
  565.                     if (strtolower($name) === strtolower($matches[2])) {
  566.                         return $reflectionProperty->name;
  567.                     }
  568.                 }
  569.             }
  570.             return $matches[2];
  571.         }
  572.         return null;
  573.     }
  574.     /**
  575.      * Searches for add and remove methods.
  576.      *
  577.      * @param \ReflectionClass $reflClass The reflection class for the given object
  578.      * @param array            $singulars The singular form of the property name or null
  579.      *
  580.      * @return array An array containing the adder and remover when found and errors
  581.      */
  582.     private function findAdderAndRemover(\ReflectionClass $reflClass, array $singulars): array
  583.     {
  584.         if (!\is_array($this->arrayMutatorPrefixes) && !== \count($this->arrayMutatorPrefixes)) {
  585.             return [nullnull, []];
  586.         }
  587.         [$addPrefix$removePrefix] = $this->arrayMutatorPrefixes;
  588.         $errors = [];
  589.         foreach ($singulars as $singular) {
  590.             $addMethod $addPrefix.$singular;
  591.             $removeMethod $removePrefix.$singular;
  592.             [$addMethodFound$addMethodAccessibleErrors] = $this->isMethodAccessible($reflClass$addMethod1);
  593.             [$removeMethodFound$removeMethodAccessibleErrors] = $this->isMethodAccessible($reflClass$removeMethod1);
  594.             $errors[] = $addMethodAccessibleErrors;
  595.             $errors[] = $removeMethodAccessibleErrors;
  596.             if ($addMethodFound && $removeMethodFound) {
  597.                 return [$addMethod$removeMethod, []];
  598.             }
  599.             if ($addMethodFound && !$removeMethodFound) {
  600.                 $errors[] = [sprintf('The add method "%s" in class "%s" was found, but the corresponding remove method "%s" was not found'$addMethod$reflClass->getName(), $removeMethod)];
  601.             } elseif (!$addMethodFound && $removeMethodFound) {
  602.                 $errors[] = [sprintf('The remove method "%s" in class "%s" was found, but the corresponding add method "%s" was not found'$removeMethod$reflClass->getName(), $addMethod)];
  603.             }
  604.         }
  605.         return [nullnullarray_merge([], ...$errors)];
  606.     }
  607.     /**
  608.      * Returns whether a method is public and has the number of required parameters and errors.
  609.      */
  610.     private function isMethodAccessible(\ReflectionClass $classstring $methodNameint $parameters): array
  611.     {
  612.         $errors = [];
  613.         if ($class->hasMethod($methodName)) {
  614.             $method $class->getMethod($methodName);
  615.             if (\ReflectionMethod::IS_PUBLIC === $this->methodReflectionFlags && !$method->isPublic()) {
  616.                 $errors[] = sprintf('The method "%s" in class "%s" was found but does not have public access.'$methodName$class->getName());
  617.             } elseif ($method->getNumberOfRequiredParameters() > $parameters || $method->getNumberOfParameters() < $parameters) {
  618.                 $errors[] = sprintf('The method "%s" in class "%s" requires %d arguments, but should accept only %d.'$methodName$class->getName(), $method->getNumberOfRequiredParameters(), $parameters);
  619.             } else {
  620.                 return [true$errors];
  621.             }
  622.         }
  623.         return [false$errors];
  624.     }
  625.     /**
  626.      * Camelizes a given string.
  627.      */
  628.     private function camelize(string $string): string
  629.     {
  630.         return str_replace(' '''ucwords(str_replace('_'' '$string)));
  631.     }
  632.     /**
  633.      * Return allowed reflection method flags.
  634.      */
  635.     private function getMethodsFlags(int $accessFlags): int
  636.     {
  637.         $methodFlags 0;
  638.         if ($accessFlags self::ALLOW_PUBLIC) {
  639.             $methodFlags |= \ReflectionMethod::IS_PUBLIC;
  640.         }
  641.         if ($accessFlags self::ALLOW_PRIVATE) {
  642.             $methodFlags |= \ReflectionMethod::IS_PRIVATE;
  643.         }
  644.         if ($accessFlags self::ALLOW_PROTECTED) {
  645.             $methodFlags |= \ReflectionMethod::IS_PROTECTED;
  646.         }
  647.         return $methodFlags;
  648.     }
  649.     /**
  650.      * Return allowed reflection property flags.
  651.      */
  652.     private function getPropertyFlags(int $accessFlags): int
  653.     {
  654.         $propertyFlags 0;
  655.         if ($accessFlags self::ALLOW_PUBLIC) {
  656.             $propertyFlags |= \ReflectionProperty::IS_PUBLIC;
  657.         }
  658.         if ($accessFlags self::ALLOW_PRIVATE) {
  659.             $propertyFlags |= \ReflectionProperty::IS_PRIVATE;
  660.         }
  661.         if ($accessFlags self::ALLOW_PROTECTED) {
  662.             $propertyFlags |= \ReflectionProperty::IS_PROTECTED;
  663.         }
  664.         return $propertyFlags;
  665.     }
  666.     private function getReadVisiblityForProperty(\ReflectionProperty $reflectionProperty): string
  667.     {
  668.         if ($reflectionProperty->isPrivate()) {
  669.             return PropertyReadInfo::VISIBILITY_PRIVATE;
  670.         }
  671.         if ($reflectionProperty->isProtected()) {
  672.             return PropertyReadInfo::VISIBILITY_PROTECTED;
  673.         }
  674.         return PropertyReadInfo::VISIBILITY_PUBLIC;
  675.     }
  676.     private function getReadVisiblityForMethod(\ReflectionMethod $reflectionMethod): string
  677.     {
  678.         if ($reflectionMethod->isPrivate()) {
  679.             return PropertyReadInfo::VISIBILITY_PRIVATE;
  680.         }
  681.         if ($reflectionMethod->isProtected()) {
  682.             return PropertyReadInfo::VISIBILITY_PROTECTED;
  683.         }
  684.         return PropertyReadInfo::VISIBILITY_PUBLIC;
  685.     }
  686.     private function getWriteVisiblityForProperty(\ReflectionProperty $reflectionProperty): string
  687.     {
  688.         if ($reflectionProperty->isPrivate()) {
  689.             return PropertyWriteInfo::VISIBILITY_PRIVATE;
  690.         }
  691.         if ($reflectionProperty->isProtected()) {
  692.             return PropertyWriteInfo::VISIBILITY_PROTECTED;
  693.         }
  694.         return PropertyWriteInfo::VISIBILITY_PUBLIC;
  695.     }
  696.     private function getWriteVisiblityForMethod(\ReflectionMethod $reflectionMethod): string
  697.     {
  698.         if ($reflectionMethod->isPrivate()) {
  699.             return PropertyWriteInfo::VISIBILITY_PRIVATE;
  700.         }
  701.         if ($reflectionMethod->isProtected()) {
  702.             return PropertyWriteInfo::VISIBILITY_PROTECTED;
  703.         }
  704.         return PropertyWriteInfo::VISIBILITY_PUBLIC;
  705.     }
  706. }