Conditions | 14 |
Paths | 76 |
Total Lines | 74 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function dispatch($uMethod, $uPathInfo) |
||
63 | { |
||
64 | if ($uMethod === "HEAD" && $this->translateHeadMethod) { |
||
65 | $uMethod = "GET"; |
||
66 | } |
||
67 | |||
68 | if (isset($this->routeDefinitions["static"][$uPathInfo])) { |
||
69 | $tRoute = $this->routeDefinitions["static"][$uPathInfo]; |
||
70 | |||
71 | if (isset($tRoute[$uMethod])) { |
||
72 | return [ |
||
73 | "status" => self::FOUND, |
||
74 | "callback" => $tRoute[$uMethod], |
||
75 | "parameters" => [] |
||
76 | ]; |
||
77 | } else { |
||
78 | return [ |
||
79 | "status" => self::METHOD_NOT_ALLOWED, |
||
80 | "methods" => array_keys($tRoute) |
||
81 | ]; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if ($this->routeDefinitions["variable"] === null) { |
||
86 | $this->compile(); |
||
87 | } |
||
88 | |||
89 | if (isset($this->routeDefinitions["variable"][$uMethod])) { |
||
90 | foreach ($this->routeDefinitions["variable"][$uMethod] as $tVariableRoute) { |
||
91 | if (preg_match($tVariableRoute["regex"], $uPathInfo, $tMatches) !== 1) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | list($tCallback, $tVariableNames) = $tVariableRoute["routeMap"][count($tMatches)]; |
||
96 | |||
97 | $tVariables = []; |
||
98 | $tCount = 0; |
||
99 | foreach ($tVariableNames as $tVariableName) { |
||
100 | $tVariables[$tVariableName] = $tMatches[++$tCount]; |
||
101 | } |
||
102 | |||
103 | return [ |
||
104 | "status" => self::FOUND, |
||
105 | "callback" => $tCallback, |
||
106 | "parameters" => $tVariables |
||
107 | ]; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | // Find allowed methods for this URI by matching against all other |
||
112 | // HTTP methods as well |
||
113 | $tAllowedMethods = []; |
||
114 | foreach ($this->routeDefinitions["variable"] as $tCurrentMethod => $tVariableRouteSets) { |
||
115 | foreach ($tVariableRouteSets as $tVariableRoute) { |
||
116 | if (preg_match($tVariableRoute["regex"], $uPathInfo, $tMatches) !== 1) { |
||
117 | continue; |
||
118 | } |
||
119 | |||
120 | $tAllowedMethods[] = $tCurrentMethod; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | if (count($tAllowedMethods) > 0) { |
||
125 | return [ |
||
126 | "status" => self::METHOD_NOT_ALLOWED, |
||
127 | "methods" => $tAllowedMethods |
||
128 | ]; |
||
129 | } |
||
130 | |||
131 | // If there are no allowed methods the route simply does not exist |
||
132 | return [ |
||
133 | "status" => self::NOT_FOUND |
||
134 | ]; |
||
135 | } |
||
136 | } |
||
137 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.