| Conditions | 11 |
| Paths | 13 |
| Total Lines | 80 |
| Code Lines | 54 |
| Lines | 4 |
| Ratio | 5 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | foreach ($uow->getScheduledEntityInsertions() as $entity) { |
||
| 59 | $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
||
| 60 | if ($businessEntity) { |
||
| 61 | $this->updateBusinessPages( |
||
| 62 | $entity, |
||
| 63 | $businessEntity, |
||
| 64 | $entityManager, |
||
| 65 | $uow->getScheduledEntityDeletions() |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * get BusinessTemplate concerned by this entity (if so) |
||
| 73 | * then get BusinessPages |
||
| 74 | * for each BusinessPage, update its slug according to the new slug (if so). |
||
| 75 | * |
||
| 76 | * @param $entity |
||
| 77 | * @param BusinessEntity $businessEntity |
||
| 78 | * @param EntityManager $entityManager |
||
| 79 | * @param array $deletions |
||
| 80 | * |
||
| 81 | * @throws \Exception |
||
| 82 | * |
||
| 83 | * @internal param LifecycleEventArgs $eventArgs |
||
| 84 | */ |
||
| 85 | public function updateBusinessPages($entity, BusinessEntity $businessEntity, EntityManager $entityManager, $deletions) |
||
| 86 | { |
||
| 87 | $businessTemplates = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
||
| 88 | foreach ($businessTemplates as $businessTemplate) { |
||
| 89 | if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $entityManager)) { |
||
| 90 | /** @var BusinessPageRepository $bepRepo */ |
||
| 91 | $bepRepo = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessPage'); |
||
| 92 | $virtualBusinessPage = $this->businessPageBuilder->generateEntityPageFromTemplate( |
||
| 93 | $businessTemplate, |
||
| 94 | $entity, |
||
| 95 | $entityManager |
||
| 96 | ); |
||
| 97 | // Get the BusinessPage if exists for the given entity |
||
| 98 | /** @var BusinessPage $businessPage */ |
||
| 99 | $businessPage = $bepRepo->findPageByBusinessEntityAndPattern( |
||
| 100 | $businessTemplate, |
||
| 101 | $entity, |
||
| 102 | $businessEntity |
||
| 103 | ); |
||
| 104 | // If there is diff between persisted BEP and computed, persist the change |
||
| 105 | $scheduledForRemove = false; |
||
| 106 | foreach ($deletions as $deletion) { |
||
| 107 | if (get_class($deletion) == get_class($businessPage) |
||
| 108 | && $deletion->getId() === $businessPage->getId() |
||
| 109 | ) { |
||
| 110 | $scheduledForRemove = true; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($businessPage && !$scheduledForRemove) { |
||
| 115 | $oldSlug = $businessPage->getSlug(); |
||
| 116 | $newSlug = $entity->getSlug(); |
||
| 117 | $staticUrl = $businessPage->getStaticUrl(); |
||
| 118 | |||
| 119 | View Code Duplication | if ($staticUrl) { |
|
| 120 | $staticUrl = preg_replace('/'.$oldSlug.'/', $newSlug, $staticUrl); |
||
| 121 | $businessPage->setStaticUrl($staticUrl); |
||
| 122 | } |
||
| 123 | |||
| 124 | $businessPage->setName($virtualBusinessPage->getName()); |
||
| 125 | $businessPage->setSlug($virtualBusinessPage->getSlug()); |
||
| 126 | |||
| 127 | $entityManager->persist($businessPage); |
||
| 128 | $entityManager->flush(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: