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.

Template   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 136
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 34
c 4
b 0
f 0
lcom 1
cbo 1
dl 18
loc 136
rs 9.2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getTemplate() 0 3 1
A getExpression() 0 9 2
B pattern() 0 20 7
A addQueryParam() 9 9 2
A addGlobalQueryParam() 9 9 2
C match() 0 35 8
C enforceParamMatching() 0 24 9
A regex() 0 3 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
namespace Zaphpa;
4
5
/**
6
 * Generic URI matcher and parser implementation.
7
 */
8
class Template {
9
10
    private static $globalQueryParams = array();
11
    private $patterns = array();
12
13
    private $template  = null;
14
    private $params    = array();
15
    private $callbacks = array();
16
17
    public function __construct($path) {
18
        if ($path{0} != '/') {
19
            $path = "/$path";
20
        }
21
        $this->template = rtrim($path, '\/');
22
    }
23
24
    public function getTemplate() {
25
        return $this->template;
26
    }
27
28
    public function getExpression() {
29
        $expression = $this->template;
30
        if (preg_match_all('~(?P<match>\{(?P<name>.+?)\})~', $expression, $matches)) {
31
            $expressions = array_map(array($this, 'pattern'), $matches['name']);
32
            $expression  = str_replace($matches['match'], $expressions, $expression);
33
        }
34
35
        return sprintf('~^%s$~', $expression);
36
    }
37
38
    public function pattern($token, $pattern = null) {
39
        if ($pattern) {
40
            if (!isset($this->patterns[$token])) {
41
                $this->patterns[$token] = $pattern;
42
            }
43
        } else {
44
45
            if (isset($this->patterns[$token])) {
46
                $pattern = $this->patterns[$token];
47
            } else {
48
                $pattern = Constants::PATTERN_ANY;
49
            }
50
51
            if ((is_string($pattern) && is_callable($pattern)) || is_array($pattern)) {
52
                $this->callbacks[$token] = $pattern;
53
                $this->patterns[$token] = $pattern = Constants::PATTERN_ANY;
54
            }
55
            return sprintf($pattern, $token);
56
        }
57
    }
58
59 View Code Duplication
    public function addQueryParam($name, $pattern = '', $defaultValue = null) {
60
        if (!$pattern) {
61
            $pattern = Constants::PATTERN_ANY;
62
        }
63
        $this->params[$name] = (object) array(
64
            'pattern' => sprintf($pattern, $name),
65
            'value'   => $defaultValue
66
        );
67
    }
68
69 View Code Duplication
    public static function addGlobalQueryParam($name, $pattern = '', $defaultValue = null) {
70
        if (!$pattern) {
71
            $pattern = Constants::PATTERN_ANY;
72
        }
73
        self::$globalQueryParams[$name] = (object) array(
74
            'pattern' => sprintf($pattern, $name),
75
            'value'   => $defaultValue
76
        );
77
    }
78
79
    public function match($uri) {
80
81
        $uri = rtrim($uri, '\/');
82
        $match_found = preg_match($this->getExpression(), $uri, $matches);
83
        if (! $match_found) return;
84
85
        foreach($matches as $k => $v) {
86
            if (is_numeric($k)) {
87
                unset($matches[$k]);
88
            } else {
89
                if (isset($this->callbacks[$k])) {
90
                    $callback = Callback_Util::getCallback($this->callbacks[$k]);
91
                    $value    = call_user_func($callback, $v);
92
                    if ($value) {
93
                        $matches[$k] = $value;
94
                    } else {
95
                        throw new InvalidURIParameterException('Invalid parameters detected');
96
                    }
97
                }
98
99
                if (strpos($v, '/') !== false) {
100
                    $matches[$k] = explode('/', trim($v, '\/'));
101
                }
102
            }
103
        }
104
105
        $params = array_merge(self::$globalQueryParams, $this->params);
106
107
        if (!empty($params)) {
108
            $this->enforceParamMatching($params);
109
        }
110
111
        return $matches;
112
113
    }
114
115
    protected function enforceParamMatching($params) {
116
117
        foreach($params as $name => $param) {
118
119
            if (!isset($_GET[$name]) && $param->value) {
120
                $_GET[$name] = $param->value;
121
                $matched = true;
122
            } else if ($param->pattern && isset($_GET[$name])) {
123
                $result = preg_match(sprintf('~^%s$~', $param->pattern), $_GET[$name]);
124
                if (!$result && $param->value) {
125
                    $_GET[$name] = $param->value;
126
                    $result = true;
127
                }
128
                $matched = $result;
129
            } else {
130
                $matched = false;
131
            }
132
133
            if ($matched == false) {
134
                throw new Exception('Request does not match');
135
            }
136
137
        }
138
    }
139
140
    public static function regex($pattern) {
141
        return "(?P<%s>$pattern)";
142
    }
143
}
144