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 ArticleTwigExtension extends AbstractExtension |
||
18 | { |
||
19 | /** |
||
20 | * @var EntityManagerInterface |
||
21 | */ |
||
22 | private $em; |
||
23 | |||
24 | /** |
||
25 | * @var RouterInterface |
||
26 | */ |
||
27 | private $router; |
||
28 | |||
29 | /** |
||
30 | * ArticleTwigExtension constructor. |
||
31 | * |
||
32 | * @param EntityManagerInterface $em |
||
33 | * @param RouterInterface $router |
||
34 | */ |
||
35 | public function __construct(EntityManagerInterface $em, RouterInterface $router) |
||
36 | { |
||
37 | $this->em = $em; |
||
38 | $this->router = $router; |
||
39 | |||
40 | if (\func_num_args() > 2) { |
||
41 | @trigger_error(sprintf('Passing the "request_stack" service as the third argument in "%s" is deprecated in KunstmaanArticleBundle 5.1 and will be removed in KunstmaanArticleBundle 6.0. Remove the "request_stack" argument from your service definition.', __METHOD__), E_USER_DEPRECATED); |
||
|
|||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Returns a list of functions to add to the existing list. |
||
47 | * |
||
48 | * @return array An array of functions |
||
49 | */ |
||
50 | public function getFunctions() |
||
67 | |||
68 | /** |
||
69 | * Get tags array for view. |
||
70 | * |
||
71 | * @param Request $request |
||
72 | * @param string $className |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | View Code Duplication | public function getTags(Request $request, $className) |
|
77 | { |
||
78 | $context = array(); |
||
79 | |||
80 | $tagRepository = $this->em->getRepository($className); |
||
81 | $context['tags'] = $tagRepository->findBy(array(), array('name' => 'ASC')); |
||
82 | |||
83 | $searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null; |
||
84 | if ($searchTag) { |
||
85 | $context['activeTag'] = true; |
||
86 | $context['activeTags'] = $searchTag; |
||
87 | } |
||
88 | |||
89 | return $context; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get categories array for view. |
||
94 | * |
||
95 | * @param Request $request |
||
96 | * @param string $className |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | View Code Duplication | public function getCategories(Request $request, $className) |
|
115 | |||
116 | /** |
||
117 | * @param string $slug |
||
118 | * @param string $tag |
||
119 | * @param string $locale |
||
120 | * @param array $parameters |
||
121 | * @param int $referenceType |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getArticleTagRouterPath($slug, $tag, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
131 | |||
132 | /** |
||
133 | * @param string $slug |
||
134 | * @param string $category |
||
135 | * @param string $locale |
||
136 | * @param array $parameters |
||
137 | * @param int $referenceType |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getName() |
||
155 | |||
156 | /** |
||
157 | * @param string $routeName |
||
158 | * @param string $type |
||
159 | * @param string $slug |
||
160 | * @param string $tagOrCategory |
||
161 | * @param string $locale |
||
162 | * @param array $parameters |
||
163 | * @param int $referenceType |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
184 | |||
185 | /** |
||
186 | * @param string $type |
||
187 | * @param string $locale |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | protected function articleRouteExists($type, $locale) |
||
201 | } |
||
202 |
If you suppress an error, we recommend checking for the error condition explicitly: