Debian Codegraph @

lib/3rdParty/composer/symfony/property-info/PropertyInfoExtractor.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\PropertyInfo;
13
14 use Symfony\Component\PropertyInfo\Util\LegacyTypeConverter;
15 use Symfony\Component\TypeInfo\Type;
16
17 /**
18 * Default {@see PropertyInfoExtractorInterface} implementation.
19 *
20 * @author Kévin Dunglas <[email protected]>
21 *
22 * @final
23 */
24 class PropertyInfoExtractor implements PropertyInfoExtractorInterface, PropertyInitializableExtractorInterface
25 {
26 /**
27 * @param iterable<mixed, PropertyListExtractorInterface> $listExtractors
28 * @param iterable<mixed, PropertyTypeExtractorInterface> $typeExtractors
29 * @param iterable<mixed, PropertyDescriptionExtractorInterface> $descriptionExtractors
30 * @param iterable<mixed, PropertyAccessExtractorInterface> $accessExtractors
31 * @param iterable<mixed, PropertyInitializableExtractorInterface> $initializableExtractors
32 */
33 public function __construct(
34 private readonly iterable $listExtractors = [],
35 private readonly iterable $typeExtractors = [],
36 private readonly iterable $descriptionExtractors = [],
37 private readonly iterable $accessExtractors = [],
38 private readonly iterable $initializableExtractors = [],
39 ) {
40 }
41
42 public function getProperties(string $class, array $context = []): ?array
43 {
44 return $this->extract($this->listExtractors, 'getProperties', [$class, $context]);
45 }
46
47 public function getShortDescription(string $class, string $property, array $context = []): ?string
48 {
49 return $this->extract($this->descriptionExtractors, 'getShortDescription', [$class, $property, $context]);
50 }
51
52 public function getLongDescription(string $class, string $property, array $context = []): ?string
53 {
54 return $this->extract($this->descriptionExtractors, 'getLongDescription', [$class, $property, $context]);
55 }
56
57 public function getType(string $class, string $property, array $context = []): ?Type
58 {
59 foreach ($this->typeExtractors as $extractor) {
60 if (!method_exists($extractor, 'getType')) {
61 $legacyTypes = $extractor->getTypes($class, $property, $context);
62
63 if (null !== $legacyTypes) {
64 return LegacyTypeConverter::toTypeInfoType($legacyTypes);
65 }
66
67 continue;
68 }
69
70 if (null !== $value = $extractor->getType($class, $property, $context)) {
71 return $value;
72 }
73 }
74
75 return null;
76 }
77
78 /**
79 * @deprecated since Symfony 7.3, use "getType" instead
80 */
81 public function getTypes(string $class, string $property, array $context = []): ?array
82 {
83 trigger_deprecation('symfony/property-info', '7.3', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);
84
85 return $this->extract($this->typeExtractors, 'getTypes', [$class, $property, $context]);
86 }
87
88 public function isReadable(string $class, string $property, array $context = []): ?bool
89 {
90 return $this->extract($this->accessExtractors, 'isReadable', [$class, $property, $context]);
91 }
92
93 public function isWritable(string $class, string $property, array $context = []): ?bool
94 {
95 return $this->extract($this->accessExtractors, 'isWritable', [$class, $property, $context]);
96 }
97
98 public function isInitializable(string $class, string $property, array $context = []): ?bool
99 {
100 return $this->extract($this->initializableExtractors, 'isInitializable', [$class, $property, $context]);
101 }
102
103 /**
104 * Iterates over registered extractors and return the first value found.
105 *
106 * @param iterable<mixed, object> $extractors
107 * @param list<mixed> $arguments
108 */
109 private function extract(iterable $extractors, string $method, array $arguments): mixed
110 {
111 foreach ($extractors as $extractor) {
112 if (null !== $value = $extractor->{$method}(...$arguments)) {
113 return $value;
114 }
115 }
116
117 return null;
118 }
119 }