| Conditions | 96 |
| Paths | > 20000 |
| Total Lines | 547 |
| Code Lines | 314 |
| Lines | 184 |
| Ratio | 33.64 % |
| 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 |
||
| 314 | public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $arrayStart, $arrayEnd) |
||
| 315 | { |
||
| 316 | $tokens = $phpcsFile->getTokens(); |
||
| 317 | $keywordStart = $tokens[$stackPtr]['column']; |
||
| 318 | |||
| 319 | // Check the closing bracket is on a new line. |
||
| 320 | $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), $arrayStart, true); |
||
| 321 | if ($tokens[$lastContent]['line'] === $tokens[$arrayEnd]['line']) { |
||
| 322 | $error = 'Closing parenthesis of array declaration must be on a new line'; |
||
| 323 | $fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNewLine'); |
||
| 324 | if ($fix === true) { |
||
| 325 | $phpcsFile->fixer->addNewlineBefore($arrayEnd); |
||
| 326 | } |
||
| 327 | View Code Duplication | } else if ($tokens[$arrayEnd]['column'] !== $keywordStart) { |
|
| 328 | // Check the closing bracket is lined up under the "a" in array. |
||
| 329 | $expected = ($keywordStart - 1); |
||
| 330 | $found = ($tokens[$arrayEnd]['column'] - 1); |
||
| 331 | $error = 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s'; |
||
| 332 | $data = array( |
||
| 333 | $expected, |
||
| 334 | $found, |
||
| 335 | ); |
||
| 336 | |||
| 337 | $fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotAligned', $data); |
||
| 338 | if ($fix === true) { |
||
| 339 | if ($found === 0) { |
||
| 340 | $phpcsFile->fixer->addContent(($arrayEnd - 1), str_repeat(' ', $expected)); |
||
| 341 | } else { |
||
| 342 | $phpcsFile->fixer->replaceToken(($arrayEnd - 1), str_repeat(' ', $expected)); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | }//end if |
||
| 346 | |||
| 347 | $keyUsed = false; |
||
| 348 | $singleUsed = false; |
||
| 349 | $indices = array(); |
||
| 350 | $maxLength = 0; |
||
| 351 | |||
| 352 | View Code Duplication | if ($tokens[$stackPtr]['code'] === T_ARRAY) { |
|
| 353 | $lastToken = $tokens[$stackPtr]['parenthesis_opener']; |
||
| 354 | } else { |
||
| 355 | $lastToken = $stackPtr; |
||
| 356 | } |
||
| 357 | |||
| 358 | // Find all the double arrows that reside in this scope. |
||
| 359 | for ($nextToken = ($stackPtr + 1); $nextToken < $arrayEnd; $nextToken++) { |
||
| 360 | // Skip bracketed statements, like function calls. |
||
| 361 | View Code Duplication | if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS |
|
| 362 | && (isset($tokens[$nextToken]['parenthesis_owner']) === false |
||
| 363 | || $tokens[$nextToken]['parenthesis_owner'] !== $stackPtr) |
||
| 364 | ) { |
||
| 365 | $nextToken = $tokens[$nextToken]['parenthesis_closer']; |
||
| 366 | continue; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ($tokens[$nextToken]['code'] === T_ARRAY |
||
| 370 | || $tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY |
||
| 371 | || $tokens[$nextToken]['code'] === T_CLOSURE |
||
| 372 | ) { |
||
| 373 | // Let subsequent calls of this test handle nested arrays. |
||
| 374 | if ($tokens[$lastToken]['code'] !== T_DOUBLE_ARROW) { |
||
| 375 | $indices[] = array('value' => $nextToken); |
||
| 376 | $lastToken = $nextToken; |
||
| 377 | } |
||
| 378 | |||
| 379 | View Code Duplication | if ($tokens[$nextToken]['code'] === T_ARRAY) { |
|
| 380 | $nextToken = $tokens[$tokens[$nextToken]['parenthesis_opener']]['parenthesis_closer']; |
||
| 381 | } else if ($tokens[$nextToken]['code'] === T_OPEN_SHORT_ARRAY) { |
||
| 382 | $nextToken = $tokens[$nextToken]['bracket_closer']; |
||
| 383 | } else { |
||
| 384 | // T_CLOSURE. |
||
| 385 | $nextToken = $tokens[$nextToken]['scope_closer']; |
||
| 386 | } |
||
| 387 | |||
| 388 | $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextToken + 1), null, true); |
||
| 389 | if ($tokens[$nextToken]['code'] !== T_COMMA) { |
||
| 390 | $nextToken--; |
||
| 391 | } else { |
||
| 392 | $lastToken = $nextToken; |
||
| 393 | } |
||
| 394 | |||
| 395 | continue; |
||
| 396 | }//end if |
||
| 397 | |||
| 398 | View Code Duplication | if ($tokens[$nextToken]['code'] !== T_DOUBLE_ARROW |
|
| 399 | && $tokens[$nextToken]['code'] !== T_COMMA |
||
| 400 | ) { |
||
| 401 | continue; |
||
| 402 | } |
||
| 403 | |||
| 404 | $currentEntry = array(); |
||
| 405 | |||
| 406 | if ($tokens[$nextToken]['code'] === T_COMMA) { |
||
| 407 | $stackPtrCount = 0; |
||
| 408 | View Code Duplication | if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
|
| 409 | $stackPtrCount = count($tokens[$stackPtr]['nested_parenthesis']); |
||
| 410 | } |
||
| 411 | |||
| 412 | $commaCount = 0; |
||
| 413 | if (isset($tokens[$nextToken]['nested_parenthesis']) === true) { |
||
| 414 | $commaCount = count($tokens[$nextToken]['nested_parenthesis']); |
||
| 415 | if ($tokens[$stackPtr]['code'] === T_ARRAY) { |
||
| 416 | // Remove parenthesis that are used to define the array. |
||
| 417 | $commaCount--; |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | if ($commaCount > $stackPtrCount) { |
||
| 422 | // This comma is inside more parenthesis than the ARRAY keyword, |
||
| 423 | // then there it is actually a comma used to separate arguments |
||
| 424 | // in a function call. |
||
| 425 | continue; |
||
| 426 | } |
||
| 427 | |||
| 428 | if ($keyUsed === true && $tokens[$lastToken]['code'] === T_COMMA) { |
||
| 429 | $error = 'No key specified for array entry; first entry specifies key'; |
||
| 430 | $phpcsFile->addError($error, $nextToken, 'NoKeySpecified'); |
||
| 431 | return; |
||
| 432 | } |
||
| 433 | |||
| 434 | View Code Duplication | if ($keyUsed === false) { |
|
| 435 | if ($tokens[($nextToken - 1)]['code'] === T_WHITESPACE) { |
||
| 436 | $content = $tokens[($nextToken - 2)]['content']; |
||
| 437 | if ($tokens[($nextToken - 1)]['content'] === $phpcsFile->eolChar) { |
||
| 438 | $spaceLength = 'newline'; |
||
| 439 | } else { |
||
| 440 | $spaceLength = $tokens[($nextToken - 1)]['length']; |
||
| 441 | } |
||
| 442 | |||
| 443 | $error = 'Expected 0 spaces between "%s" and comma; %s found'; |
||
| 444 | $data = array( |
||
| 445 | $content, |
||
| 446 | $spaceLength, |
||
| 447 | ); |
||
| 448 | |||
| 449 | $fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data); |
||
| 450 | if ($fix === true) { |
||
| 451 | $phpcsFile->fixer->replaceToken(($nextToken - 1), ''); |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | $valueContent = $phpcsFile->findNext( |
||
| 456 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 457 | ($lastToken + 1), |
||
| 458 | $nextToken, |
||
| 459 | true |
||
| 460 | ); |
||
| 461 | |||
| 462 | $indices[] = array('value' => $valueContent); |
||
| 463 | $singleUsed = true; |
||
| 464 | }//end if |
||
| 465 | |||
| 466 | $lastToken = $nextToken; |
||
| 467 | continue; |
||
| 468 | }//end if |
||
| 469 | |||
| 470 | if ($tokens[$nextToken]['code'] === T_DOUBLE_ARROW) { |
||
| 471 | if ($singleUsed === true) { |
||
| 472 | $error = 'Key specified for array entry; first entry has no key'; |
||
| 473 | $phpcsFile->addError($error, $nextToken, 'KeySpecified'); |
||
| 474 | return; |
||
| 475 | } |
||
| 476 | |||
| 477 | $currentEntry['arrow'] = $nextToken; |
||
| 478 | $keyUsed = true; |
||
| 479 | |||
| 480 | // Find the start of index that uses this double arrow. |
||
| 481 | $indexEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($nextToken - 1), $arrayStart, true); |
||
| 482 | $indexStart = $phpcsFile->findStartOfStatement($indexEnd); |
||
| 483 | |||
| 484 | View Code Duplication | if ($indexStart === $indexEnd) { |
|
| 485 | $currentEntry['index'] = $indexEnd; |
||
| 486 | $currentEntry['index_content'] = $tokens[$indexEnd]['content']; |
||
| 487 | } else { |
||
| 488 | $currentEntry['index'] = $indexStart; |
||
| 489 | $currentEntry['index_content'] = $phpcsFile->getTokensAsString($indexStart, ($indexEnd - $indexStart + 1)); |
||
| 490 | } |
||
| 491 | |||
| 492 | $indexLength = strlen($currentEntry['index_content']); |
||
| 493 | if ($maxLength < $indexLength) { |
||
| 494 | $maxLength = $indexLength; |
||
| 495 | } |
||
| 496 | |||
| 497 | // Find the value of this index. |
||
| 498 | $nextContent = $phpcsFile->findNext( |
||
| 499 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 500 | ($nextToken + 1), |
||
| 501 | $arrayEnd, |
||
| 502 | true |
||
| 503 | ); |
||
| 504 | |||
| 505 | $currentEntry['value'] = $nextContent; |
||
| 506 | $indices[] = $currentEntry; |
||
| 507 | $lastToken = $nextToken; |
||
| 508 | }//end if |
||
| 509 | }//end for |
||
| 510 | |||
| 511 | // Check for mutli-line arrays that should be single-line. |
||
| 512 | $singleValue = false; |
||
| 513 | |||
| 514 | View Code Duplication | if (empty($indices) === true) { |
|
| 515 | $singleValue = true; |
||
| 516 | } else if (count($indices) === 1 && $tokens[$lastToken]['code'] === T_COMMA) { |
||
| 517 | // There may be another array value without a comma. |
||
| 518 | $exclude = PHP_CodeSniffer_Tokens::$emptyTokens; |
||
| 519 | $exclude[] = T_COMMA; |
||
| 520 | $nextContent = $phpcsFile->findNext($exclude, ($indices[0]['value'] + 1), $arrayEnd, true); |
||
| 521 | if ($nextContent === false) { |
||
| 522 | $singleValue = true; |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | if ($singleValue === true) { |
||
| 527 | // Array cannot be empty, so this is a multi-line array with |
||
| 528 | // a single value. It should be defined on single line. |
||
| 529 | $error = 'Multi-line array contains a single value; use single-line array instead'; |
||
| 530 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiLineNotAllowed'); |
||
| 531 | |||
| 532 | if ($fix === true) { |
||
| 533 | $phpcsFile->fixer->beginChangeset(); |
||
| 534 | for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) { |
||
| 535 | if ($tokens[$i]['code'] !== T_WHITESPACE) { |
||
| 536 | break; |
||
| 537 | } |
||
| 538 | |||
| 539 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 540 | } |
||
| 541 | |||
| 542 | for ($i = ($arrayEnd - 1); $i > $arrayStart; $i--) { |
||
| 543 | if ($tokens[$i]['code'] !== T_WHITESPACE) { |
||
| 544 | break; |
||
| 545 | } |
||
| 546 | |||
| 547 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 548 | } |
||
| 549 | |||
| 550 | $phpcsFile->fixer->endChangeset(); |
||
| 551 | } |
||
| 552 | |||
| 553 | return; |
||
| 554 | }//end if |
||
| 555 | |||
| 556 | /* |
||
| 557 | This section checks for arrays that don't specify keys. |
||
| 558 | |||
| 559 | Arrays such as: |
||
| 560 | array( |
||
| 561 | 'aaa', |
||
| 562 | 'bbb', |
||
| 563 | 'd', |
||
| 564 | ); |
||
| 565 | */ |
||
| 566 | |||
| 567 | if ($keyUsed === false && empty($indices) === false) { |
||
| 568 | $count = count($indices); |
||
| 569 | $lastIndex = $indices[($count - 1)]['value']; |
||
| 570 | |||
| 571 | $trailingContent = $phpcsFile->findPrevious( |
||
| 572 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 573 | ($arrayEnd - 1), |
||
| 574 | $lastIndex, |
||
| 575 | true |
||
| 576 | ); |
||
| 577 | |||
| 578 | if ($tokens[$trailingContent]['code'] !== T_COMMA) { |
||
| 579 | $phpcsFile->recordMetric($stackPtr, 'Array end comma', 'no'); |
||
| 580 | $error = 'Comma required after last value in array declaration'; |
||
| 581 | $fix = $phpcsFile->addFixableError($error, $trailingContent, 'NoCommaAfterLast'); |
||
| 582 | if ($fix === true) { |
||
| 583 | $phpcsFile->fixer->addContent($trailingContent, ','); |
||
| 584 | } |
||
| 585 | } else { |
||
| 586 | $phpcsFile->recordMetric($stackPtr, 'Array end comma', 'yes'); |
||
| 587 | } |
||
| 588 | |||
| 589 | $lastValueLine = false; |
||
| 590 | foreach ($indices as $value) { |
||
| 591 | if (empty($value['value']) === true) { |
||
| 592 | // Array was malformed and we couldn't figure out |
||
| 593 | // the array value correctly, so we have to ignore it. |
||
| 594 | // Other parts of this sniff will correct the error. |
||
| 595 | continue; |
||
| 596 | } |
||
| 597 | |||
| 598 | if ($lastValueLine !== false && $tokens[$value['value']]['line'] === $lastValueLine) { |
||
| 599 | $error = 'Each value in a multi-line array must be on a new line'; |
||
| 600 | $fix = $phpcsFile->addFixableError($error, $value['value'], 'ValueNoNewline'); |
||
| 601 | if ($fix === true) { |
||
| 602 | if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { |
||
| 603 | $phpcsFile->fixer->replaceToken(($value['value'] - 1), ''); |
||
| 604 | } |
||
| 605 | |||
| 606 | $phpcsFile->fixer->addNewlineBefore($value['value']); |
||
| 607 | } |
||
| 608 | } else if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { |
||
| 609 | $expected = $keywordStart; |
||
| 610 | |||
| 611 | $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $value['value'], true); |
||
| 612 | $found = ($tokens[$first]['column'] - 1); |
||
| 613 | if ($found !== $expected) { |
||
| 614 | $error = 'Array value not aligned correctly; expected %s spaces but found %s'; |
||
| 615 | $data = array( |
||
| 616 | $expected, |
||
| 617 | $found, |
||
| 618 | ); |
||
| 619 | |||
| 620 | $fix = $phpcsFile->addFixableError($error, $value['value'], 'ValueNotAligned', $data); |
||
| 621 | View Code Duplication | if ($fix === true) { |
|
| 622 | if ($found === 0) { |
||
| 623 | $phpcsFile->fixer->addContent(($value['value'] - 1), str_repeat(' ', $expected)); |
||
| 624 | } else { |
||
| 625 | $phpcsFile->fixer->replaceToken(($value['value'] - 1), str_repeat(' ', $expected)); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | } |
||
| 629 | }//end if |
||
| 630 | |||
| 631 | $lastValueLine = $tokens[$value['value']]['line']; |
||
| 632 | }//end foreach |
||
| 633 | }//end if |
||
| 634 | |||
| 635 | /* |
||
| 636 | Below the actual indentation of the array is checked. |
||
| 637 | Errors will be thrown when a key is not aligned, when |
||
| 638 | a double arrow is not aligned, and when a value is not |
||
| 639 | aligned correctly. |
||
| 640 | If an error is found in one of the above areas, then errors |
||
| 641 | are not reported for the rest of the line to avoid reporting |
||
| 642 | spaces and columns incorrectly. Often fixing the first |
||
| 643 | problem will fix the other 2 anyway. |
||
| 644 | |||
| 645 | For example: |
||
| 646 | |||
| 647 | $a = array( |
||
| 648 | 'index' => '2', |
||
| 649 | ); |
||
| 650 | |||
| 651 | or |
||
| 652 | |||
| 653 | $a = [ |
||
| 654 | 'index' => '2', |
||
| 655 | ]; |
||
| 656 | |||
| 657 | In this array, the double arrow is indented too far, but this |
||
| 658 | will also cause an error in the value's alignment. If the arrow were |
||
| 659 | to be moved back one space however, then both errors would be fixed. |
||
| 660 | */ |
||
| 661 | |||
| 662 | $numValues = count($indices); |
||
| 663 | |||
| 664 | $indicesStart = ($keywordStart + 1); |
||
| 665 | $arrowStart = ($indicesStart + $maxLength + 1); |
||
| 666 | $valueStart = ($arrowStart + 3); |
||
| 667 | $indexLine = $tokens[$stackPtr]['line']; |
||
| 668 | $lastIndexLine = null; |
||
| 669 | foreach ($indices as $index) { |
||
| 670 | View Code Duplication | if (isset($index['index']) === false) { |
|
| 671 | // Array value only. |
||
| 672 | if ($tokens[$index['value']]['line'] === $tokens[$stackPtr]['line'] && $numValues > 1) { |
||
| 673 | $error = 'The first value in a multi-value array must be on a new line'; |
||
| 674 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'FirstValueNoNewline'); |
||
| 675 | if ($fix === true) { |
||
| 676 | $phpcsFile->fixer->addNewlineBefore($index['value']); |
||
| 677 | } |
||
| 678 | } |
||
| 679 | |||
| 680 | continue; |
||
| 681 | } |
||
| 682 | |||
| 683 | $lastIndexLine = $indexLine; |
||
| 684 | $indexLine = $tokens[$index['index']]['line']; |
||
| 685 | |||
| 686 | if ($indexLine === $tokens[$stackPtr]['line']) { |
||
| 687 | $error = 'The first index in a multi-value array must be on a new line'; |
||
| 688 | $fix = $phpcsFile->addFixableError($error, $index['index'], 'FirstIndexNoNewline'); |
||
| 689 | if ($fix === true) { |
||
| 690 | $phpcsFile->fixer->addNewlineBefore($index['index']); |
||
| 691 | } |
||
| 692 | |||
| 693 | continue; |
||
| 694 | } |
||
| 695 | |||
| 696 | View Code Duplication | if ($indexLine === $lastIndexLine) { |
|
| 697 | $error = 'Each index in a multi-line array must be on a new line'; |
||
| 698 | $fix = $phpcsFile->addFixableError($error, $index['index'], 'IndexNoNewline'); |
||
| 699 | if ($fix === true) { |
||
| 700 | if ($tokens[($index['index'] - 1)]['code'] === T_WHITESPACE) { |
||
| 701 | $phpcsFile->fixer->replaceToken(($index['index'] - 1), ''); |
||
| 702 | } |
||
| 703 | |||
| 704 | $phpcsFile->fixer->addNewlineBefore($index['index']); |
||
| 705 | } |
||
| 706 | |||
| 707 | continue; |
||
| 708 | } |
||
| 709 | |||
| 710 | if ($tokens[$index['index']]['column'] !== $indicesStart) { |
||
| 711 | $expected = ($indicesStart - 1); |
||
| 712 | $found = ($tokens[$index['index']]['column'] - 1); |
||
| 713 | $error = 'Array key not aligned correctly; expected %s spaces but found %s'; |
||
| 714 | $data = array( |
||
| 715 | $expected, |
||
| 716 | $found, |
||
| 717 | ); |
||
| 718 | |||
| 719 | $fix = $phpcsFile->addFixableError($error, $index['index'], 'KeyNotAligned', $data); |
||
| 720 | View Code Duplication | if ($fix === true) { |
|
| 721 | if ($found === 0) { |
||
| 722 | $phpcsFile->fixer->addContent(($index['index'] - 1), str_repeat(' ', $expected)); |
||
| 723 | } else { |
||
| 724 | $phpcsFile->fixer->replaceToken(($index['index'] - 1), str_repeat(' ', $expected)); |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | continue; |
||
| 729 | } |
||
| 730 | |||
| 731 | if ($tokens[$index['arrow']]['column'] !== $arrowStart) { |
||
| 732 | $expected = ($arrowStart - (strlen($index['index_content']) + $tokens[$index['index']]['column'])); |
||
| 733 | $found = ($tokens[$index['arrow']]['column'] - (strlen($index['index_content']) + $tokens[$index['index']]['column'])); |
||
| 734 | $error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s'; |
||
| 735 | $data = array( |
||
| 736 | $expected, |
||
| 737 | $found, |
||
| 738 | ); |
||
| 739 | |||
| 740 | $fix = $phpcsFile->addFixableError($error, $index['arrow'], 'DoubleArrowNotAligned', $data); |
||
| 741 | View Code Duplication | if ($fix === true) { |
|
| 742 | if ($found === 0) { |
||
| 743 | $phpcsFile->fixer->addContent(($index['arrow'] - 1), str_repeat(' ', $expected)); |
||
| 744 | } else { |
||
| 745 | $phpcsFile->fixer->replaceToken(($index['arrow'] - 1), str_repeat(' ', $expected)); |
||
| 746 | } |
||
| 747 | } |
||
| 748 | |||
| 749 | continue; |
||
| 750 | } |
||
| 751 | |||
| 752 | if ($tokens[$index['value']]['column'] !== $valueStart) { |
||
| 753 | $expected = ($valueStart - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column'])); |
||
| 754 | $found = ($tokens[$index['value']]['column'] - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column'])); |
||
| 755 | if ($found < 0) { |
||
| 756 | $found = 'newline'; |
||
| 757 | } |
||
| 758 | |||
| 759 | $error = 'Array value not aligned correctly; expected %s space(s) but found %s'; |
||
| 760 | $data = array( |
||
| 761 | $expected, |
||
| 762 | $found, |
||
| 763 | ); |
||
| 764 | |||
| 765 | $fix = $phpcsFile->addFixableError($error, $index['arrow'], 'ValueNotAligned', $data); |
||
| 766 | if ($fix === true) { |
||
| 767 | if ($found === 'newline') { |
||
| 768 | $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($index['value'] - 1), null, true); |
||
| 769 | $phpcsFile->fixer->beginChangeset(); |
||
| 770 | View Code Duplication | for ($i = ($prev + 1); $i < $index['value']; $i++) { |
|
| 771 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 772 | } |
||
| 773 | |||
| 774 | $phpcsFile->fixer->replaceToken(($index['value'] - 1), str_repeat(' ', $expected)); |
||
| 775 | $phpcsFile->fixer->endChangeset(); |
||
| 776 | } else if ($found === 0) { |
||
| 777 | $phpcsFile->fixer->addContent(($index['value'] - 1), str_repeat(' ', $expected)); |
||
| 778 | } else { |
||
| 779 | $phpcsFile->fixer->replaceToken(($index['value'] - 1), str_repeat(' ', $expected)); |
||
| 780 | } |
||
| 781 | } |
||
| 782 | }//end if |
||
| 783 | |||
| 784 | // Check each line ends in a comma. |
||
| 785 | $valueLine = $tokens[$index['value']]['line']; |
||
| 786 | $nextComma = false; |
||
| 787 | for ($i = $index['value']; $i < $arrayEnd; $i++) { |
||
| 788 | // Skip bracketed statements, like function calls. |
||
| 789 | View Code Duplication | if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { |
|
| 790 | $i = $tokens[$i]['parenthesis_closer']; |
||
| 791 | $valueLine = $tokens[$i]['line']; |
||
| 792 | continue; |
||
| 793 | } |
||
| 794 | |||
| 795 | if ($tokens[$i]['code'] === T_ARRAY) { |
||
| 796 | $i = $tokens[$tokens[$i]['parenthesis_opener']]['parenthesis_closer']; |
||
| 797 | $valueLine = $tokens[$i]['line']; |
||
| 798 | continue; |
||
| 799 | } |
||
| 800 | |||
| 801 | // Skip to the end of multi-line strings. |
||
| 802 | if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true) { |
||
| 803 | $i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true); |
||
| 804 | $i--; |
||
| 805 | $valueLine = $tokens[$i]['line']; |
||
| 806 | continue; |
||
| 807 | } |
||
| 808 | |||
| 809 | View Code Duplication | if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) { |
|
| 810 | $i = $tokens[$i]['bracket_closer']; |
||
| 811 | $valueLine = $tokens[$i]['line']; |
||
| 812 | continue; |
||
| 813 | } |
||
| 814 | |||
| 815 | View Code Duplication | if ($tokens[$i]['code'] === T_CLOSURE) { |
|
| 816 | $i = $tokens[$i]['scope_closer']; |
||
| 817 | $valueLine = $tokens[$i]['line']; |
||
| 818 | continue; |
||
| 819 | } |
||
| 820 | |||
| 821 | if ($tokens[$i]['code'] === T_COMMA) { |
||
| 822 | $nextComma = $i; |
||
| 823 | break; |
||
| 824 | } |
||
| 825 | }//end for |
||
| 826 | |||
| 827 | if ($nextComma === false || ($tokens[$nextComma]['line'] !== $valueLine)) { |
||
| 828 | $error = 'Each line in an array declaration must end in a comma'; |
||
| 829 | $fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma'); |
||
| 830 | |||
| 831 | View Code Duplication | if ($fix === true) { |
|
| 832 | // Find the end of the line and put a comma there. |
||
| 833 | for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { |
||
| 834 | if ($tokens[$i]['line'] > $valueLine) { |
||
| 835 | break; |
||
| 836 | } |
||
| 837 | } |
||
| 838 | |||
| 839 | $phpcsFile->fixer->addContentBefore(($i - 1), ','); |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | // Check that there is no space before the comma. |
||
| 844 | View Code Duplication | if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { |
|
| 845 | $content = $tokens[($nextComma - 2)]['content']; |
||
| 846 | $spaceLength = $tokens[($nextComma - 1)]['length']; |
||
| 847 | $error = 'Expected 0 spaces between "%s" and comma; %s found'; |
||
| 848 | $data = array( |
||
| 849 | $content, |
||
| 850 | $spaceLength, |
||
| 851 | ); |
||
| 852 | |||
| 853 | $fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data); |
||
| 854 | if ($fix === true) { |
||
| 855 | $phpcsFile->fixer->replaceToken(($nextComma - 1), ''); |
||
| 856 | } |
||
| 857 | } |
||
| 858 | }//end foreach |
||
| 859 | |||
| 860 | }//end processMultiLineArray() |
||
| 861 | |||
| 864 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.