From f7ed1440504570fe27c87879abfe8b700deb75d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 10 Sep 2018 22:30:12 +0200 Subject: [PATCH 1/3] [PropertyInfo] Add an extractor to guess if a property is initializable --- components/property_info.rst | 62 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/components/property_info.rst b/components/property_info.rst index 0b240497dc5..f70df4bdd91 100644 --- a/components/property_info.rst +++ b/components/property_info.rst @@ -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 added in Symfony 4.2. + Extractor Ordering ~~~~~~~~~~~~~~~~~~ @@ -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 five types of information: -* :ref:`List of properties `: `getProperties()` -* :ref:`Property type `: `getTypes()` -* :ref:`Property description `: `getShortDescription()` and `getLongDescription()` -* :ref:`Property access details `: `isReadable()` and `isWritable()` +* :ref:`List of properties `: :method:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface::getProperties()` +* :ref:`Property type `: :method:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface::getTypes()` +* :ref:`Property description `: :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getShortDescription()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyDescriptionExtractorInterface::getLongDescription()` +* :ref:`Property access details `: :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isReadable()` and :method:`Symfony\\Component\\PropertyInfo\\PropertyAccessExtractorInterface::isWritable()` +* :ref:`Property initializable through the constructor `: :method:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface::isInitializable()` .. note:: @@ -244,10 +252,26 @@ 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 five 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 @@ -350,7 +374,9 @@ 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 using +constructor's parameters. +It supports return and scalar types for PHP 7+. .. note:: @@ -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 ~~~~~~~~~~~~~~~ @@ -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`` From eebca4a87da37c831ac8719ee92488ef4444c571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 11 Sep 2018 00:27:23 +0200 Subject: [PATCH 2/3] RST --- components/property_info.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/property_info.rst b/components/property_info.rst index f70df4bdd91..f83a7f0b5dc 100644 --- a/components/property_info.rst +++ b/components/property_info.rst @@ -252,7 +252,7 @@ works. The support of hasser methods in the ``ReflectionExtractor`` class was introduced in Symfony 4.1. -.. _`property-info-initializable` +.. _property-info-initializable: Property Initializable Information ---------------------------------- From 8415f96cd2808aad20f61e67453b2f2af4d888d9 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 11 Sep 2018 09:17:13 +0200 Subject: [PATCH 3/3] Minor tweaks --- components/property_info.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/components/property_info.rst b/components/property_info.rst index f83a7f0b5dc..ca627bdcb03 100644 --- a/components/property_info.rst +++ b/components/property_info.rst @@ -78,7 +78,7 @@ provide it with a set of information extractors. .. versionadded:: 4.2 :class:`Symfony\\Component\\PropertyInfo\\PropertyInitializableExtractorInterface` - was added in Symfony 4.2. + was introduced in Symfony 4.2. Extractor Ordering ~~~~~~~~~~~~~~~~~~ @@ -127,7 +127,7 @@ Extractable Information ----------------------- The :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor` -class exposes public methods to extract five types of information: +class exposes public methods to extract several types of information: * :ref:`List of properties `: :method:`Symfony\\Component\\PropertyInfo\\PropertyListExtractorInterface::getProperties()` * :ref:`Property type `: :method:`Symfony\\Component\\PropertyInfo\\PropertyTypeExtractorInterface::getTypes()` @@ -265,13 +265,14 @@ provide whether properties are initializable through the class's constructor as $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. +: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 five 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 @@ -374,9 +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 give the type of a property, and if it is initializable through the constructor using -constructor's parameters. -It supports 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::