Skip to content

[PropertyInfo] Add an extractor to guess if a property is initializable #10319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 47 additions & 15 deletions components/property_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,36 @@ provide it with a set of information extractors.
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();

// array of PropertyListExtractorInterface
// list of PropertyListExtractorInterface (any iterable)
$listExtractors = array($reflectionExtractor);

// array of PropertyTypeExtractorInterface
// list of PropertyTypeExtractorInterface (any iterable)
$typeExtractors = array($phpDocExtractor, $reflectionExtractor);

// array of PropertyDescriptionExtractorInterface
// list of PropertyDescriptionExtractorInterface (any iterable)
$descriptionExtractors = array($phpDocExtractor);

// array of PropertyAccessExtractorInterface
$accessExtractors = array($reflectionExtractor);
// list of PropertyInitializableExtractorInterface (any iterable)
$propertyInitializableExtractors = array($reflectionExtractor);

// list of PropertyAccessExtractorInterface (any iterable)

$propertyInfo = new PropertyInfoExtractor(
$listExtractors,
$typeExtractors,
$descriptionExtractors,
$accessExtractors
$accessExtractors,
$propertyInitializableExtractors
);

// see below for more examples
$class = YourAwesomeCoolClass::class;
$properties = $propertyInfo->getProperties($class);

.. versionadded:: 4.2
:class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`
was introduced in Symfony 4.2.

Extractor Ordering
~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -120,12 +127,13 @@ Extractable Information
-----------------------

The :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor`
class exposes public methods to extract four types of information:
class exposes public methods to extract several types of information:

* :ref:`List of properties <property-info-list>`: `getProperties()`
* :ref:`Property type <property-info-type>`: `getTypes()`
* :ref:`Property description <property-info-description>`: `getShortDescription()` and `getLongDescription()`
* :ref:`Property access details <property-info-access>`: `isReadable()` and `isWritable()`
* :ref:`List of properties <property-info-list>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface::getProperties()`
* :ref:`Property type <property-info-type>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface::getTypes()`
* :ref:`Property description <property-info-description>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getShortDescription()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getLongDescription()`
* :ref:`Property access details <property-info-access>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isReadable()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isWritable()`
* :ref:`Property initializable through the constructor <property-info-initializable>`: :method:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface::isInitializable()`

.. note::

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

.. _property-info-initializable:

Property Initializable Information
----------------------------------

Extractors that implement :class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`
provide whether properties are initializable through the class's constructor as booleans.

.. code-block:: php

$propertyInfo->isInitializable($class, $property);
// Example Result: bool(true)

:method:`Symfony\\Component\\PropertyInfo\\Extractor\\ReflectionExtractor::isInitializable`
returns ``true`` if a constructor's parameter of the given class matches the
given property name.

.. tip::

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

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

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

.. note::

Expand All @@ -372,12 +398,17 @@ It can also provide return and scalar types for PHP 7+.

// List information.
$reflectionExtractor->getProperties($class);

// Type information.
$reflectionExtractor->getTypes($class, $property);

// Access information.
$reflectionExtractor->isReadable($class, $property);
$reflectionExtractor->isWritable($class, $property);

// Initializable information
$reflectionExtractor->isInitializable($class, $property);

PhpDocExtractor
~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -471,8 +502,9 @@ You can create your own property information extractors by creating a
class that implements one or more of the following interfaces:
:class:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface`,
:class:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface`,
:class:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface`
and :class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface`.
:class:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface`,
:class:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface` and
:class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface`.

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