| Conditions | 2 |
| Paths | 1 |
| Total Lines | 407 |
| 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 |
||
| 195 | public function providerAuthorize() |
||
| 196 | { |
||
| 197 | $trueRuleMock = $this->getMockBuilder(Rule::class) |
||
| 198 | ->setMethods(['allowed']) |
||
| 199 | ->getMock(); |
||
| 200 | $trueRuleMock->expects($this->any()) |
||
| 201 | ->method('allowed') |
||
| 202 | ->willReturn(true); |
||
| 203 | |||
| 204 | return [ |
||
| 205 | 'happy-strict-all' => [ |
||
| 206 | //permissions |
||
| 207 | [[ |
||
| 208 | 'role' => 'test', |
||
| 209 | 'service' => 'tests', |
||
| 210 | 'action' => 'test', |
||
| 211 | 'allowed' => true, |
||
| 212 | ]], |
||
| 213 | //user |
||
| 214 | [ |
||
| 215 | 'id' => 1, |
||
| 216 | 'username' => 'luke', |
||
| 217 | 'role' => 'test', |
||
| 218 | ], |
||
| 219 | //request |
||
| 220 | [ |
||
| 221 | 'service' => 'tests', |
||
| 222 | 'action' => 'test' |
||
| 223 | ], |
||
| 224 | //expected |
||
| 225 | true |
||
| 226 | ], |
||
| 227 | 'happy-strict-all-deny' => [ |
||
| 228 | //permissions |
||
| 229 | [[ |
||
| 230 | 'role' => 'test', |
||
| 231 | 'service' => 'tests', |
||
| 232 | 'action' => 'test', |
||
| 233 | 'allowed' => false, |
||
| 234 | ]], |
||
| 235 | //user |
||
| 236 | [ |
||
| 237 | 'id' => 1, |
||
| 238 | 'username' => 'luke', |
||
| 239 | 'role' => 'test', |
||
| 240 | ], |
||
| 241 | //request |
||
| 242 | [ |
||
| 243 | 'service' => 'tests', |
||
| 244 | 'action' => 'test' |
||
| 245 | ], |
||
| 246 | //expected |
||
| 247 | false |
||
| 248 | ], |
||
| 249 | 'happy-pl-null-allowed-null' => [ |
||
| 250 | //permissions |
||
| 251 | [[ |
||
| 252 | 'role' => 'test', |
||
| 253 | 'service' => 'tests', |
||
| 254 | 'action' => 'test', |
||
| 255 | ]], |
||
| 256 | //user |
||
| 257 | [ |
||
| 258 | 'id' => 1, |
||
| 259 | 'username' => 'luke', |
||
| 260 | 'role' => 'test', |
||
| 261 | ], |
||
| 262 | //request |
||
| 263 | [ |
||
| 264 | 'service' => 'tests', |
||
| 265 | 'action' => 'test' |
||
| 266 | ], |
||
| 267 | //expected |
||
| 268 | true |
||
| 269 | ], |
||
| 270 | 'happy-asterisk' => [ |
||
| 271 | //permissions |
||
| 272 | [[ |
||
| 273 | 'role' => 'test', |
||
| 274 | 'service' => 'tests', |
||
| 275 | 'action' => 'test', |
||
| 276 | ]], |
||
| 277 | //user |
||
| 278 | [ |
||
| 279 | 'id' => 1, |
||
| 280 | 'username' => 'luke', |
||
| 281 | 'role' => 'test', |
||
| 282 | ], |
||
| 283 | //request |
||
| 284 | [ |
||
| 285 | 'service' => 'tests', |
||
| 286 | 'action' => 'test' |
||
| 287 | ], |
||
| 288 | //expected |
||
| 289 | true |
||
| 290 | ], |
||
| 291 | 'happy-asterisk-main-app' => [ |
||
| 292 | //permissions |
||
| 293 | [[ |
||
| 294 | 'role' => 'test', |
||
| 295 | 'service' => 'tests', |
||
| 296 | 'action' => 'test', |
||
| 297 | ]], |
||
| 298 | //user |
||
| 299 | [ |
||
| 300 | 'id' => 1, |
||
| 301 | 'username' => 'luke', |
||
| 302 | 'role' => 'test', |
||
| 303 | ], |
||
| 304 | //request |
||
| 305 | [ |
||
| 306 | 'service' => 'tests', |
||
| 307 | 'action' => 'test' |
||
| 308 | ], |
||
| 309 | //expected |
||
| 310 | true |
||
| 311 | ], |
||
| 312 | 'happy-role-asterisk' => [ |
||
| 313 | //permissions |
||
| 314 | [[ |
||
| 315 | 'role' => '*', |
||
| 316 | 'service' => 'tests', |
||
| 317 | 'action' => 'test', |
||
| 318 | ]], |
||
| 319 | //user |
||
| 320 | [ |
||
| 321 | 'id' => 1, |
||
| 322 | 'username' => 'luke', |
||
| 323 | 'role' => 'any-role', |
||
| 324 | ], |
||
| 325 | //request |
||
| 326 | [ |
||
| 327 | 'service' => 'tests', |
||
| 328 | 'action' => 'test' |
||
| 329 | ], |
||
| 330 | //expected |
||
| 331 | true |
||
| 332 | ], |
||
| 333 | 'happy-service-asterisk' => [ |
||
| 334 | //permissions |
||
| 335 | [[ |
||
| 336 | 'role' => 'test', |
||
| 337 | 'service' => '*', |
||
| 338 | 'action' => 'test', |
||
| 339 | ]], |
||
| 340 | //user |
||
| 341 | [ |
||
| 342 | 'id' => 1, |
||
| 343 | 'username' => 'luke', |
||
| 344 | 'role' => 'test', |
||
| 345 | ], |
||
| 346 | //request |
||
| 347 | [ |
||
| 348 | 'service' => 'tests', |
||
| 349 | 'action' => 'test' |
||
| 350 | ], |
||
| 351 | //expected |
||
| 352 | true |
||
| 353 | ], |
||
| 354 | 'happy-action-asterisk' => [ |
||
| 355 | //permissions |
||
| 356 | [[ |
||
| 357 | 'role' => 'test', |
||
| 358 | 'service' => 'tests', |
||
| 359 | 'action' => '*', |
||
| 360 | ]], |
||
| 361 | //user |
||
| 362 | [ |
||
| 363 | 'id' => 1, |
||
| 364 | 'username' => 'luke', |
||
| 365 | 'role' => 'test', |
||
| 366 | ], |
||
| 367 | //request |
||
| 368 | [ |
||
| 369 | 'service' => 'tests', |
||
| 370 | 'action' => 'any' |
||
| 371 | ], |
||
| 372 | //expected |
||
| 373 | true |
||
| 374 | ], |
||
| 375 | 'happy-some-asterisk-allowed' => [ |
||
| 376 | //permissions |
||
| 377 | [[ |
||
| 378 | 'role' => 'test', |
||
| 379 | 'service' => '*', |
||
| 380 | 'action' => '*', |
||
| 381 | ]], |
||
| 382 | //user |
||
| 383 | [ |
||
| 384 | 'id' => 1, |
||
| 385 | 'username' => 'luke', |
||
| 386 | 'role' => 'test', |
||
| 387 | ], |
||
| 388 | //request |
||
| 389 | [ |
||
| 390 | 'service' => 'tests', |
||
| 391 | 'action' => 'any' |
||
| 392 | ], |
||
| 393 | //expected |
||
| 394 | true |
||
| 395 | ], |
||
| 396 | 'happy-some-asterisk-deny' => [ |
||
| 397 | //permissions |
||
| 398 | [[ |
||
| 399 | 'role' => 'test', |
||
| 400 | 'service' => '*', |
||
| 401 | 'action' => '*', |
||
| 402 | 'allowed' => false, |
||
| 403 | ]], |
||
| 404 | //user |
||
| 405 | [ |
||
| 406 | 'id' => 1, |
||
| 407 | 'username' => 'luke', |
||
| 408 | 'role' => 'test', |
||
| 409 | ], |
||
| 410 | //request |
||
| 411 | [ |
||
| 412 | 'service' => 'tests', |
||
| 413 | 'action' => 'any' |
||
| 414 | ], |
||
| 415 | //expected |
||
| 416 | false |
||
| 417 | ], |
||
| 418 | 'all-deny' => [ |
||
| 419 | //permissions |
||
| 420 | [[ |
||
| 421 | 'role' => '*', |
||
| 422 | 'service' => '*', |
||
| 423 | 'action' => '*', |
||
| 424 | 'allowed' => false, |
||
| 425 | ]], |
||
| 426 | //user |
||
| 427 | [ |
||
| 428 | 'id' => 1, |
||
| 429 | 'username' => 'luke', |
||
| 430 | 'role' => 'test', |
||
| 431 | ], |
||
| 432 | //request |
||
| 433 | [ |
||
| 434 | 'service' => 'Any', |
||
| 435 | 'action' => 'any' |
||
| 436 | ], |
||
| 437 | //expected |
||
| 438 | false |
||
| 439 | ], |
||
| 440 | 'dasherized' => [ |
||
| 441 | //permissions |
||
| 442 | [[ |
||
| 443 | 'role' => 'test', |
||
| 444 | 'service' => 'TestTests', |
||
| 445 | 'action' => 'TestAction', |
||
| 446 | 'allowed' => true, |
||
| 447 | ]], |
||
| 448 | //user |
||
| 449 | [ |
||
| 450 | 'id' => 1, |
||
| 451 | 'username' => 'luke', |
||
| 452 | 'role' => 'test', |
||
| 453 | ], |
||
| 454 | //request |
||
| 455 | [ |
||
| 456 | 'service' => 'test-tests', |
||
| 457 | 'action' => 'test-action' |
||
| 458 | ], |
||
| 459 | //expected |
||
| 460 | true |
||
| 461 | ], |
||
| 462 | 'happy-array' => [ |
||
| 463 | //permissions |
||
| 464 | [[ |
||
| 465 | 'role' => ['test'], |
||
| 466 | 'service' => ['tests'], |
||
| 467 | 'action' => ['one', 'two'], |
||
| 468 | ]], |
||
| 469 | //user |
||
| 470 | [ |
||
| 471 | 'id' => 1, |
||
| 472 | 'username' => 'luke', |
||
| 473 | 'role' => 'test', |
||
| 474 | ], |
||
| 475 | //request |
||
| 476 | [ |
||
| 477 | 'service' => 'tests', |
||
| 478 | 'action' => 'one' |
||
| 479 | ], |
||
| 480 | //expected |
||
| 481 | true |
||
| 482 | ], |
||
| 483 | 'happy-array' => [ |
||
| 484 | //permissions |
||
| 485 | [[ |
||
| 486 | 'role' => ['test'], |
||
| 487 | 'service' => ['tests'], |
||
| 488 | 'action' => ['one', 'two'], |
||
| 489 | ]], |
||
| 490 | //user |
||
| 491 | [ |
||
| 492 | 'id' => 1, |
||
| 493 | 'username' => 'luke', |
||
| 494 | 'role' => 'test', |
||
| 495 | ], |
||
| 496 | //request |
||
| 497 | [ |
||
| 498 | 'service' => 'tests', |
||
| 499 | 'action' => 'three' |
||
| 500 | ], |
||
| 501 | //expected |
||
| 502 | false |
||
| 503 | ], |
||
| 504 | 'happy-callback-check-params' => [ |
||
| 505 | //permissions |
||
| 506 | [[ |
||
| 507 | 'role' => ['test'], |
||
| 508 | 'service' => ['tests'], |
||
| 509 | 'action' => ['one', 'two'], |
||
| 510 | 'allowed' => function ($user, $role, $request) { |
||
| 511 | return $user['id'] === 1 && $role == 'test'; |
||
| 512 | } |
||
| 513 | ]], |
||
| 514 | //user |
||
| 515 | [ |
||
| 516 | 'id' => 1, |
||
| 517 | 'username' => 'luke', |
||
| 518 | 'role' => 'test', |
||
| 519 | ], |
||
| 520 | //request |
||
| 521 | [ |
||
| 522 | 'service' => 'tests', |
||
| 523 | 'action' => 'one' |
||
| 524 | ], |
||
| 525 | //expected |
||
| 526 | true |
||
| 527 | ], |
||
| 528 | 'happy-callback-deny' => [ |
||
| 529 | //permissions |
||
| 530 | [[ |
||
| 531 | 'role' => ['test'], |
||
| 532 | 'service' => ['tests'], |
||
| 533 | 'action' => ['one', 'two'], |
||
| 534 | 'allowed' => function ($user, $role, $request) { |
||
| 535 | return false; |
||
| 536 | } |
||
| 537 | ]], |
||
| 538 | //user |
||
| 539 | [ |
||
| 540 | 'id' => 1, |
||
| 541 | 'username' => 'luke', |
||
| 542 | 'role' => 'test', |
||
| 543 | ], |
||
| 544 | //request |
||
| 545 | [ |
||
| 546 | 'service' => 'tests', |
||
| 547 | 'action' => 'one' |
||
| 548 | ], |
||
| 549 | //expected |
||
| 550 | false |
||
| 551 | ], |
||
| 552 | 'happy-prefix' => [ |
||
| 553 | //permissions |
||
| 554 | [[ |
||
| 555 | 'role' => ['test'], |
||
| 556 | 'prefix' => ['admin'], |
||
| 557 | 'service' => ['tests'], |
||
| 558 | 'action' => ['one', 'two'], |
||
| 559 | ]], |
||
| 560 | //user |
||
| 561 | [ |
||
| 562 | 'id' => 1, |
||
| 563 | 'username' => 'luke', |
||
| 564 | 'role' => 'test', |
||
| 565 | ], |
||
| 566 | //request |
||
| 567 | [ |
||
| 568 | 'prefix' => 'admin', |
||
| 569 | 'service' => 'tests', |
||
| 570 | 'action' => 'one' |
||
| 571 | ], |
||
| 572 | //expected |
||
| 573 | true |
||
| 574 | ], |
||
| 575 | |||
| 576 | 'rule-class' => [ |
||
| 577 | //permissions |
||
| 578 | [ |
||
| 579 | [ |
||
| 580 | 'role' => ['test'], |
||
| 581 | 'service' => '*', |
||
| 582 | 'action' => 'one', |
||
| 583 | 'allowed' => $trueRuleMock, |
||
| 584 | ], |
||
| 585 | ], |
||
| 586 | //user |
||
| 587 | [ |
||
| 588 | 'id' => 1, |
||
| 589 | 'username' => 'luke', |
||
| 590 | 'role' => 'test', |
||
| 591 | ], |
||
| 592 | //request |
||
| 593 | [ |
||
| 594 | 'service' => 'tests', |
||
| 595 | 'action' => 'one' |
||
| 596 | ], |
||
| 597 | //expected |
||
| 598 | true |
||
| 599 | ], |
||
| 600 | ]; |
||
| 601 | } |
||
| 602 | } |
||
| 603 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.