Skip to content

Commit 5bc7ac0

Browse files
committed
minor #10319 [PropertyInfo] Add an extractor to guess if a property is initializable (dunglas, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- [PropertyInfo] Add an extractor to guess if a property is initializable symfony/symfony#26997 symfony/symfony#24571 Closes #10272. Commits ------- 8415f96 Minor tweaks eebca4a RST f7ed144 [PropertyInfo] Add an extractor to guess if a property is initializable
2 parents fb3b7bd + 8415f96 commit 5bc7ac0

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

components/property_info.rst

+47-15
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,36 @@ provide it with a set of information extractors.
5050
$phpDocExtractor = new PhpDocExtractor();
5151
$reflectionExtractor = new ReflectionExtractor();
5252
53-
// array of PropertyListExtractorInterface
53+
// list of PropertyListExtractorInterface (any iterable)
5454
$listExtractors = array($reflectionExtractor);
5555
56-
// array of PropertyTypeExtractorInterface
56+
// list of PropertyTypeExtractorInterface (any iterable)
5757
$typeExtractors = array($phpDocExtractor, $reflectionExtractor);
5858
59-
// array of PropertyDescriptionExtractorInterface
59+
// list of PropertyDescriptionExtractorInterface (any iterable)
6060
$descriptionExtractors = array($phpDocExtractor);
6161
62-
// array of PropertyAccessExtractorInterface
63-
$accessExtractors = array($reflectionExtractor);
62+
// list of PropertyInitializableExtractorInterface (any iterable)
63+
$propertyInitializableExtractors = array($reflectionExtractor);
64+
65+
// list of PropertyAccessExtractorInterface (any iterable)
6466
6567
$propertyInfo = new PropertyInfoExtractor(
6668
$listExtractors,
6769
$typeExtractors,
6870
$descriptionExtractors,
69-
$accessExtractors
71+
$accessExtractors,
72+
$propertyInitializableExtractors
7073
);
7174
7275
// see below for more examples
7376
$class = YourAwesomeCoolClass::class;
7477
$properties = $propertyInfo->getProperties($class);
7578
79+
.. versionadded:: 4.2
80+
:class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`
81+
was introduced in Symfony 4.2.
82+
7683
Extractor Ordering
7784
~~~~~~~~~~~~~~~~~~
7885

@@ -120,12 +127,13 @@ Extractable Information
120127
-----------------------
121128

122129
The :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor`
123-
class exposes public methods to extract four types of information:
130+
class exposes public methods to extract several types of information:
124131

125-
* :ref:`List of properties <property-info-list>`: `getProperties()`
126-
* :ref:`Property type <property-info-type>`: `getTypes()`
127-
* :ref:`Property description <property-info-description>`: `getShortDescription()` and `getLongDescription()`
128-
* :ref:`Property access details <property-info-access>`: `isReadable()` and `isWritable()`
132+
* :ref:`List of properties <property-info-list>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface::getProperties()`
133+
* :ref:`Property type <property-info-type>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface::getTypes()`
134+
* :ref:`Property description <property-info-description>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getShortDescription()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getLongDescription()`
135+
* :ref:`Property access details <property-info-access>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isReadable()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isWritable()`
136+
* :ref:`Property initializable through the constructor <property-info-initializable>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface::isInitializable()`
129137

130138
.. note::
131139

@@ -244,10 +252,27 @@ works.
244252
The support of hasser methods in the ``ReflectionExtractor`` class was
245253
introduced in Symfony 4.1.
246254

255+
.. _property-info-initializable:
256+
257+
Property Initializable Information
258+
----------------------------------
259+
260+
Extractors that implement :class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`
261+
provide whether properties are initializable through the class's constructor as booleans.
262+
263+
.. code-block:: php
264+
265+
$propertyInfo->isInitializable($class, $property);
266+
// Example Result: bool(true)
267+
268+
:method:`Symfony\\Component\\PropertyInfo\\Extractor\\ReflectionExtractor::isInitializable`
269+
returns ``true`` if a constructor's parameter of the given class matches the
270+
given property name.
271+
247272
.. tip::
248273

249274
The main :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor`
250-
class implements all four interfaces, delegating the extraction of property
275+
class implements all interfaces, delegating the extraction of property
251276
information to the extractors that have been registered with it.
252277

253278
This means that any method available on each of the extractors is also
@@ -350,7 +375,8 @@ ReflectionExtractor
350375

351376
Using PHP reflection, the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\ReflectionExtractor`
352377
provides list, type and access information from setter and accessor methods.
353-
It can also provide return and scalar types for PHP 7+.
378+
It can also give the type of a property, and if it is initializable through the
379+
constructor. It supports return and scalar types for PHP 7.
354380

355381
.. note::
356382

@@ -372,12 +398,17 @@ It can also provide return and scalar types for PHP 7+.
372398
373399
// List information.
374400
$reflectionExtractor->getProperties($class);
401+
375402
// Type information.
376403
$reflectionExtractor->getTypes($class, $property);
404+
377405
// Access information.
378406
$reflectionExtractor->isReadable($class, $property);
379407
$reflectionExtractor->isWritable($class, $property);
380408
409+
// Initializable information
410+
$reflectionExtractor->isInitializable($class, $property);
411+
381412
PhpDocExtractor
382413
~~~~~~~~~~~~~~~
383414

@@ -471,8 +502,9 @@ You can create your own property information extractors by creating a
471502
class that implements one or more of the following interfaces:
472503
:class:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface`,
473504
:class:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface`,
474-
:class:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface`
475-
and :class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface`.
505+
:class:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface`,
506+
:class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface` and
507+
:class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`.
476508

477509
If you have enabled the PropertyInfo component with the FrameworkBundle,
478510
you can automatically register your extractor class with the ``property_info``

0 commit comments

Comments
 (0)