Issue Bug Closed

#103456AbstractRestrictionContainer::removeByType() causes problems with xclassed restrictions.

Ole Trenner 03/21/24, 13:46 updated 07/09/24, 15:38

Description

The current implementation of AbstractRestrictionContainer::removeByType() removes values from the restrictions and enforcedRestrictions arrays by unsetting the restriction-type as specified by the parameter:

unset($this->restrictions[$restrictionType], $this->enforcedRestrictions[$restrictionType]);

This causes problems when a restriction is xclassed. The common approach to disable default restrictions is something like this:

$queryBuilder->getRestrictions()
->removeByType(HiddenRestriction::class)
->removeByType(StartTimeRestriction::class)
->removeByType(EndTimeRestriction::class);

and this would fail to remove an xclassed restriction with the current implementation.

An alternative approach might be to iterate over the array values and check if they are instances of the specified type:

public function removeByType(string $restrictionType): QueryRestrictionContainerInterface
{
foreach ($this->restrictions as $type => $instance) {
if ($instance instanceof $restrictionType) {
unset($this->restrictions[$type]);
}
}
foreach ($this->enforcedRestrictions as $type => $instance) {
if ($instance instanceof $restrictionType) {
unset($this->enforcedRestrictions[$type]);
}
}
return $this;
}

This approach would work both for the original and possible xclassed restrictions.

Thank you :)
Ole.

Reviews 3

History 27

  1. Gerrit Code Review pushed patch set 1 for main to change 83562 status New Under Review 03/21/24, 17:07

  2. Gerrit Code Review pushed patch set 1 for main to change 83568 03/22/24, 09:32

  3. Gerrit Code Review pushed patch set 2 for main to change 83562 03/22/24, 09:35

  4. Gerrit Code Review pushed patch set 1 for 12.4 to change 83539 03/22/24, 12:00

  5. Gerrit Code Review pushed patch set 2 for 12.4 to change 83539 03/22/24, 12:48

  6. Oliver Bartsch commented and changed status Under Review Resolved done 0 % 100 % 03/22/24, 15:25

    Applied in changeset commit:1350e2b8b35159f44e3930c3ae0a59a1f35839a2.

  7. Benni Mack changed status Resolved Closed 07/09/24, 15:38