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::addGlobalQueryParam()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
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