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 |
||
32 | class EccubeExtension extends \Twig_Extension |
||
|
|||
33 | { |
||
34 | private $app; |
||
35 | |||
36 | 5 | public function __construct(Application $app) |
|
37 | { |
||
38 | 5 | $this->app = $app; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns a list of functions to add to the existing list. |
||
43 | * |
||
44 | * @return array An array of functions |
||
45 | */ |
||
46 | 4 | public function getFunctions() |
|
47 | { |
||
48 | $RoutingExtension = $this->app['twig']->getExtension('routing'); |
||
49 | |||
50 | return array( |
||
51 | new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')), |
||
52 | new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')), |
||
53 | new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))), |
||
54 | |||
55 | // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url |
||
56 | new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
||
57 | // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path |
||
58 | new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
||
59 | 4 | ); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Returns a list of filters. |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | 4 | public function getFilters() |
|
68 | { |
||
69 | return array( |
||
70 | new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')), |
||
71 | new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')), |
||
72 | new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')), |
||
73 | new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')), |
||
74 | new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')), |
||
75 | 4 | ); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Name of this extension |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 5 | public function getName() |
|
84 | { |
||
85 | 5 | return 'eccube'; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Name of this extension |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getCalcIncTax($price, $tax_rate, $tax_rule) |
||
97 | |||
98 | /** |
||
99 | * Name of this extension |
||
100 | * |
||
101 | * @param array $menus |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getActiveMenus($menus = array()) |
||
105 | { |
||
106 | $count = count($menus); |
||
107 | for ($i = $count; $i <= 2; $i++) { |
||
108 | $menus[] = ''; |
||
109 | } |
||
110 | |||
111 | return $menus; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Name of this extension |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getCsrfTokenForAnchor() |
||
120 | { |
||
121 | $token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue(); |
||
122 | return 'token-for-anchor=\'' . $token . '\''; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * return No Image filename |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getNoImageProduct($image) |
||
131 | { |
||
132 | return empty($image) ? 'no_image_product.jpg' : $image; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Name of this extension |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
||
141 | { |
||
142 | if (is_null($date)) { |
||
143 | return $value; |
||
144 | } else { |
||
145 | return $date->format($format); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Name of this extension |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',') |
||
161 | |||
162 | /** |
||
163 | * Name of this extension |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getEllipsis($value, $length = 100, $end = '...') |
||
171 | |||
172 | /** |
||
173 | * Name of this extension |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getTimeAgo($date) |
||
181 | |||
182 | /** |
||
183 | * bind から URL へ変換します。 |
||
184 | * \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、 |
||
185 | * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
||
186 | * 文字列 "/404?bind={bind}" を返します。 |
||
187 | * |
||
188 | * @param string $name |
||
189 | * @param array $parameters |
||
190 | * @param boolean $relative |
||
191 | * @return string URL |
||
192 | */ |
||
193 | View Code Duplication | public function getPath($name, $parameters = array(), $relative = false) |
|
204 | |||
205 | /** |
||
206 | * bind から URL へ変換します。 |
||
207 | * \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、 |
||
208 | * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、 |
||
209 | * 文字列 "/404?bind={bind}" を返します。 |
||
210 | * |
||
211 | * @param string $name |
||
212 | * @param array $parameters |
||
213 | * @param boolean $schemeRelative |
||
214 | * @return string URL |
||
215 | */ |
||
216 | View Code Duplication | public function getUrl($name, $parameters = array(), $schemeRelative = false) |
|
227 | } |
||
228 |