| Conditions | 1 |
| Paths | 1 |
| Total Lines | 191 |
| Code Lines | 143 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 107 | public function testAllowedActions() |
||
| 108 | { |
||
| 109 | $adminUser = $this->objFromFixture(Member::class, 'admin'); |
||
| 110 | |||
| 111 | $response = $this->get("UnsecuredController/"); |
||
| 112 | $this->assertEquals( |
||
| 113 | 200, |
||
| 114 | $response->getStatusCode(), |
||
| 115 | 'Access granted on index action without $allowed_actions on defining controller, ' . |
||
| 116 | 'when called without an action in the URL' |
||
| 117 | ); |
||
| 118 | |||
| 119 | $response = $this->get("UnsecuredController/index"); |
||
| 120 | $this->assertEquals( |
||
| 121 | 200, |
||
| 122 | $response->getStatusCode(), |
||
| 123 | 'Access denied on index action without $allowed_actions on defining controller, ' . |
||
| 124 | 'when called with an action in the URL' |
||
| 125 | ); |
||
| 126 | |||
| 127 | $response = $this->get("UnsecuredController/method1"); |
||
| 128 | $this->assertEquals( |
||
| 129 | 403, |
||
| 130 | $response->getStatusCode(), |
||
| 131 | 'Access denied on action without $allowed_actions on defining controller, ' . |
||
| 132 | 'when called without an action in the URL' |
||
| 133 | ); |
||
| 134 | |||
| 135 | $response = $this->get("AccessBaseController/"); |
||
| 136 | $this->assertEquals( |
||
| 137 | 200, |
||
| 138 | $response->getStatusCode(), |
||
| 139 | 'Access granted on index with empty $allowed_actions on defining controller, ' . |
||
| 140 | 'when called without an action in the URL' |
||
| 141 | ); |
||
| 142 | |||
| 143 | $response = $this->get("AccessBaseController/index"); |
||
| 144 | $this->assertEquals( |
||
| 145 | 200, |
||
| 146 | $response->getStatusCode(), |
||
| 147 | 'Access granted on index with empty $allowed_actions on defining controller, ' . |
||
| 148 | 'when called with an action in the URL' |
||
| 149 | ); |
||
| 150 | |||
| 151 | $response = $this->get("AccessBaseController/method1"); |
||
| 152 | $this->assertEquals( |
||
| 153 | 403, |
||
| 154 | $response->getStatusCode(), |
||
| 155 | 'Access denied on action with empty $allowed_actions on defining controller' |
||
| 156 | ); |
||
| 157 | |||
| 158 | $response = $this->get("AccessBaseController/method2"); |
||
| 159 | $this->assertEquals( |
||
| 160 | 403, |
||
| 161 | $response->getStatusCode(), |
||
| 162 | 'Access denied on action with empty $allowed_actions on defining controller, ' . |
||
| 163 | 'even when action is allowed in subclasses (allowed_actions don\'t inherit)' |
||
| 164 | ); |
||
| 165 | |||
| 166 | $response = $this->get("AccessSecuredController/"); |
||
| 167 | $this->assertEquals( |
||
| 168 | 200, |
||
| 169 | $response->getStatusCode(), |
||
| 170 | 'Access granted on index with non-empty $allowed_actions on defining controller, ' . |
||
| 171 | 'even when index isn\'t specifically mentioned in there' |
||
| 172 | ); |
||
| 173 | |||
| 174 | $response = $this->get("AccessSecuredController/method1"); |
||
| 175 | $this->assertEquals( |
||
| 176 | 403, |
||
| 177 | $response->getStatusCode(), |
||
| 178 | 'Access denied on action which is only defined in parent controller, ' . |
||
| 179 | 'even when action is allowed in currently called class (allowed_actions don\'t inherit)' |
||
| 180 | ); |
||
| 181 | |||
| 182 | $response = $this->get("AccessSecuredController/method2"); |
||
| 183 | $this->assertEquals( |
||
| 184 | 200, |
||
| 185 | $response->getStatusCode(), |
||
| 186 | 'Access granted on action originally defined with empty $allowed_actions on parent controller, ' . |
||
| 187 | 'because it has been redefined in the subclass' |
||
| 188 | ); |
||
| 189 | |||
| 190 | $response = $this->get("AccessSecuredController/templateaction"); |
||
| 191 | $this->assertEquals( |
||
| 192 | 403, |
||
| 193 | $response->getStatusCode(), |
||
| 194 | 'Access denied on action with $allowed_actions on defining controller, ' . |
||
| 195 | 'if action is not a method but rather a template discovered by naming convention' |
||
| 196 | ); |
||
| 197 | |||
| 198 | $response = $this->get("AccessSecuredController/templateaction"); |
||
| 199 | $this->assertEquals( |
||
| 200 | 403, |
||
| 201 | $response->getStatusCode(), |
||
| 202 | 'Access denied on action with $allowed_actions on defining controller, ' . |
||
| 203 | 'if action is not a method but rather a template discovered by naming convention' |
||
| 204 | ); |
||
| 205 | |||
| 206 | $this->session()->inst_set('loggedInAs', $adminUser->ID); |
||
| 207 | $response = $this->get("AccessSecuredController/templateaction"); |
||
| 208 | $this->assertEquals( |
||
| 209 | 200, |
||
| 210 | $response->getStatusCode(), |
||
| 211 | 'Access granted for logged in admin on action with $allowed_actions on defining controller, ' . |
||
| 212 | 'if action is not a method but rather a template discovered by naming convention' |
||
| 213 | ); |
||
| 214 | $this->session()->inst_set('loggedInAs', null); |
||
| 215 | |||
| 216 | $response = $this->get("AccessSecuredController/adminonly"); |
||
| 217 | $this->assertEquals( |
||
| 218 | 403, |
||
| 219 | $response->getStatusCode(), |
||
| 220 | 'Access denied on action with $allowed_actions on defining controller, ' . |
||
| 221 | 'when restricted by unmatched permission code' |
||
| 222 | ); |
||
| 223 | |||
| 224 | $response = $this->get("AccessSecuredController/aDmiNOnlY"); |
||
| 225 | $this->assertEquals( |
||
| 226 | 403, |
||
| 227 | $response->getStatusCode(), |
||
| 228 | 'Access denied on action with $allowed_actions on defining controller, ' . |
||
| 229 | 'regardless of capitalization' |
||
| 230 | ); |
||
| 231 | |||
| 232 | $response = $this->get('AccessSecuredController/protectedmethod'); |
||
| 233 | $this->assertEquals( |
||
| 234 | 404, |
||
| 235 | $response->getStatusCode(), |
||
| 236 | "Access denied to protected method even if its listed in allowed_actions" |
||
| 237 | ); |
||
| 238 | |||
| 239 | $this->session()->inst_set('loggedInAs', $adminUser->ID); |
||
| 240 | $response = $this->get("AccessSecuredController/adminonly"); |
||
| 241 | $this->assertEquals( |
||
| 242 | 200, |
||
| 243 | $response->getStatusCode(), |
||
| 244 | "Permission codes are respected when set in \$allowed_actions" |
||
| 245 | ); |
||
| 246 | $this->session()->inst_set('loggedInAs', null); |
||
| 247 | |||
| 248 | $response = $this->get('AccessBaseController/extensionmethod1'); |
||
| 249 | $this->assertEquals( |
||
| 250 | 200, |
||
| 251 | $response->getStatusCode(), |
||
| 252 | "Access granted to method defined in allowed_actions on extension, " . |
||
| 253 | "where method is also defined on extension" |
||
| 254 | ); |
||
| 255 | |||
| 256 | $response = $this->get('AccessSecuredController/extensionmethod1'); |
||
| 257 | $this->assertEquals( |
||
| 258 | 200, |
||
| 259 | $response->getStatusCode(), |
||
| 260 | "Access granted to method defined in allowed_actions on extension, " . |
||
| 261 | "where method is also defined on extension, even when called in a subclass" |
||
| 262 | ); |
||
| 263 | |||
| 264 | $response = $this->get('AccessBaseController/extensionmethod2'); |
||
| 265 | $this->assertEquals( |
||
| 266 | 404, |
||
| 267 | $response->getStatusCode(), |
||
| 268 | "Access denied to method not defined in allowed_actions on extension, " . |
||
| 269 | "where method is also defined on extension" |
||
| 270 | ); |
||
| 271 | |||
| 272 | $response = $this->get('IndexSecuredController/'); |
||
| 273 | $this->assertEquals( |
||
| 274 | 403, |
||
| 275 | $response->getStatusCode(), |
||
| 276 | "Access denied when index action is limited through allowed_actions, " . |
||
| 277 | "and doesn't satisfy checks, and action is empty" |
||
| 278 | ); |
||
| 279 | |||
| 280 | $response = $this->get('IndexSecuredController/index'); |
||
| 281 | $this->assertEquals( |
||
| 282 | 403, |
||
| 283 | $response->getStatusCode(), |
||
| 284 | "Access denied when index action is limited through allowed_actions, " . |
||
| 285 | "and doesn't satisfy checks" |
||
| 286 | ); |
||
| 287 | |||
| 288 | $this->session()->inst_set('loggedInAs', $adminUser->ID); |
||
| 289 | $response = $this->get('IndexSecuredController/'); |
||
| 290 | $this->assertEquals( |
||
| 291 | 200, |
||
| 292 | $response->getStatusCode(), |
||
| 293 | "Access granted when index action is limited through allowed_actions, " . |
||
| 294 | "and does satisfy checks" |
||
| 295 | ); |
||
| 296 | $this->session()->inst_set('loggedInAs', null); |
||
| 297 | } |
||
| 298 | |||
| 547 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.