Skip to content

Add ImportInternalFunctionSniff and OperatorPlacementSniff #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SymfonyCustom/Helpers/FixerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PHP_CodeSniffer\Files\File;

use function str_repeat;

/**
* Class FixerHelper
*/
Expand Down
65 changes: 65 additions & 0 deletions SymfonyCustom/Helpers/SniffHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

use function mb_strtolower;
use function preg_match;
use function trim;

/**
* Class SniffHelper
*/
Expand Down Expand Up @@ -187,6 +191,67 @@ public static function isGlobalUse(File $phpcsFile, int $stackPtr): bool
return true;
}

/**
* @param File $phpcsFile
* @param int $scopePtr
*
* @return array
*/
public static function getUseStatements(File $phpcsFile, int $scopePtr): array
{
$tokens = $phpcsFile->getTokens();

$uses = [];

if (isset($tokens[$scopePtr]['scope_opener'])) {
$start = $tokens[$scopePtr]['scope_opener'];
$end = $tokens[$scopePtr]['scope_closer'];
} else {
$start = $scopePtr;
$end = null;
}

$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
while (false !== $use && T_USE === $tokens[$use]['code']) {
if (
!self::isGlobalUse($phpcsFile, $use)
|| (null !== $end
&& (!isset($tokens[$use]['conditions'][$scopePtr])
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))
) {
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $use + 1, $end, true);
continue;
}

// find semicolon as the end of the global use scope
$endOfScope = $phpcsFile->findNext(T_SEMICOLON, $use + 1);

$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $use + 1, $endOfScope);

$type = 'class';
if (T_STRING === $tokens[$startOfName]['code']) {
$lowerContent = mb_strtolower($tokens[$startOfName]['content']);
if ('function' === $lowerContent || 'const' === $lowerContent) {
$type = $lowerContent;

$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $startOfName + 1, $endOfScope);
}
}

$uses[] = [
'ptrUse' => $use,
'name' => trim($phpcsFile->getTokensAsString($startOfName, $endOfScope - $startOfName)),
'ptrEnd' => $endOfScope,
'string' => trim($phpcsFile->getTokensAsString($use, $endOfScope - $use + 1)),
'type' => $type,
];

$use = $phpcsFile->findNext(Tokens::$emptyTokens, $endOfScope + 1, $end, true);
}

return $uses;
}

/**
* @param string $content
*
Expand Down
4 changes: 4 additions & 0 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Helpers\FixerHelper;

use function count;
use function in_array;
use function mb_strlen;

/**
* A test to ensure that arrays conform to the array coding standard.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function in_array;

/**
* Throws errors if forbidden tags are met.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use SymfonyCustom\Helpers\FixerHelper;
use SymfonyCustom\Helpers\SniffHelper;

use function array_merge;
use function in_array;

/**
* Throws errors if comments are not grouped by type with one blank line between them.
*/
Expand Down
4 changes: 4 additions & 0 deletions SymfonyCustom/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Helpers\FixerHelper;

use function ltrim;
use function rtrim;
use function str_repeat;

/**
* Ensures doc blocks follow basic formatting.
*/
Expand Down
4 changes: 4 additions & 0 deletions SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Helpers\SniffHelper;

use function count;
use function preg_match;
use function preg_replace;

/**
* SymfonyCustom standard customization to PEARs FunctionCommentSniff.
*/
Expand Down
3 changes: 3 additions & 0 deletions SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use SymfonyCustom\Helpers\SniffHelper;

use function preg_match;
use function preg_replace;

/**
* Parses and verifies the variable doc comment.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

use function in_array;

/**
* Throws errors if there's no blank line before return statements.
*/
Expand Down
3 changes: 3 additions & 0 deletions SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

use function array_merge;
use function in_array;

/**
* Enforces Yoda conditional statements.
*/
Expand Down
3 changes: 3 additions & 0 deletions SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function array_keys;
use function in_array;

/**
* Throws warnings if properties are declared after methods
*/
Expand Down
68 changes: 5 additions & 63 deletions SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Helpers\SniffHelper;

use function explode;
use function str_replace;
use function strcasecmp;

/**
* Class AlphabeticallySortedUseSniff
*/
Expand Down Expand Up @@ -40,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr): int
}
}

