Completed
Push — master ( d8324b...c53dd7 )
by chihiro
23s
created

EccubeExtension   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 196
Duplicated Lines 11.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 22
loc 196
ccs 48
cts 58
cp 0.8276
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getCalcIncTax() 0 4 1
A getCsrfTokenForAnchor() 0 5 1
A getNoImageProduct() 0 4 2
A getDateFormatFilter() 0 8 2
A getPriceFilter() 0 7 1
A getEllipsis() 0 4 1
A getTimeAgo() 0 4 1
A getPath() 11 11 2
A getUrl() 11 11 2
A getFunctions() 0 15 1
A getFilters() 0 10 1
A getActiveMenus() 0 9 2
A __construct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

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
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Twig\Extension;
26
27
use Eccube\Common\Constant;
28
use Eccube\Util\Str;
29
use Silex\Application;
30
use Symfony\Component\Routing\Exception\RouteNotFoundException;
31
32
class EccubeExtension extends \Twig_Extension
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
33
{
34
    private $app;
35
36 468
    public function __construct(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
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()
84
    {
85 468
        return 'eccube';
86
    }
87
88
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$price" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rate" missing
Loading history...
introduced by
Doc comment for parameter "$tax_rule" missing
Loading history...
89
     * Name of this extension
90
     *
91
     * @return string
92
     */
93 19
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
94
    {
95 19
        return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule);
96
    }
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()
120
    {
121 30
        $token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue();
122 30
        return 'token-for-anchor=\'' . $token . '\'';
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123
    }
124
125
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
126
     * return No Image filename
127
     *
128
     * @return string
129
     */
130 40
    public function getNoImageProduct($image)
131
    {
132 40
        return empty($image) ? 'no_image_product.jpg' : $image;
133
    }
134
135
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$format" missing
Loading history...
introduced by
Doc comment for parameter "$date" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
136
     * Name of this extension
137
     *
138
     * @return string
139
     */
140 14
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
141
    {
142 14
        if (is_null($date)) {
143 10
            return $value;
144
        } else {
145 4
            return $date->format($format);
146
        }
147
    }
148
149
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$number" missing
Loading history...
introduced by
Doc comment for parameter "$decimals" missing
Loading history...
introduced by
Doc comment for parameter "$decPoint" missing
Loading history...
introduced by
Doc comment for parameter "$thousandsSep" missing
Loading history...
150
     * Name of this extension
151
     *
152
     * @return string
153
     */
154 121
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
155
    {
156 121
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
157 121
        $price = '¥ ' . $price;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
158
159 121
        return $price;
160
    }
161
162
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$length" missing
Loading history...
introduced by
Doc comment for parameter "$end" missing
Loading history...
introduced by
Doc comment for parameter "$value" missing
Loading history...
163
     * Name of this extension
164
     *
165
     * @return string
166
     */
167
    public function getEllipsis($value, $length = 100, $end = '...')
168
    {
169
        return Str::ellipsis($value, $length, $end);
170
    }
171
172
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
173
     * Name of this extension
174
     *
175
     * @return string
176
     */
177
    public function getTimeAgo($date)
178
    {
179
        return Str::timeAgo($date);
180
    }
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
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
189
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
190
     * @param boolean $relative
191
     * @return string URL
192
     */
193 243 View Code Duplication
    public function getPath($name, $parameters = array(), $relative = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
194
    {
195 243
        $RoutingExtension = $this->app['twig']->getExtension('routing');
196
        try {
197 243
            return $RoutingExtension->getPath($name, $parameters, $relative);
198
        } catch (RouteNotFoundException $e) {
199
            trigger_error($e->getMessage(), E_USER_WARNING);
200
        }
201
202
        return $RoutingExtension->getPath('homepage').'404?bind='.$name;
203
    }
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
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
212
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
213
     * @param boolean $schemeRelative
214
     * @return string URL
215
     */
216 239 View Code Duplication
    public function getUrl($name, $parameters = array(), $schemeRelative = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
217
    {
218 239
        $RoutingExtension = $this->app['twig']->getExtension('routing');
219
        try {
220 239
            return $RoutingExtension->getUrl($name, $parameters, $schemeRelative);
221
        } catch (RouteNotFoundException $e) {
222
            trigger_error($e->getMessage(), E_USER_WARNING);
223
        }
224
225
        return $RoutingExtension->getUrl('homepage').'404?bind='.$name;
226
    }
227
}
228