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 | 468 | public function __construct(Application $app) |
|
| 37 | 1 | { |
|
| 38 | 468 | $this->app = $app; |
|
| 39 | 468 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Returns a list of functions to add to the existing list. |
||
| 43 | * |
||
| 44 | * @return array An array of functions |
||
| 45 | */ |
||
| 46 | 97 | public function getFunctions() |
|
| 47 | { |
||
| 48 | 97 | $RoutingExtension = $this->app['twig']->getExtension('routing'); |
|
| 49 | |||
| 50 | return array( |
||
| 51 | 97 | new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')), |
|
| 52 | 97 | new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')), |
|
| 53 | 97 | 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 | 97 | new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
|
| 57 | // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path |
||
| 58 | 97 | new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))), |
|
| 59 | 97 | ); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns a list of filters. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | 97 | public function getFilters() |
|
| 68 | { |
||
| 69 | return array( |
||
| 70 | 97 | new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')), |
|
| 71 | 97 | new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')), |
|
| 72 | 97 | new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')), |
|
| 73 | 97 | new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')), |
|
| 74 | 97 | new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')), |
|
| 75 | 97 | ); |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Name of this extension |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 468 | public function getName() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Name of this extension |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 19 | 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 | 135 | public function getActiveMenus($menus = array()) |
|
| 105 | { |
||
| 106 | 135 | $count = count($menus); |
|
| 107 | 135 | for ($i = $count; $i <= 2; $i++) { |
|
| 108 | 78 | $menus[] = ''; |
|
| 109 | 78 | } |
|
| 110 | |||
| 111 | 135 | return $menus; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Name of this extension |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 30 | public function getCsrfTokenForAnchor() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * return No Image filename |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 40 | public function getNoImageProduct($image) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Name of this extension |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 14 | public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d') |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Name of this extension |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 121 | 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 | 243 | 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 | 239 | View Code Duplication | public function getUrl($name, $parameters = array(), $schemeRelative = false) |
| 227 | } |
||
| 228 |