Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 17 | class LinkExtension extends \Twig_Extension |
||
| 18 | { |
||
| 19 | protected $router; |
||
| 20 | protected $analytics; |
||
| 21 | protected $businessEntityHelper; // @victoire_business_page.business_entity_helper |
||
| 22 | protected $BusinessPageHelper; // @victoire_business_page.business_page_helper |
||
| 23 | protected $pageHelper; |
||
| 24 | protected $em; // @doctrine.orm.entity_manager |
||
| 25 | |||
| 26 | public function __construct( |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns a list of functions to add to the existing list. |
||
| 46 | * |
||
| 47 | * @return \Twig_SimpleFunction[] An array of functions |
||
| 48 | */ |
||
| 49 | public function getFunctions() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Generate the complete link (with the a tag). |
||
| 61 | * |
||
| 62 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
| 63 | * @param string $avoidRefresh Do we have to refresh or not ? |
||
| 64 | * @param array $url Fallback url |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function victoireLinkUrl($parameters, $avoidRefresh = true, $url = '#') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Generate the complete link (with the a tag). |
||
| 124 | * |
||
| 125 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
| 126 | * @param string $label link label |
||
| 127 | * @param array $attr custom attributes |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function victoireLink($parameters, $label, $attr = [], $currentClass = 'active', $url = '#') |
||
| 132 | { |
||
| 133 | $referenceLink = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 134 | $attachedWidget = isset($parameters['attachedWidget']) ? $parameters['attachedWidget'] : null; |
||
| 135 | |||
| 136 | if ($parameters['linkType'] == 'attachedWidget' && $attachedWidget && method_exists($attachedWidget->getView(), 'getUrl')) { |
||
| 137 | $viewUrl = $this->router->generate('victoire_core_page_show', ['_locale' => $attachedWidget->getView()->getLocale(), 'url' => $attachedWidget->getView()->getUrl()], $referenceLink); |
||
| 138 | if (rtrim($this->request->getRequestUri(), '/') == rtrim($viewUrl, '/')) { |
||
| 139 | $attr['data-scroll'] = 'smooth'; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | //Avoid to refresh page if not needed |
||
| 144 | if ($this->request->getRequestUri() == $this->victoireLinkUrl($parameters, false)) { |
||
| 145 | $this->addAttr('class', $currentClass, $attr); |
||
| 146 | } |
||
| 147 | |||
| 148 | //Build the target attribute |
||
| 149 | if ($parameters['target'] == 'ajax-modal') { |
||
| 150 | $attr['data-toggle'] = 'ajax-modal'; |
||
| 151 | } elseif ($parameters['target'] == '') { |
||
| 152 | $attr['target'] = '_parent'; |
||
| 153 | } else { |
||
| 154 | $attr['target'] = $parameters['target']; |
||
| 155 | } |
||
| 156 | |||
| 157 | //Add the analytics tracking code attribute |
||
| 158 | if (isset($parameters['analyticsTrackCode'])) { |
||
| 159 | $this->addAttr('onclick', $parameters['analyticsTrackCode'], $attr); |
||
| 160 | } |
||
| 161 | |||
| 162 | //Assemble and prepare attributes |
||
| 163 | $attributes = []; |
||
| 164 | View Code Duplication | foreach ($attr as $key => $_attr) { |
|
| 165 | if (is_array($_attr)) { |
||
| 166 | $attr = implode($_attr, ' '); |
||
| 167 | } else { |
||
| 168 | $attr = $_attr; |
||
| 169 | } |
||
| 170 | $attributes[] = $key.'="'.$attr.'"'; |
||
| 171 | } |
||
| 172 | |||
| 173 | $url = $this->victoireLinkUrl($parameters, true, $url); |
||
| 174 | //Creates a new twig environment |
||
| 175 | $twig = new \Twig_Environment(new \Twig_Loader_Array(['linkTemplate' => '{{ link|raw }}'])); |
||
| 176 | |||
| 177 | return $twig->render('linkTemplate', ['link' => '<a href="'.$url.'" '.implode($attributes, ' ').'>'.$label.'</a>']); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Generate the complete menu link item (with the li tag). |
||
| 182 | * |
||
| 183 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
| 184 | * @param string $label link label |
||
| 185 | * @param array $attr custom attributes |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function victoireMenuLink($parameters, $label, $attr = []) |
||
| 212 | |||
| 213 | public function victoireBusinessLink($businessEntityInstance, $templateId = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Add a given attribute to given attributes. |
||
| 239 | * |
||
| 240 | * @param string $label |
||
| 241 | * @param string $value |
||
| 242 | * @param array $attr The current attributes array |
||
| 243 | * |
||
| 244 | * @return LinkExtension |
||
| 245 | **/ |
||
| 246 | protected function addAttr($label, $value, &$attr) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the name of the extension. |
||
| 260 | * |
||
| 261 | * @return string The extension name |
||
| 262 | */ |
||
| 263 | public function getName() |
||
| 267 | } |
||
| 268 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.