From c2061110ddef45e43f6fd78f2634d4ecd8c22787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 26 Apr 2022 21:16:55 +0200 Subject: [PATCH] fix cs --- .../ChillActivityBundle/Entity/Activity.php | 23 ++-- .../Tests/Entity/ActivityTest.php | 5 +- .../Entity/SocialWork/SocialAction.php | 110 +++++++++--------- 3 files changed, 70 insertions(+), 68 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index d77e0250b..eabaa44d1 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -35,7 +35,6 @@ use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\SerializedName; -use function count; /** * Class Activity. @@ -238,23 +237,14 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this; } - private function ensureSocialActionConsistency(): void - { - $ancestors = SocialAction::findAncestorSocialActions($this->getSocialActions()); - - foreach ($ancestors as $ancestor) { - $this->removeSocialAction($ancestor); - } - } - /** - * Add a social issue + * Add a social issue. * * Note: the social issue consistency (the fact that only yougest social issues * are kept) is processed by an entity listener: + * * @see{\Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodSocialIssueConsistencyEntityListener} * - * @param SocialIssue $socialIssue * @return $this */ public function addSocialIssue(SocialIssue $socialIssue): self @@ -652,4 +642,13 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this; } + + private function ensureSocialActionConsistency(): void + { + $ancestors = SocialAction::findAncestorSocialActions($this->getSocialActions()); + + foreach ($ancestors as $ancestor) { + $this->removeSocialAction($ancestor); + } + } } diff --git a/src/Bundle/ChillActivityBundle/Tests/Entity/ActivityTest.php b/src/Bundle/ChillActivityBundle/Tests/Entity/ActivityTest.php index a14ac4a24..7a68a36fc 100644 --- a/src/Bundle/ChillActivityBundle/Tests/Entity/ActivityTest.php +++ b/src/Bundle/ChillActivityBundle/Tests/Entity/ActivityTest.php @@ -20,6 +20,10 @@ use Doctrine\ORM\Event\LifecycleEventArgs; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +/** + * @internal + * @coversNothing + */ final class ActivityTest extends TestCase { use ProphecyTrait; @@ -107,5 +111,4 @@ final class ActivityTest extends TestCase $this->assertNotContains($parent, $activity->getSocialIssues()); $this->assertNotContains($child, $activity->getSocialIssues()); } - } diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index 198950494..904950489 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -128,6 +128,44 @@ class SocialAction return $this; } + /** + * In a SocialIssues's collection, find the elements which are an ancestor of + * other elements. + * + * The difference of the given list (thus, the elements which are **not** kept + * in the returned collection) are the most-grand-child elements of the list. + * + * Removing those elements of the Collection (which is not done by this method) + * will ensure that only the most descendent elements are present in the collection, + * (any ancestor of another element are present). + * + * @param Collection|SocialAction[] $socialActions + * + * @return Collection|SocialAction[] a list with the elements of the given list which are parent of other elements in the given list + */ + public static function findAncestorSocialActions(Collection $socialActions): Collection + { + $ancestors = new ArrayCollection(); + + foreach ($socialActions as $candidateChild) { + if ($ancestors->contains($candidateChild)) { + continue; + } + + foreach ($socialActions as $candidateParent) { + if ($ancestors->contains($candidateParent)) { + continue; + } + + if ($candidateChild->isDescendantOf($candidateParent)) { + $ancestors->add($candidateParent); + } + } + } + + return $ancestors; + } + /** * @return Collection|self[] */ @@ -233,6 +271,23 @@ class SocialAction return $this->getParent() instanceof self; } + /** + * Recursive method which return true if the current $action + * is a descendant of the $action given in parameter. + */ + public function isDescendantOf(SocialAction $action): bool + { + if (!$this->hasParent()) { + return false; + } + + if ($this->getParent() === $action) { + return true; + } + + return $this->getParent()->isDescendantOf($action); + } + public function removeChild(self $child): self { if ($this->children->removeElement($child)) { @@ -307,59 +362,4 @@ class SocialAction return $this; } - - /** - * Recursive method which return true if the current $action - * is a descendant of the $action given in parameter. - */ - public function isDescendantOf(SocialAction $action): bool - { - if (!$this->hasParent()) { - return false; - } - - if ($this->getParent() === $action) { - return true; - } - - return $this->getParent()->isDescendantOf($action); - } - - /** - * In a SocialIssues's collection, find the elements which are an ancestor of - * other elements. - * - * The difference of the given list (thus, the elements which are **not** kept - * in the returned collection) are the most-grand-child elements of the list. - * - * Removing those elements of the Collection (which is not done by this method) - * will ensure that only the most descendent elements are present in the collection, - * (any ancestor of another element are present). - * - * @param Collection|SocialAction[] $socialActions - * - * @return Collection|SocialAction[] a list with the elements of the given list which are parent of other elements in the given list - */ - public static function findAncestorSocialActions(Collection $socialActions): Collection - { - $ancestors = new ArrayCollection(); - - foreach ($socialActions as $candidateChild) { - if ($ancestors->contains($candidateChild)) { - continue; - } - - foreach ($socialActions as $candidateParent) { - if ($ancestors->contains($candidateParent)) { - continue; - } - - if ($candidateChild->isDescendantOf($candidateParent)) { - $ancestors->add($candidateParent); - } - } - } - - return $ancestors; - } }