Skip to content

📦 Refacto #95

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 5 commits into from
Dec 26, 2019
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
15 changes: 1 addition & 14 deletions SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,8 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();

if (T_ARRAY === $tokens[$stackPtr]['code']) {
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');

// Array keyword should be lower case.
if (strtolower($tokens[$stackPtr]['content']) !== $tokens[$stackPtr]['content']) {
if (strtoupper($tokens[$stackPtr]['content']) === $tokens[$stackPtr]['content']) {
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'upper');
} else {
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'mixed');
}

$fix = $phpcsFile->addFixableError(
'Array keyword should be lower case; expected "array" but found "%s"',
$stackPtr,
Expand All @@ -56,8 +48,6 @@ public function process(File $phpcsFile, $stackPtr): void
if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr, 'array');
}
} else {
$phpcsFile->recordMetric($stackPtr, 'Array keyword case', 'lower');
}

$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
Expand All @@ -79,7 +69,6 @@ public function process(File $phpcsFile, $stackPtr): void
}
}
} else {
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes');
$arrayStart = $stackPtr;
$arrayEnd = $tokens[$stackPtr]['bracket_closer'];
}
Expand Down Expand Up @@ -449,17 +438,15 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
);

if (T_COMMA !== $tokens[$trailingContent]['code']) {
$phpcsFile->recordMetric($stackPtr, 'Array end comma', 'no');
$fix = $phpcsFile->addFixableError(
'Comma required after last value in array declaration',
$trailingContent,
'NoCommaAfterLast'
);

if ($fix) {
$phpcsFile->fixer->addContent($trailingContent, ',');
}
} else {
$phpcsFile->recordMetric($stackPtr, 'Array end comma', 'yes');
}

$lastValueLine = $stackPtr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public function process(File $phpcsFile, $stackPtr): void
$tokens = $phpcsFile->getTokens();
$opener = $phpcsFile->findPrevious([T_IF, T_CASE], $stackPtr);

if ($opener
if (false !== $opener
&& isset($tokens[$opener]['scope_closer'])
&& $stackPtr <= $tokens[$opener]['scope_closer']) {
&& $stackPtr <= $tokens[$opener]['scope_closer']
) {
$isClosure = $phpcsFile->findPrevious(T_CLOSURE, $stackPtr, $opener);
if (false !== $isClosure) {
return;
Expand Down
2 changes: 1 addition & 1 deletion SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool
|| ('class' === $type
&& ((T_DOC_COMMENT_STRING === $tokens[$classUsed]['code']
&& preg_match(
'/(\s|\||\(|\<|\,|^)'.preg_quote($searchName, '/').'(\s|\||\\\\|\<|\,|\>|$|\[\])/i',
'/(\s|\||\(|\<|\,|\:|^)'.preg_quote($searchName, '/').'(\s|\||\\\\|\<|\,|\>|$|\[\])/i',
$tokens[$classUsed]['content']
))
|| (T_DOC_COMMENT_TAG === $tokens[$classUsed]['code']
Expand Down
81 changes: 45 additions & 36 deletions SymfonyCustom/Sniffs/NamingConventions/ValidClassNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,72 +39,81 @@ public function process(File $phpcsFile, $stackPtr): void
if (T_INTERFACE === $tokens[$stackPtr]['code']) {
$name = $phpcsFile->findNext(T_STRING, $stackPtr);

if ($name && substr($tokens[$name]['content'], -9) !== 'Interface') {
$phpcsFile->addError(
'Interface name is not suffixed with "Interface"',
$stackPtr,
'InvalidInterfaceName'
);
}
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Interface');
break;
}

// Suffix traits with Trait
if (T_TRAIT === $tokens[$stackPtr]['code']) {
$name = $phpcsFile->findNext(T_STRING, $stackPtr);

if ($name && substr($tokens[$name]['content'], -5) !== 'Trait') {
$phpcsFile->addError(
'Trait name is not suffixed with "Trait"',
$stackPtr,
'InvalidTraitName'
);
}
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Trait');
break;
}

// Suffix exceptions with Exception;
if (T_EXTENDS === $tokens[$stackPtr]['code']) {
$extend = $phpcsFile->findNext(T_STRING, $stackPtr);

if ($extend
&& substr($tokens[$extend]['content'], -9) === 'Exception'
) {
if ($extend && substr($tokens[$extend]['content'], -9) === 'Exception') {
$class = $phpcsFile->findPrevious(T_CLASS, $stackPtr);
$name = $phpcsFile->findNext(T_STRING, $class);

if ($name
&& substr($tokens[$name]['content'], -9) !== 'Exception'
) {
$phpcsFile->addError(
'Exception name is not suffixed with "Exception"',
$stackPtr,
'InvalidExceptionName'
);
}
$this->checkSuffix($phpcsFile, $stackPtr, $name, 'Exception');
}
break;
}

// Prefix abstract classes with Abstract.
if (T_ABSTRACT === $tokens[$stackPtr]['code']) {
$name = $phpcsFile->findNext(T_STRING, $stackPtr);
$function = $phpcsFile->findNext(T_FUNCTION, $stackPtr);
$name = $phpcsFile->findNext([T_STRING, T_FUNCTION], $stackPtr);

// Making sure we're not dealing with an abstract function
if ($name && (false === $function || $name < $function)
&& substr($tokens[$name]['content'], 0, 8) !== 'Abstract'
) {
$phpcsFile->addError(
'Abstract class name is not prefixed with "Abstract"',
$stackPtr,
'InvalidAbstractName'
);
if (false !== $name && T_FUNCTION !== $tokens[$name]['code']) {
$this->checkPrefix($phpcsFile, $stackPtr, $name, 'Abstract');
}
break;
}

$stackPtr++;
}
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int|bool $name
* @param string $prefix
*/
private function checkPrefix(File $phpcsFile, int $stackPtr, $name, string $prefix): void
{
$tokens = $phpcsFile->getTokens();

if (false !== $name && substr($tokens[$name]['content'], 0, strlen($prefix)) !== $prefix) {
$phpcsFile->addError(
"$prefix name is not prefixed with '$prefix'",
$stackPtr,
"Invalid{$prefix}Name"
);
}
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @param int|bool $name
* @param string $suffix
*/
private function checkSuffix(File $phpcsFile, int $stackPtr, $name, string $suffix): void
{
$tokens = $phpcsFile->getTokens();

if (false !== $name && substr($tokens[$name]['content'], -strlen($suffix)) !== $suffix) {
$phpcsFile->addError(
"$suffix name is not suffixed with '$suffix'",
$stackPtr,
"Invalid{$suffix}Name"
);
}
}
}
3 changes: 0 additions & 3 deletions SymfonyCustom/Sniffs/NamingConventions/ValidFileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public function process(File $phpcsFile, $stackPtr): void
'Invalid',
[$filename]
);
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'no');
} else {
$phpcsFile->recordMetric($stackPtr, 'Alphanumeric filename', 'yes');
}
}
}
Loading