EccubeExtension   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 208
Duplicated Lines 10.58 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.14%

Importance

Changes 0
Metric Value
dl 22
loc 208
ccs 46
cts 56
cp 0.8214
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 5

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilters() 0 10 1
A getName() 0 4 1
A getCalcIncTax() 0 4 1
A getActiveMenus() 0 9 2
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 16 1
A isObject() 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 585
    public function __construct(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
37
    {
38 585
        $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 99
    public function getFunctions()
47
    {
48 99
        $RoutingExtension = $this->app['twig']->getExtension('routing');
49
50
        return array(
51 99
            new \Twig_SimpleFunction('is_object', array($this, 'isObject')),
52 99
            new \Twig_SimpleFunction('calc_inc_tax', array($this, 'getCalcIncTax')),
53 99
            new \Twig_SimpleFunction('active_menus', array($this, 'getActiveMenus')),
54 99
            new \Twig_SimpleFunction('csrf_token_for_anchor', array($this, 'getCsrfTokenForAnchor'), array('is_safe' => array('all'))),
55
56
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::url
57 99
            new \Twig_SimpleFunction('url', array($this, 'getUrl'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
58
            // Override: \Symfony\Bridge\Twig\Extension\RoutingExtension::path
59 99
            new \Twig_SimpleFunction('path', array($this, 'getPath'), array('is_safe_callback' => array($RoutingExtension, 'isUrlGenerationSafe'))),
60
        );
61
    }
62
63
    /**
64
     * Returns a list of filters.
65
     *
66
     * @return array
67
     */
68 99
    public function getFilters()
69
    {
70
        return array(
71 99
            new \Twig_SimpleFilter('no_image_product', array($this, 'getNoImageProduct')),
72 99
            new \Twig_SimpleFilter('date_format', array($this, 'getDateFormatFilter')),
73 99
            new \Twig_SimpleFilter('price', array($this, 'getPriceFilter')),
74 99
            new \Twig_SimpleFilter('ellipsis', array($this, 'getEllipsis')),
75 99
            new \Twig_SimpleFilter('time_ago', array($this, 'getTimeAgo')),
76
        );
77
    }
78
79
    /**
80
     * Name of this extension
81
     *
82
     * @return string
83
     */
84 585
    public function getName()
85
    {
86 585
        return 'eccube';
87
    }
88
89
    /**
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...
90
     * Name of this extension
91
     *
92
     * @return string
93
     */
94 33
    public function getCalcIncTax($price, $tax_rate, $tax_rule)
95
    {
96 33
        return $price + $this->app['eccube.service.tax_rule']->calcTax($price, $tax_rate, $tax_rule);
97
    }
98
99
    /**
100
     * Name of this extension
101
     *
102
     * @param array $menus
103
     * @return array
104
     */
105 155
    public function getActiveMenus($menus = array())
106
    {
107 155
        $count = count($menus);
108 155
        for ($i = $count; $i <= 2; $i++) {
109 98
            $menus[] = '';
110
        }
111
112 155
        return $menus;
113
    }
114
115
    /**
116
     * Name of this extension
117
     *
118
     * @return string
119
     */
120 82
    public function getCsrfTokenForAnchor()
121
    {
122 82
        $token = $this->app['form.csrf_provider']->getToken(Constant::TOKEN_NAME)->getValue();
123 82
        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...
124
    }
125
126
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$image" missing
Loading history...
127
     * return No Image filename
128
     *
129
     * @return string
130
     */
131 130
    public function getNoImageProduct($image)
132
    {
133 130
        return empty($image) ? 'no_image_product.jpg' : $image;
134
    }
135
136
    /**
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...
137
     * Name of this extension
138
     *
139
     * @return string
140
     */
141 24
    public function getDateFormatFilter($date, $value = '', $format = 'Y/m/d')
142
    {
143 24
        if (is_null($date)) {
144 20
            return $value;
145
        } else {
146 4
            return $date->format($format);
147
        }
148
    }
149
150
    /**
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...
151
     * Name of this extension
152
     *
153
     * @return string
154
     */
155 208
    public function getPriceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
156
    {
157 208
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
158 208
        $price = '¥ ' . $price;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
159
160 208
        return $price;
161
    }
162
163
    /**
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...
164
     * Name of this extension
165
     *
166
     * @return string
167
     */
168
    public function getEllipsis($value, $length = 100, $end = '...')
169
    {
170
        return Str::ellipsis($value, $length, $end);
171
    }
172
173
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$date" missing
Loading history...
174
     * Name of this extension
175
     *
176
     * @return string
177
     */
178
    public function getTimeAgo($date)
179
    {
180
        return Str::timeAgo($date);
181
    }
182
183
    /**
184
     * bind から URL へ変換します。
185
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getPath の処理を拡張し、
186
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
187
     * 文字列 "/404?bind={bind}" を返します。
188
     *
189
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
190
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
191
     * @param boolean $relative
192
     * @return string URL
193
     */
194 292 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...
195
    {
196 292
        $RoutingExtension = $this->app['twig']->getExtension('routing');
197
        try {
198 292
            return $RoutingExtension->getPath($name, $parameters, $relative);
199
        } catch (RouteNotFoundException $e) {
200
            trigger_error($e->getMessage(), E_USER_WARNING);
201
        }
202
203
        return $RoutingExtension->getPath('homepage').'404?bind='.$name;
204
    }
205
206
    /**
207
     * bind から URL へ変換します。
208
     * \Symfony\Bridge\Twig\Extension\RoutingExtension::getUrl の処理を拡張し、
209
     * RouteNotFoundException 発生時に E_USER_WARNING を発生させ、
210
     * 文字列 "/404?bind={bind}" を返します。
211
     *
212
     * @param string $name
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
213
     * @param array $parameters
0 ignored issues
show
introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
214
     * @param boolean $schemeRelative
215
     * @return string URL
216
     */
217 347 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...
218
    {
219 347
        $RoutingExtension = $this->app['twig']->getExtension('routing');
220
        try {
221 347
            return $RoutingExtension->getUrl($name, $parameters, $schemeRelative);
222
        } catch (RouteNotFoundException $e) {
223
            trigger_error($e->getMessage(), E_USER_WARNING);
224
        }
225
226
        return $RoutingExtension->getUrl('homepage').'404?bind='.$name;
227
    }
228
229
    /**
230
     * Check if the value is object
231
     *
232
     * @param object $value
233
     * @return bool
234
     */
235 2
    public function isObject($value)
236
    {
237 2
        return is_object($value);
238
    }
239
}
240