Skip to content

Commit

Permalink
[Task] Substitute method exists with interface check in field defin…
Browse files Browse the repository at this point in the history
…itions (pimcore#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 <[email protected]>

* Update Fieldcollections.php

added comment to remove method_exists

* added BC layer

* Update models/DataObject/ClassDefinition/Data/Objectbricks.php

Co-authored-by: Sebastian Blank <[email protected]>

* Update models/DataObject/ClassDefinition/Data/Localizedfields.php

Co-authored-by: Sebastian Blank <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
Corepex and blankse authored Dec 20, 2022
1 parent 44b23c3 commit 80a0e3f
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 14 additions & 0 deletions models/DataObject/ClassDefinition/Data/ClassSavedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Pimcore\Model\DataObject\ClassDefinition\Data;

use Pimcore\Model\DataObject;

interface ClassSavedInterface
{
/**
* @param DataObject\ClassDefinition $class
* @param array $params
*/
public function classSaved($class/**, $params = [] **/);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Pimcore\Normalizer\NormalizerInterface;
use Pimcore\Tool;

class Classificationstore extends Data implements CustomResourcePersistingInterface, TypeDeclarationSupportInterface, NormalizerInterface, PreGetDataInterface, LayoutDefinitionEnrichmentInterface, VarExporterInterface
class Classificationstore extends Data implements CustomResourcePersistingInterface, TypeDeclarationSupportInterface, NormalizerInterface, PreGetDataInterface, LayoutDefinitionEnrichmentInterface, VarExporterInterface, ClassSavedInterface
{
use Element\ChildsCompatibilityTrait;

Expand Down Expand Up @@ -603,7 +603,7 @@ public function delete($object, $params = [])
* @param DataObject\ClassDefinition $class
* @param array $params
*/
public function classSaved($class, $params = [])
public function classSaved($class/**, $params = []**/)
{
$classificationStore = new DataObject\Classificationstore();
$classificationStore->setClass($class);
Expand Down
9 changes: 7 additions & 2 deletions models/DataObject/ClassDefinition/Data/Fieldcollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
7 changes: 6 additions & 1 deletion models/DataObject/ClassDefinition/Data/Localizedfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
11 changes: 7 additions & 4 deletions models/DataObject/ClassDefinition/Data/Objectbricks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
7 changes: 7 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 80a0e3f

Please sign in to comment.