Skip to content

Commit

Permalink
Merge pull request #12 from asispts/feature/fully-qualified-global-const
Browse files Browse the repository at this point in the history
Allow fully qualified global constants
  • Loading branch information
asispts authored Nov 3, 2023
2 parents 9d72d4d + 9b67b43 commit 3938baf
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"require": {
"php": ">=7.2",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"slevomat/coding-standard": "^8.8",
"slevomat/coding-standard": "^8.14",
"squizlabs/php_codesniffer": "^3.7"
},
"require-dev": {
Expand Down
48 changes: 24 additions & 24 deletions ptscs/Sniffs/PSR12/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class FileHeaderSniff implements Sniff
*/
public function register()
{
return [T_OPEN_TAG];
return [\T_OPEN_TAG];
}

/**
Expand All @@ -41,8 +41,8 @@ public function process(File $phpcsFile, $stackPtr)

$possibleHeaders = [];

$searchFor = Tokens::$ooScopeTokens;
$searchFor[T_OPEN_TAG] = T_OPEN_TAG;
$searchFor = Tokens::$ooScopeTokens;
$searchFor[\T_OPEN_TAG] = \T_OPEN_TAG;

$openTag = $stackPtr;
do {
Expand Down Expand Up @@ -95,7 +95,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($openTag !== 0) {
// Allow for hashbang lines.
$hashbang = false;
if ($tokens[($openTag - 1)]['code'] === T_INLINE_HTML) {
if ($tokens[($openTag - 1)]['code'] === \T_INLINE_HTML) {
$content = \trim($tokens[($openTag - 1)]['content']);
if (\substr($content, 0, 2) === '#!') {
$hashbang = true;
Expand Down Expand Up @@ -127,7 +127,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, ($stackPtr + 1), null, true);
if ($next === false) {
return [];
}
Expand All @@ -142,19 +142,19 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
$foundDocblock = false;

$commentOpeners = Tokens::$scopeOpeners;
unset($commentOpeners[T_NAMESPACE]);
unset($commentOpeners[T_DECLARE]);
unset($commentOpeners[T_USE]);
unset($commentOpeners[T_IF]);
unset($commentOpeners[T_WHILE]);
unset($commentOpeners[T_FOR]);
unset($commentOpeners[T_FOREACH]);
unset($commentOpeners[T_DO]);
unset($commentOpeners[T_TRY]);
unset($commentOpeners[\T_NAMESPACE]);
unset($commentOpeners[\T_DECLARE]);
unset($commentOpeners[\T_USE]);
unset($commentOpeners[\T_IF]);
unset($commentOpeners[\T_WHILE]);
unset($commentOpeners[\T_FOR]);
unset($commentOpeners[\T_FOREACH]);
unset($commentOpeners[\T_DO]);
unset($commentOpeners[\T_TRY]);

do {
switch ($tokens[$next]['code']) {
case T_DOC_COMMENT_OPEN_TAG:
case \T_DOC_COMMENT_OPEN_TAG:
if ($foundDocblock === true) {
// Found a second docblock, so start of code.
break(2);
Expand All @@ -168,7 +168,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
}

if (
$tokens[$docToken]['code'] === T_ATTRIBUTE
$tokens[$docToken]['code'] === \T_ATTRIBUTE
&& isset($tokens[$docToken]['attribute_closer']) === true
) {
$docToken = $tokens[$docToken]['attribute_closer'];
Expand All @@ -190,7 +190,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
$annotation = false;
for ($i = $next; $i < $end; $i++) {
if (
$tokens[$i]['code'] === T_DOC_COMMENT_TAG
$tokens[$i]['code'] === \T_DOC_COMMENT_TAG
&& \strtolower($tokens[$i]['content']) === '@var'
) {
$annotation = true;
Expand All @@ -210,8 +210,8 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)

$next = $end;
break;
case T_DECLARE:
case T_NAMESPACE:
case \T_DECLARE:
case \T_NAMESPACE:
if (isset($tokens[$next]['scope_opener']) === true) {
// If this statement is using bracketed syntax, it doesn't
// apply to the entire files and so is not part of header.
Expand All @@ -229,10 +229,10 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)

$next = $end;
break;
case T_USE:
case \T_USE:
$type = 'use';
$useType = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
if ($useType !== false && $tokens[$useType]['code'] === T_STRING) {
if ($useType !== false && $tokens[$useType]['code'] === \T_STRING) {
$content = \strtolower($tokens[$useType]['content']);
if ($content === 'function' || $content === 'const') {
$type .= ' ' . $content;
Expand Down Expand Up @@ -266,7 +266,7 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
break(2);
}//end switch

$next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, ($next + 1), null, true);
} while ($next !== false);

return $headerLines;
Expand Down Expand Up @@ -295,7 +295,7 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
// We're at the end of the current header block.
// Make sure there is a single blank line after
// this block.
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, ($line['end'] + 1), null, true);
if ($next !== false && $tokens[$next]['line'] !== ($tokens[$line['end']]['line'] + 2)) {
$error = 'Header blocks must be separated by a single blank line';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingAfterBlock');
Expand Down Expand Up @@ -335,7 +335,7 @@ public function processHeaderLines(File $phpcsFile, $headerLines)
} elseif ($headerLines[($i + 1)]['type'] === $line['type']) {
// Still in the same block, so make sure there is no
// blank line after this statement.
$next = $phpcsFile->findNext(T_WHITESPACE, ($line['end'] + 1), null, true);
$next = $phpcsFile->findNext(\T_WHITESPACE, ($line['end'] + 1), null, true);
if ($tokens[$next]['line'] > ($tokens[$line['end']]['line'] + 1)) {
$error = 'Header blocks must not contain blank lines';
$fix = $phpcsFile->addFixableError($error, $line['end'], 'SpacingInsideBlock');
Expand Down
2 changes: 2 additions & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias" />

<!-- Fully qualified exceptions and global functions -->
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants" />
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions" />
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="searchAnnotations" value="true" />
<property name="allowFullyQualifiedGlobalFunctions" value="true" />
<property name="allowFullyQualifiedGlobalConstants" value="true" />
</properties>
</rule>

Expand Down
4 changes: 2 additions & 2 deletions tests/SniffTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function test_sniffs(array $errorData = [], array $warningData = []): voi
$filename = \str_replace('Test', '', \array_pop($names));
$paths = \array_merge([__DIR__], \array_slice($names, 2), ['_data'], [$filename . '.php.inc']);

$filepath = \implode(DIRECTORY_SEPARATOR, $paths);
$this->assertFileExists($filepath);
$filepath = \implode(\DIRECTORY_SEPARATOR, $paths);
$this->assertFileExists($filepath, "File $filepath is not exists");

$sniff = new SniffAssertion($filepath, $this->standard, $this->excludes);
$sniff->assertError($this, $errorData);
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/Slevomat/FullyQualifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static function provideTestData(): Iterator
{
yield[
[
new ErrorData(11, 'SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions'),
new ErrorData(13, 'SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions'),
new ErrorData(15, 'SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions'),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Sniffs/Slevomat/_data/DisallowGroupUse.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App;

use App\Entity\{User,Role};
use App\Entity\{User, Role as EntityRole};

final class Foobar
{
Expand All @@ -12,7 +12,7 @@ final class Foobar
private $user;

/**
* @var Role
* @var EntityRole
*/
private $role;
}
4 changes: 2 additions & 2 deletions tests/Sniffs/Slevomat/_data/DisallowGroupUse.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App;

use App\Entity\{User,Role};
use App\Entity\{User, Role as EntityRole};

final class Foobar
{
Expand All @@ -12,7 +12,7 @@ final class Foobar
private $user;

/**
* @var Role
* @var EntityRole
*/
private $role;
}
4 changes: 3 additions & 1 deletion tests/Sniffs/Slevomat/_data/FullyQualified.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class FooException extends Exception

public function __construct()
{
$this->values = \explode(DIRECTORY_SEPARATOR, __DIR__);
$this->values = \explode(\DIRECTORY_SEPARATOR, __DIR__);

echo \parse_url('localhost', \PHP_URL_QUERY);
}
}
4 changes: 4 additions & 0 deletions tests/Sniffs/Slevomat/_data/FullyQualified.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace App;

use const PHP_URL_QUERY;

final class FooException extends Exception
{
private $values;

public function __construct()
{
$this->values = explode(DIRECTORY_SEPARATOR, __DIR__);

echo parse_url('localhost', PHP_URL_QUERY);
}
}

0 comments on commit 3938baf

Please sign in to comment.