GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FUrlRule   F
last analyzed

Complexity

Total Complexity 60

Size/Duplication

Total Lines 202
Duplicated Lines 2.48 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 85.98%

Importance

Changes 0
Metric Value
dl 5
loc 202
rs 3.6
c 0
b 0
f 0
ccs 92
cts 107
cp 0.8598
wmc 60
lcom 2
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A parseUrl() 5 23 5
F createUrl() 0 75 30
C preparePathInfo() 0 26 13
B getRoute() 0 24 8

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like FUrlRule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FUrlRule, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author Sergey Glagolev <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2013 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.components.url
8
 *
9
 * <pre>
10
 * 'productType' => array('product/type', 'pattern' => 'type-<type:\w+>/<page:\d+>', 'defaultParams' => array('page' => 1), 'canonicalParams' => array('page'), 'shouldRemember' => false),
11
 * </pre>
12
 */
13
class FUrlRule extends CUrlRule
14
{
15
  /**
16
   * @var array Параметры, которые будут оставлены при построении канонической ссылки
17
   */
18
  public $canonicalParams = array();
19
20
  /**
21
   * @var bool Запоминаем или нет маршрут в сессию, чтобы вернуться на страницу после авторизации пользователя
22
   */
23
  public $shouldRemember = true;
24
25
  /**
26
   * @var bool Строим ли ссылку с параметрами по-умолчанию или без них
27
   */
28
  public $createWithDefault = false;
29
30 6
  public function __construct($route, $pattern)
31
  {
32 6
    if( is_array($route) )
33 6
      foreach(array('canonicalParams', 'shouldRemember', 'createWithDefault') as $name)
34 6
        if( isset($route[$name]) )
35 6
          $this->$name = $route[$name];
36
37 6
    parent::__construct($route, $pattern);
38 6
  }
39
40
  /**
41
   * @param FUrlManager $manager
42
   * @param CHttpRequest $request
43
   * @param string $pathInfo
44
   * @param string $rawPathInfo
45
   *
46
   * @return mixed
47
   */
48 2
  public function parseUrl($manager, $request, $pathInfo, $rawPathInfo)
49
  {
50 2
    $manager->defaultParams = array();
51
52 2
    if( ($pathInfo = $this->preparePathInfo($manager, $request, $pathInfo, $rawPathInfo)) === false )
53 2
    {
54
      return false;
55
    }
56
57 2 View Code Duplication
    if( !empty($this->defaultParams) && !preg_match($this->pattern, $pathInfo, $matches)  )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58 2
    {
59 2
      $pathInfo .= implode('/', $this->defaultParams).'/';
60 2
      $manager->defaultParams = $this->defaultParams;
61 2
    }
62
63 2
    if( preg_match($this->pattern, $pathInfo, $matches) )
64 2
    {
65 2
      $manager->rule = $this;
66 2
      return $this->getRoute($manager, $pathInfo, $matches);
67
    }
68
    else
69 1
      return false;
70
  }
71
72 15
  public function createUrl($manager, $route, $params, $ampersand)
73
  {
74 15
    if( $this->parsingOnly )
75 15
      return false;
76
77 15
    if( $manager->caseSensitive && $this->caseSensitive === null || $this->caseSensitive )
78 15
      $case = '';
79
    else
80
      $case = 'i';
81
82 15
    $tr = array();
83 15
    if( $route !== $this->route )
84 15
    {
85 14
      if( $this->routePattern !== null && preg_match($this->routePattern.$case, $route, $matches) )
86 14
      {
87
        foreach($this->references as $key => $name)
88
          $tr[$name] = $matches[$key];
89
      }
90
      else
91 14
        return false;
92
    }
93
94
    // Если в параметрах построения ссылки не заданы какие-то параметры по-умолчанию,
95
    // то добавляем пустое значение в массив параметров. Это позволяет пройти проверку,
96
    // но в ссылку они добавлены не будут
97 15
    foreach($this->defaultParams as $key => $value)
98 9
      if( !isset($params[$key]) )
99 9
        $params[$key] = $this->createWithDefault ? $value : '';
100
101 15
    foreach($this->params as $key => $value)
102 12
      if( !isset($params[$key]) )
103 12
        return false;
104
105 13
    if( $manager->matchValue && $this->matchValue === null || $this->matchValue )
106 13
    {
107
      foreach($this->params as $key => $value)
108
      {
109
        if( !preg_match('/\A'.$value.'\z/u'.$case, $params[$key]) )
110
          return false;
111
      }
112
    }
113
114 13
    foreach($this->params as $key => $value)
115
    {
116 10
      $tr["<$key>"] = preg_match('/[a-z\/-]/', $params[$key]) ? $params[$key] : urlencode($params[$key]);
117 10
      unset($params[$key]);
118 13
    }
119
120 13
    $suffix = $this->urlSuffix === null ? $manager->urlSuffix : $this->urlSuffix;
121 13
    $url = strtr($this->template, $tr);
122
123 13
    if( empty($suffix) && !empty($url) )
124 13
      $url = trim($url, '/').'/';
125
126 13
    if( $this->hasHostInfo )
127 13
    {
128
      $hostInfo = Yii::app()->getRequest()->getHostInfo();
129
      if( stripos($url, $hostInfo) === 0 )
130
        $url = substr($url, strlen($hostInfo));
131
    }
132
133 13
    if( empty($params) )
134 13
      return $url !== '' ? $url.$suffix : $url;
135
136 8
    if( $this->append )
137 8
      $url .= '/'.$manager->createPathInfo($params, '/', '/').$suffix;
138
    else
139
    {
140 8
      if( $url !== '' )
141 8
        $url .= $suffix;
142 8
      $url .= '?'.$manager->createPathInfo($params, '=', $ampersand);
143
    }
144
145 8
    return $url;
146
  }
147
148
  /**
149
   * @param FUrlManager $manager
150
   * @param CHttpRequest $request
151
   * @param string $pathInfo
152
   * @param string $rawPathInfo
153
   *
154
   * @return mixed
155
   */
156 3
  protected function preparePathInfo($manager, $request, $pathInfo, $rawPathInfo)
157
  {
158 3
    if( $this->verb !== null && !in_array($request->getRequestType(), $this->verb, true) )
159 3
      return false;
160
161 3
    if( !($manager->caseSensitive && $this->caseSensitive === null || $this->caseSensitive) )
162 3
      $this->pattern .= 'i';
163
164 3
    if( $this->urlSuffix !== null )
165 3
      $pathInfo = $manager->removeUrlSuffix($rawPathInfo, $this->urlSuffix);
166
167
    // URL suffix required, but not found in the requested URL
168 3
    if( $manager->useStrictParsing && $pathInfo === $rawPathInfo )
169 3
    {
170 3
      $urlSuffix = $this->urlSuffix === null ? $manager->urlSuffix : $this->urlSuffix;
171 3
      if( $urlSuffix != '' && $urlSuffix !== '/' )
172 3
        return false;
173 3
    }
174
175 3
    if( $this->hasHostInfo )
176 3
      $pathInfo = strtolower($request->getHostInfo()).rtrim('/'.$pathInfo, '/');
177
178 3
    $pathInfo .= '/';
179
180 3
    return $pathInfo;
181
  }
182
183
  /**
184
   * @param FUrlManager $manager
185
   * @param string $pathInfo
186
   * @param array $matches
187
   *
188
   * @return string
189
   */
190 3
  protected function getRoute($manager, $pathInfo, $matches)
191
  {
192 3
    foreach($this->defaultParams as $name => $value)
193
    {
194 2
      if( !isset($_GET[$name]) )
195 2
        $_REQUEST[$name] = $_GET[$name] = $value;
196 3
    }
197
198 3
    $tr = array();
199 3
    foreach($matches as $key => $value)
200
    {
201 3
      if( isset($this->references[$key]) )
202 3
        $tr[$this->references[$key]] = $value;
203 3
      elseif( isset($this->params[$key]) )
204 3
        $_REQUEST[$key] = $_GET[$key] = $value;
205 3
    }
206
207 3
    if( $pathInfo !== $matches[0] ) // there're additional GET params
208 3
      $manager->parsePathInfo(ltrim(substr($pathInfo, strlen($matches[0])), '/'));
209 3
    if( $this->routePattern !== null )
210 3
      return strtr($this->route, $tr);
211
    else
212 3
      return $this->route;
213
  }
214
}