| Conditions | 12 |
| Paths | 26 |
| Total Lines | 98 |
| Code Lines | 65 |
| Lines | 30 |
| Ratio | 30.61 % |
| Changes | 11 | ||
| Bugs | 2 | Features | 2 |
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 |
||
| 67 | public function generateEntityPageFromTemplate(BusinessTemplate $businessTemplate, $entity, EntityManager $em) |
||
| 68 | { |
||
| 69 | $viewTranslations = $em->getRepository(ViewTranslation::class)->getTranslationForView($businessTemplate); |
||
| 70 | $translations = []; |
||
| 71 | if (count($viewTranslations == 0)) { |
||
| 72 | return $this->legacyGenerateEntityPageFromTemplate($businessTemplate, $entity, $em); |
||
| 73 | } |
||
| 74 | $page = new VirtualBusinessPage(); |
||
| 75 | |||
| 76 | $pageLocale = $businessTemplate->getLocale(); |
||
| 77 | foreach (array_reverse($viewTranslations) as $viewTranslation) { |
||
| 78 | $page = new VirtualBusinessPage(); |
||
| 79 | $em->refresh($viewTranslation->getObject()->setTranslatableLocale($viewTranslation->getLocale())); |
||
| 80 | $reflect = new \ReflectionClass($businessTemplate); |
||
| 81 | $templateProperties = $reflect->getProperties(); |
||
| 82 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
| 83 | View Code Duplication | foreach ($templateProperties as $property) { |
|
| 84 | if (!in_array( |
||
| 85 | $property->getName(), |
||
| 86 | ['id', 'widgetMap', 'slots', 'seo', 'i18n', 'widgets'] |
||
| 87 | ) && !$property->isStatic() |
||
| 88 | ) { |
||
| 89 | $value = $accessor->getValue($viewTranslation->getObject(), $property->getName()); |
||
| 90 | $setMethod = 'set'.ucfirst($property->getName()); |
||
| 91 | if (method_exists($page, $setMethod)) { |
||
| 92 | $accessor->setValue($page, $property->getName(), $value); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | $translations[$viewTranslation->getLocale()]['page'] = $page; |
||
| 97 | } |
||
| 98 | |||
| 99 | //find Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity object according to the given $entity |
||
| 100 | $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
||
| 101 | |||
| 102 | if ($businessEntity !== null) { |
||
| 103 | //the business properties usable in a url |
||
| 104 | $businessProperties = $this->getBusinessProperties($businessEntity); |
||
| 105 | |||
| 106 | $entityProxy = $this->entityProxyProvider->getEntityProxy($entity, $businessEntity, $em); |
||
| 107 | |||
| 108 | $references = []; |
||
| 109 | foreach ($translations as $locale => $translation) { |
||
| 110 | /** @var VirtualBusinessPage $page */ |
||
| 111 | $page = $translation['page']; |
||
| 112 | //the url of the page |
||
| 113 | $pageUrl = $this->urlBuilder->buildUrl($page); |
||
| 114 | |||
| 115 | $pageName = $page->getName(); |
||
| 116 | $pageSlug = $page->getSlug(); |
||
| 117 | //parse the business properties |
||
| 118 | View Code Duplication | foreach ($businessProperties as $businessProperty) { |
|
| 119 | $pageUrl = $this->parameterConverter->setBusinessPropertyInstance( |
||
| 120 | $pageUrl, |
||
| 121 | $businessProperty, |
||
| 122 | $entity |
||
| 123 | ); |
||
| 124 | $pageSlug = $this->parameterConverter->setBusinessPropertyInstance( |
||
| 125 | $pageSlug, |
||
| 126 | $businessProperty, |
||
| 127 | $entity |
||
| 128 | ); |
||
| 129 | $pageName = $this->parameterConverter->setBusinessPropertyInstance( |
||
| 130 | $pageName, |
||
| 131 | $businessProperty, |
||
| 132 | $entity |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | //we update the url of the page |
||
| 136 | $page->setTranslatableLocale($locale); |
||
| 137 | $page->setUrl($pageUrl); |
||
| 138 | $page->setSlug($pageSlug); |
||
| 139 | $page->setName($pageName); |
||
| 140 | $page->setEntityProxy($entityProxy); |
||
| 141 | $page->setTemplate($businessTemplate); |
||
| 142 | |||
| 143 | //Check that all twig variables in pattern url was removed for it's generated BusinessPage |
||
| 144 | preg_match_all('/\{\%\s*([^\%\}]*)\s*\%\}|\{\{\s*([^\}\}]*)\s*\}\}/i', $pageUrl, $matches); |
||
| 145 | |||
| 146 | if (count($matches[2])) { |
||
| 147 | throw new IdentifierNotDefinedException($matches[2]); |
||
| 148 | } |
||
| 149 | $translation['page'] = $page; |
||
| 150 | $references[$locale] = $this->viewReferenceBuilder->buildViewReference($page, $em); |
||
| 151 | $translations[$locale] = $translation; |
||
| 152 | } |
||
| 153 | $firstTranslation = $translations[$pageLocale]; |
||
| 154 | $page = $firstTranslation['page']; |
||
| 155 | $page->setReferences($references); |
||
| 156 | |||
| 157 | if ($seo = $businessTemplate->getSeo()) { |
||
| 158 | $pageSeo = clone $seo; |
||
| 159 | $page->setSeo($pageSeo); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $page; |
||
| 164 | } |
||
| 165 | |||
| 309 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.