$uses = $this->getUseStatements($phpcsFile, $stackPtr);
$uses = SniffHelper::getUseStatements($phpcsFile, $stackPtr);

$lastUse = null;
foreach ($uses as $use) {
Expand Down Expand Up @@ -68,67 +71,6 @@ public function process(File $phpcsFile, $stackPtr): int
return T_OPEN_TAG === $tokens[$stackPtr]['code'] ? $phpcsFile->numTokens + 1 : $stackPtr + 1;
}

/**
* @param File $phpcsFile
* @param int $scopePtr
*
* @return array
*/
private function getUseStatements(File $phpcsFile, int $scopePtr): array
{
$tokens = $phpcsFile->getTokens();

$uses = [];

if (isset($tokens[$scopePtr]['scope_opener'])) {
$start = $tokens[$scopePtr]['scope_opener'];
$end = $tokens[$scopePtr]['scope_closer'];
} else {
$start = $scopePtr;
$end = null;
}

$use = $phpcsFile->findNext(T_USE, $start + 1, $end);
while (false !== $use && T_USE === $tokens[$use]['code']) {
if (
!SniffHelper::isGlobalUse($phpcsFile, $use)
|| (null !== $end
&& (!isset($tokens[$use]['conditions'][$scopePtr])
|| $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1))
) {
$use = $phpcsFile->findNext(Tokens::$emptyTokens, $use + 1, $end, true);
continue;
}

// find semicolon as the end of the global use scope
$endOfScope = $phpcsFile->findNext(T_SEMICOLON, $use + 1);

$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $use + 1, $endOfScope);

$type = 'class';
if (T_STRING === $tokens[$startOfName]['code']) {
$lowerContent = mb_strtolower($tokens[$startOfName]['content']);
if ('function' === $lowerContent || 'const' === $lowerContent) {
$type = $lowerContent;

$startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $startOfName + 1, $endOfScope);
}
}

$uses[] = [
'ptrUse' => $use,
'name' => trim($phpcsFile->getTokensAsString($startOfName, $endOfScope - $startOfName)),
'ptrEnd' => $endOfScope,
'string' => trim($phpcsFile->getTokensAsString($use, $endOfScope - $use + 1)),
'type' => $type,
];

$use = $phpcsFile->findNext(Tokens::$emptyTokens, $endOfScope + 1, $end, true);
}

return $uses;
}

/**
* @param array $a
* @param array $b
Expand Down
11 changes: 10 additions & 1 deletion SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
use PHP_CodeSniffer\Util\Tokens;
use SymfonyCustom\Helpers\SniffHelper;

use function in_array;
use function mb_strrpos;
use function mb_strtolower;
use function mb_substr;
use function preg_match;
use function preg_quote;
use function strcasecmp;
use function trim;

/**
* Checks for "use" statements that are not needed in a file.
*/
Expand Down Expand Up @@ -300,7 +309,7 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
if (strcasecmp($namespace, $useNamespace) === 0) {
$classUsed = false;
}
} elseif (false === $namespacePtr && '' === $useNamespace) {
} elseif ('' === $useNamespace) {
$classUsed = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function mb_strlen;
use function mb_substr;

/**
* Throws errors if symfony's naming conventions are not met.
*/
Expand Down
4 changes: 4 additions & 0 deletions SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

use function basename;
use function ctype_alnum;
use function mb_strlen;

/**
* Checks whether filename contains any other character than alphanumeric and underscores.
*/
Expand Down
16 changes: 15 additions & 1 deletion SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use SymfonyCustom\Helpers\SniffHelper;

use function array_pop;
use function array_unique;
use function count;
use function implode;
use function in_array;
use function mb_strlen;
use function mb_strpos;
use function mb_strtolower;
use function mb_substr;
use function preg_match;
use function preg_replace;
use function preg_split;
use function usort;

/**
* Throws errors if PHPDocs type hint are not valid.
*/
Expand Down Expand Up @@ -233,7 +247,7 @@ private function getValidType(string $typeName): string
return self::TYPES[$lowerType] ? $lowerType : $typeName;
}

// This can't be case insensitive since this is not reserved keyword
// This can't be case-insensitive since this is not reserved keyword
if (isset(self::ALIAS_TYPES[$typeName])) {
return self::ALIAS_TYPES[$typeName];
}
Expand Down
Loading