From 80a0e3f8bd3e6cacc4b660abc12a63d24f454444 Mon Sep 17 00:00:00 2001 From: Christian F Date: Tue, 20 Dec 2022 09:41:02 +0100 Subject: [PATCH] [Task] Substitute `method exists` with interface check in field definitions (#13846) * added ClassSavedInterface to several classes, removed method_exists * Update AdvancedManyToManyRelation.php added $params parameter * Update models/DataObject/ClassDefinition/Data/Fieldcollections.php added BC layer Co-authored-by: Sebastian Blank * Update Fieldcollections.php added comment to remove method_exists * added BC layer * Update models/DataObject/ClassDefinition/Data/Objectbricks.php Co-authored-by: Sebastian Blank * Update models/DataObject/ClassDefinition/Data/Localizedfields.php Co-authored-by: Sebastian Blank * passed parameters to classSaved function * removed interface from classes * removed interface from classes * Update models/DataObject/ClassDefinition/Data/Fieldcollections.php Co-authored-by: Sebastian Blank * added upgrade notes * changed interface * added type * updated upgrade notes * added unknown parameter to ignore list * added unknown parameter to ignore list * fixed phpstan formatting * fixed phpstan formatting * changed condition order Co-authored-by: Sebastian Blank --- .../09_Upgrade_Notes/README.md | 1 + .../Data/AdvancedManyToManyObjectRelation.php | 4 ++-- .../Data/AdvancedManyToManyRelation.php | 4 ++-- .../ClassDefinition/Data/ClassSavedInterface.php | 14 ++++++++++++++ .../ClassDefinition/Data/Classificationstore.php | 4 ++-- .../ClassDefinition/Data/Fieldcollections.php | 9 +++++++-- .../ClassDefinition/Data/Localizedfields.php | 7 ++++++- .../ClassDefinition/Data/Objectbricks.php | 11 +++++++---- phpstan-baseline.neon | 7 +++++++ 9 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 models/DataObject/ClassDefinition/Data/ClassSavedInterface.php diff --git a/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md b/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md index e04e5638415..4eefe38ffdf 100644 --- a/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md +++ b/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md @@ -8,6 +8,7 @@ This method will return the field/column name for the current version and provide a way to support both version for bundles. E.g. passing `o_id` in Pimcore 10 will return `o_id`, but `id` in Pimcore 11. - [Ecommerce] Elasticsearch 7 support has been deprecated, elasticsearch 8 supported was added. +- [ClassSavedInterface] Introduced additional interface implementing the `classSaved` method. The interface will be used by field definitions in `Pimcore\Model\DataObject\ClassDefinition\Data\*`. If your custom field definition implements the `classSaved` method, please use the `ClassSavedInterface` interface. Make sure that you either provide a default value (e.g. `$params = []`) for `$params` or don't use a second parameter in the method signature at all. Note that using the `classSaved` method without implementing the interface is deprecated and won't work in Pimcore 11. ## 10.5.10 diff --git a/models/DataObject/ClassDefinition/Data/AdvancedManyToManyObjectRelation.php b/models/DataObject/ClassDefinition/Data/AdvancedManyToManyObjectRelation.php index a57b89acad8..8afed6fe3b4 100644 --- a/models/DataObject/ClassDefinition/Data/AdvancedManyToManyObjectRelation.php +++ b/models/DataObject/ClassDefinition/Data/AdvancedManyToManyObjectRelation.php @@ -25,7 +25,7 @@ /** * @method DataObject\Data\ObjectMetadata\Dao getDao() */ -class AdvancedManyToManyObjectRelation extends ManyToManyObjectRelation implements IdRewriterInterface, PreGetDataInterface, LayoutDefinitionEnrichmentInterface +class AdvancedManyToManyObjectRelation extends ManyToManyObjectRelation implements IdRewriterInterface, PreGetDataInterface, LayoutDefinitionEnrichmentInterface, ClassSavedInterface { use DataObject\Traits\ElementWithMetadataComparisonTrait; use DataObject\ClassDefinition\Data\Extension\PositionSortTrait; @@ -757,7 +757,7 @@ public function setEnableBatchEdit($enableBatchEdit) * @param DataObject\ClassDefinition $class * @param array $params */ - public function classSaved($class, $params = []) + public function classSaved($class/**, $params = []**/) { /** @var DataObject\Data\ObjectMetadata $temp */ $temp = \Pimcore::getContainer()->get('pimcore.model.factory') diff --git a/models/DataObject/ClassDefinition/Data/AdvancedManyToManyRelation.php b/models/DataObject/ClassDefinition/Data/AdvancedManyToManyRelation.php index 9f27b240187..8a66f4037ab 100644 --- a/models/DataObject/ClassDefinition/Data/AdvancedManyToManyRelation.php +++ b/models/DataObject/ClassDefinition/Data/AdvancedManyToManyRelation.php @@ -23,7 +23,7 @@ use Pimcore\Model\Document; use Pimcore\Model\Element; -class AdvancedManyToManyRelation extends ManyToManyRelation implements IdRewriterInterface, PreGetDataInterface +class AdvancedManyToManyRelation extends ManyToManyRelation implements IdRewriterInterface, PreGetDataInterface, ClassSavedInterface { use DataObject\Traits\ElementWithMetadataComparisonTrait; use DataObject\ClassDefinition\Data\Extension\PositionSortTrait; @@ -775,7 +775,7 @@ public function getColumnKeys() /** * @param DataObject\ClassDefinition $class */ - public function classSaved($class) + public function classSaved($class/**, $params = [] **/) { /** @var DataObject\Data\ElementMetadata $temp */ $temp = \Pimcore::getContainer()->get('pimcore.model.factory') diff --git a/models/DataObject/ClassDefinition/Data/ClassSavedInterface.php b/models/DataObject/ClassDefinition/Data/ClassSavedInterface.php new file mode 100644 index 00000000000..49c10703790 --- /dev/null +++ b/models/DataObject/ClassDefinition/Data/ClassSavedInterface.php @@ -0,0 +1,14 @@ +setClass($class); diff --git a/models/DataObject/ClassDefinition/Data/Fieldcollections.php b/models/DataObject/ClassDefinition/Data/Fieldcollections.php index af1b3bc45d1..6fbdf93fc78 100644 --- a/models/DataObject/ClassDefinition/Data/Fieldcollections.php +++ b/models/DataObject/ClassDefinition/Data/Fieldcollections.php @@ -687,9 +687,14 @@ public function classSaved($class, $params = []) foreach ($fieldDefinition as $fd) { //TODO Pimcore 11 remove method_exists call - if (!$fd instanceof DataContainerAwareInterface && method_exists($fd, 'classSaved')) { + if (!$fd instanceof DataContainerAwareInterface && ($fd instanceof ClassSavedInterface || method_exists($fd, 'classSaved'))) { + if (!$fd instanceof ClassSavedInterface) { + trigger_deprecation('pimcore/pimcore', '10.6', + sprintf('Usage of method_exists is deprecated since version 10.6 and will be removed in Pimcore 11.' . + 'Implement the %s interface instead.', ClassSavedInterface::class)); + } // defer creation - $fd->classSaved($class); + $fd->classSaved($class, $params); } } diff --git a/models/DataObject/ClassDefinition/Data/Localizedfields.php b/models/DataObject/ClassDefinition/Data/Localizedfields.php index c3237572bca..3cce4e81895 100644 --- a/models/DataObject/ClassDefinition/Data/Localizedfields.php +++ b/models/DataObject/ClassDefinition/Data/Localizedfields.php @@ -572,7 +572,12 @@ public function classSaved($class, $params = []) foreach ($this->getFieldDefinitions() as $fd) { //TODO Pimcore 11 remove method_exists call - if (!$fd instanceof DataContainerAwareInterface && method_exists($fd, 'classSaved')) { + if (!$fd instanceof DataContainerAwareInterface && ($fd instanceof ClassSavedInterface || method_exists($fd, 'classSaved'))) { + if (!$fd instanceof ClassSavedInterface) { + trigger_deprecation('pimcore/pimcore', '10.6', + sprintf('Usage of method_exists is deprecated since version 10.6 and will be removed in Pimcore 11.' . + 'Implement the %s interface instead.', ClassSavedInterface::class)); + } $fd->classSaved($class, $params); } } diff --git a/models/DataObject/ClassDefinition/Data/Objectbricks.php b/models/DataObject/ClassDefinition/Data/Objectbricks.php index 3f66adf2c7a..3096125f292 100644 --- a/models/DataObject/ClassDefinition/Data/Objectbricks.php +++ b/models/DataObject/ClassDefinition/Data/Objectbricks.php @@ -903,11 +903,14 @@ public function classSaved($class, $params = []) foreach ($fieldDefinition as $fd) { //TODO Pimcore 11 remove method_exists call - if (method_exists($fd, 'classSaved')) { - // defer creation - if (!$fd instanceof DataContainerAwareInterface) { - $fd->classSaved($class); + if (!$fd instanceof DataContainerAwareInterface && ($fd instanceof ClassSavedInterface || method_exists($fd, 'classSaved'))) { + if (!$fd instanceof ClassSavedInterface) { + trigger_deprecation('pimcore/pimcore', '10.6', + sprintf('Usage of method_exists is deprecated since version 10.6 and will be removed in Pimcore 11.' . + 'Implement the %s interface instead.', ClassSavedInterface::class)); } + // defer creation + $fd->classSaved($class, $params); } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d7abf87562c..7cfff68c51d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -309,3 +309,10 @@ parameters: message: "#^PHPDoc type Elastic\\\\Elasticsearch\\\\Client\\|null of property Pimcore\\\\Bundle\\\\EcommerceFrameworkBundle\\\\IndexService\\\\Worker\\\\ElasticSearch\\\\DefaultElasticSearch8\\:\\:\\$elasticSearchClient is not covariant with PHPDoc type Elasticsearch\\\\Client\\|null of overridden property Pimcore\\\\Bundle\\\\EcommerceFrameworkBundle\\\\IndexService\\\\Worker\\\\ElasticSearch\\\\AbstractElasticSearch\\:\\:\\$elasticSearchClient\\.$#" count: 1 path: bundles/EcommerceFrameworkBundle/IndexService/Worker/ElasticSearch/DefaultElasticSearch8.php + + - + message: "#^PHPDoc tag \\@param references unknown parameter\\: \\$params$#" + paths: + - models/DataObject/ClassDefinition/Data/AdvancedManyToManyObjectRelation.php + - models/DataObject/ClassDefinition/Data/ClassSavedInterface.php + - models/DataObject/ClassDefinition/Data/Classificationstore.php \ No newline at end of file