-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SlevomatCodingStandard.Classes.DisallowStringExpressionPropertyFetch:…
… New sniff that disallows string expression property fetch $object->{'foo'}
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
SlevomatCodingStandard/Sniffs/Classes/DisallowStringExpressionPropertyFetchSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use SlevomatCodingStandard\Helpers\FixerHelper; | ||
use SlevomatCodingStandard\Helpers\TokenHelper; | ||
use function preg_match; | ||
use const T_CONSTANT_ENCAPSED_STRING; | ||
use const T_OBJECT_OPERATOR; | ||
use const T_OPEN_CURLY_BRACKET; | ||
use const T_OPEN_PARENTHESIS; | ||
|
||
class DisallowStringExpressionPropertyFetchSniff implements Sniff | ||
{ | ||
|
||
public const CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH = 'DisallowedStringExpressionPropertyFetch'; | ||
|
||
/** | ||
* @return array<int, (int|string)> | ||
*/ | ||
public function register(): array | ||
{ | ||
return [T_OBJECT_OPERATOR]; | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @param int $objectOperatorPointer | ||
*/ | ||
public function process(File $phpcsFile, $objectOperatorPointer): void | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$curlyBracketOpenerPointer = TokenHelper::findNextEffective($phpcsFile, $objectOperatorPointer + 1); | ||
|
||
if ($tokens[$curlyBracketOpenerPointer]['code'] !== T_OPEN_CURLY_BRACKET) { | ||
return; | ||
} | ||
|
||
$curlyBracketCloserPointer = $tokens[$curlyBracketOpenerPointer]['bracket_closer']; | ||
|
||
if (TokenHelper::findNextExcluding( | ||
$phpcsFile, | ||
T_CONSTANT_ENCAPSED_STRING, | ||
$curlyBracketOpenerPointer + 1, | ||
$curlyBracketCloserPointer | ||
) !== null) { | ||
return; | ||
} | ||
|
||
$pointerAfterCurlyBracketCloser = TokenHelper::findNextEffective($phpcsFile, $curlyBracketCloserPointer + 1); | ||
|
||
if ($tokens[$pointerAfterCurlyBracketCloser]['code'] === T_OPEN_PARENTHESIS) { | ||
return; | ||
} | ||
|
||
if (preg_match( | ||
'~^(["\'])([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\1$~', | ||
$tokens[$curlyBracketOpenerPointer + 1]['content'], | ||
$matches | ||
) !== 1) { | ||
return; | ||
} | ||
|
||
$fix = $phpcsFile->addFixableError( | ||
'String expression property fetch is disallowed, use identifier property fetch.', | ||
$curlyBracketOpenerPointer, | ||
self::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH | ||
); | ||
|
||
if (!$fix) { | ||
return; | ||
} | ||
|
||
$phpcsFile->fixer->beginChangeset(); | ||
|
||
$phpcsFile->fixer->replaceToken($curlyBracketOpenerPointer, $matches[2]); | ||
FixerHelper::removeBetweenIncluding($phpcsFile, $curlyBracketOpenerPointer + 1, $curlyBracketCloserPointer); | ||
|
||
$phpcsFile->fixer->endChangeset(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/Sniffs/Classes/DisallowStringExpressionPropertyFetchSniffTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace SlevomatCodingStandard\Sniffs\Classes; | ||
|
||
use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
||
class DisallowStringExpressionPropertyFetchSniffTest extends TestCase | ||
{ | ||
|
||
public function testNoErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/disallowStringExpressionPropertyFetchNoErrors.php'); | ||
self::assertNoSniffErrorInFile($report); | ||
} | ||
|
||
public function testErrors(): void | ||
{ | ||
$report = self::checkFile(__DIR__ . '/data/disallowStringExpressionPropertyFetchErrors.php'); | ||
|
||
self::assertSame(3, $report->getErrorCount()); | ||
|
||
self::assertSniffError($report, 3, DisallowStringExpressionPropertyFetchSniff::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH); | ||
self::assertSniffError($report, 5, DisallowStringExpressionPropertyFetchSniff::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH); | ||
self::assertSniffError($report, 7, DisallowStringExpressionPropertyFetchSniff::CODE_DISALLOWED_STRING_EXPRESSION_PROPERTY_FETCH); | ||
|
||
self::assertAllFixedInFile($report); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
tests/Sniffs/Classes/data/disallowStringExpressionPropertyFetchErrors.fixed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
$a->foo; | ||
|
||
$a->boo; | ||
|
||
$a->foo_boo; |
7 changes: 7 additions & 0 deletions
7
tests/Sniffs/Classes/data/disallowStringExpressionPropertyFetchErrors.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
$a->{'foo'}; | ||
|
||
$a->{"boo"}; | ||
|
||
$a->{"foo_boo"}; |
13 changes: 13 additions & 0 deletions
13
tests/Sniffs/Classes/data/disallowStringExpressionPropertyFetchNoErrors.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$a->foo; | ||
|
||
$b->boo(); | ||
|
||
$c->{'coo'}(); | ||
|
||
$d->{'doo' . $c}; | ||
|
||
if (isset($a->{'not-compatible'})) { | ||
|
||
} |