ReflectionExtractor.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface, PropertyInitializableExtractorInterface, PropertyReadInfoExtractorInterface, PropertyWriteInfoExtractorInterface, ConstructorArgumentTypeExtractorInterface
  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 = 1 << 0;
  50. /** @var int Allow magic __set methods */
  51. public const ALLOW_MAGIC_SET = 1 << 1;
  52. /** @var int Allow magic __call methods */
  53. public const ALLOW_MAGIC_CALL = 1 << 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 = null, bool $enableConstructorExtraction = true, int $accessFlags = self::ALLOW_PUBLIC, InflectorInterface $inflector = null, int $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->arrayMutatorPrefixes, array_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 $class, string $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 $class, string $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 $class, string $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 $class, string $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 $class, string $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 $class, string $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, $property, PropertyReadInfo::VISIBILITY_PUBLIC, false, false);
  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'.$camelProp, PropertyReadInfo::VISIBILITY_PUBLIC, false, false);
  261. }
  262. return null;
  263. }
  264. /**
  265. * {@inheritdoc}
  266. */
  267. public function getWriteInfo(string $class, string $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, $methodName, 1);
  308. if (!$accessible) {
  309. $errors[] = $methodAccessibleErrors;
  310. continue;
  311. }
  312. $method = $reflClass->getMethod($methodName);
  313. if (!\in_array($mutatorPrefix, $this->arrayMutatorPrefixes, true)) {
  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, $getsetter, 1);
  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, $property, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
  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'.$camelized, PropertyWriteInfo::VISIBILITY_PUBLIC, false);
  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 $class, string $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 (1 === \count($type) && \in_array($prefix, $this->arrayMutatorPrefixes)) {
  373. $type = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, 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 $class, string $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 $class, string $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 $class, string $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), null, Type::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, $nullable, null, true);
  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 $class, string $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 $class, string $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 $class, string $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 (0 === $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 $class, string $property): ?array
  530. {
  531. $ucProperty = ucfirst($property);
  532. $ucSingulars = $this->inflector->singularize($ucProperty);
  533. $mutatorPrefixes = \in_array($ucProperty, $ucSingulars, true) ? $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) && 2 !== \count($this->arrayMutatorPrefixes)) {
  585. return [null, null, []];
  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, $addMethod, 1);
  593. [$removeMethodFound, $removeMethodAccessibleErrors] = $this->isMethodAccessible($reflClass, $removeMethod, 1);
  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 [null, null, array_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 $class, string $methodName, int $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. }