Skip to content

Commit

Permalink
Merge pull request #336 from cakephp/type-code
Browse files Browse the repository at this point in the history
Convert use of token type to code
  • Loading branch information
othercorey authored Jul 11, 2021
2 parents eebf2ff + 3ae15c2 commit 6b17905
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 28 deletions.
16 changes: 8 additions & 8 deletions CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function process(File $phpcsFile, $stackPtr)
return [];
}

$returnTokenType = $tokens[$startIndex]['type'];
if ($returnTokenType !== 'T_SELF') {
$returnTokenCode = $tokens[$startIndex]['code'];
if ($returnTokenCode !== T_SELF) {
// Then we can only warn, but not auto-fix
$phpcsFile->addError(
'Chaining methods (@return $this) should not have any return-type-hint.',
Expand Down Expand Up @@ -107,7 +107,7 @@ protected function isChainingMethod(File $phpCsFile, int $stackPointer): bool
$docBlockStartIndex = $tokens[$docBlockEndIndex]['comment_opener'];

for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) {
if ($tokens[$i]['type'] !== 'T_DOC_COMMENT_TAG') {
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
continue;
}
if ($tokens[$i]['content'] !== '@return') {
Expand All @@ -116,7 +116,7 @@ protected function isChainingMethod(File $phpCsFile, int $stackPointer): bool

$classNameIndex = $i + 2;

if ($tokens[$classNameIndex]['type'] !== 'T_DOC_COMMENT_STRING') {
if ($tokens[$classNameIndex]['code'] !== T_DOC_COMMENT_STRING) {
continue;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ protected function assertNotThisOrStatic(File $phpCsFile, int $stackPointer): vo
$docBlockStartIndex = $tokens[$docBlockEndIndex]['comment_opener'];

for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) {
if ($tokens[$i]['type'] !== 'T_DOC_COMMENT_TAG') {
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
continue;
}
if ($tokens[$i]['content'] !== '@return') {
Expand All @@ -158,7 +158,7 @@ protected function assertNotThisOrStatic(File $phpCsFile, int $stackPointer): vo

$classNameIndex = $i + 2;

if ($tokens[$classNameIndex]['type'] !== 'T_DOC_COMMENT_STRING') {
if ($tokens[$classNameIndex]['code'] !== T_DOC_COMMENT_STRING) {
continue;
}

Expand Down Expand Up @@ -197,14 +197,14 @@ protected function findRelatedDocBlock(File $phpCsFile, int $stackPointer): ?int

if (
!empty($tokens[$beginningOfLine - 2])
&& $tokens[$beginningOfLine - 2]['type'] === 'T_DOC_COMMENT_CLOSE_TAG'
&& $tokens[$beginningOfLine - 2]['code'] === T_DOC_COMMENT_CLOSE_TAG
) {
return $beginningOfLine - 2;
}

if (
!empty($tokens[$beginningOfLine - 3])
&& $tokens[$beginningOfLine - 3]['type'] === 'T_DOC_COMMENT_CLOSE_TAG'
&& $tokens[$beginningOfLine - 3]['code'] === T_DOC_COMMENT_CLOSE_TAG
) {
return $beginningOfLine - 3;
}
Expand Down
2 changes: 1 addition & 1 deletion CakePHP/Sniffs/Commenting/DocBlockAlignmentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->fixer->beginChangeset();
foreach ($tokensToIndent as $searchToken => $indent) {
$indentString = str_repeat(' ', $indent);
$isOpenTag = $tokens[$searchToken]['type'] === 'T_DOC_COMMENT_OPEN_TAG';
$isOpenTag = $tokens[$searchToken]['code'] === T_DOC_COMMENT_OPEN_TAG;
if ($isOpenTag && $commentIndentation === 0) {
$phpcsFile->fixer->addContentBefore($searchToken, $indentString);
} else {
Expand Down
6 changes: 3 additions & 3 deletions CakePHP/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public function process(File $phpcsFile, $stackPtr)
) {
$previous = $commentEnd;
if (
$tokens[$commentEnd]['type'] === 'T_ATTRIBUTE_END'
|| $tokens[$commentEnd]['type'] === 'T_ATTRIBUTE'
$tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
|| $tokens[$commentEnd]['code'] === T_ATTRIBUTE
) {
while ($tokens[$previous]['type'] !== 'T_ATTRIBUTE') {
while ($tokens[$previous]['code'] !== T_ATTRIBUTE) {
$previous--;
}
$previous--;
Expand Down
22 changes: 11 additions & 11 deletions CakePHP/Sniffs/Formatting/BlankLineBeforeReturnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,24 @@ public function process(File $phpcsFile, $stackPtr)
while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) {
if (
$tokens[$current]['line'] == $previousLine
&& $tokens[$current]['type'] !== 'T_WHITESPACE'
&& $tokens[$current]['type'] !== 'T_COMMENT'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_OPEN_TAG'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_TAG'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_STRING'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_CLOSE_TAG'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_WHITESPACE'
&& $tokens[$current]['code'] !== T_WHITESPACE
&& $tokens[$current]['code'] !== T_COMMENT
&& $tokens[$current]['code'] !== T_DOC_COMMENT_OPEN_TAG
&& $tokens[$current]['code'] !== T_DOC_COMMENT_TAG
&& $tokens[$current]['code'] !== T_DOC_COMMENT_STRING
&& $tokens[$current]['code'] !== T_DOC_COMMENT_CLOSE_TAG
&& $tokens[$current]['code'] !== T_DOC_COMMENT_WHITESPACE
) {
$prevLineTokens[] = $tokens[$current]['type'];
$prevLineTokens[] = $tokens[$current]['code'];
}
$current--;
}

if (
isset($prevLineTokens[0])
&& ($prevLineTokens[0] === 'T_OPEN_CURLY_BRACKET'
|| $prevLineTokens[0] === 'T_COLON'
|| $prevLineTokens[0] === 'T_OPEN_TAG')
&& ($prevLineTokens[0] === T_OPEN_CURLY_BRACKET
|| $prevLineTokens[0] === T_COLON
|| $prevLineTokens[0] === T_OPEN_TAG)
) {
return;
} elseif (count($prevLineTokens) > 0) {
Expand Down
10 changes: 5 additions & 5 deletions CakePHP/Sniffs/WhiteSpace/FunctionSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function process(File $phpCsFile, $stackPointer)
$nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $semicolonIndex + 1, null, true);

// Do not mess with the end of the class
if ($tokens[$nextContentIndex]['type'] === 'T_CLOSE_CURLY_BRACKET') {
if ($tokens[$nextContentIndex]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public function process(File $phpCsFile, $stackPointer)
$nextContentIndex = $phpCsFile->findNext(T_WHITESPACE, $closingBraceIndex + 1, null, true);

// Do not mess with the end of the class
if ($tokens[$nextContentIndex]['type'] === 'T_CLOSE_CURLY_BRACKET') {
if ($tokens[$nextContentIndex]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}

Expand Down Expand Up @@ -135,11 +135,11 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, $stackPointer)

$prevContentIndex = $phpCsFile->findPrevious(T_WHITESPACE, $firstTokenInLineIndex - 1, null, true);

if ($tokens[$prevContentIndex]['type'] === 'T_ATTRIBUTE_END') {
if ($tokens[$prevContentIndex]['code'] === T_ATTRIBUTE_END) {
return;
}

if ($tokens[$prevContentIndex]['type'] === 'T_DOC_COMMENT_CLOSE_TAG') {
if ($tokens[$prevContentIndex]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
$firstTokenInLineIndex = $tokens[$prevContentIndex]['comment_opener'];
while ($tokens[$firstTokenInLineIndex - 1]['line'] === $line) {
$firstTokenInLineIndex--;
Expand All @@ -149,7 +149,7 @@ protected function assertNewLineAtTheBeginning(File $phpCsFile, $stackPointer)
$prevContentIndex = $phpCsFile->findPrevious(T_WHITESPACE, $firstTokenInLineIndex - 1, null, true);

// Do not mess with the start of the class
if ($tokens[$prevContentIndex]['type'] === 'T_OPEN_CURLY_BRACKET') {
if ($tokens[$prevContentIndex]['code'] === T_OPEN_CURLY_BRACKET) {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Beakman;

class Foo
{
/**
* @return $this
*/
public function correct()
{
}

/**
* @return $this
*/
public function incorrect(): Foo
{
}

/**
* @return $this
*/
public function incorrectSelf(): self
{
}
}
28 changes: 28 additions & 0 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CakePHP\Tests\Classes;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ReturnTypeHintUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritDoc
*/
public function getErrorList()
{
return [
16 => 1,
23 => 1,
];
}

/**
* @inheritDoc
*/
public function getWarningList()
{
return [
];
}
}
29 changes: 29 additions & 0 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTestinc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Beakman;

use Other\Crap;
use Other\Error as OtherError;

class Foo
{
/**
* @return $this
*/
public function correct()
{
}

/**
* @return $this
*/
public function incorrect(): Foo
{
}

/**
* @return $this
*/
public function incorrectSelf()
{
}
}

0 comments on commit 6b17905

Please sign in to comment.