Skip to content
24 changes: 14 additions & 10 deletions src/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
class FileCommentSniff implements Sniff
{

/**
* Required tags in correct order.
*
* @var array<string, true>
*/
private const REQUIRED_TAGS = [
'@package' => true,
'@subpackage' => true,
'@author' => true,
'@copyright' => true,
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -123,18 +135,10 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment');
}

// Required tags in correct order.
$required = [
'@package' => true,
'@subpackage' => true,
'@author' => true,
'@copyright' => true,
];

$foundTags = [];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
$name = $tokens[$tag]['content'];
$isRequired = isset($required[$name]);
$isRequired = isset(self::REQUIRED_TAGS[$name]);

if ($isRequired === true && in_array($name, $foundTags, true) === true) {
$error = 'Only one %s tag is allowed in a file comment';
Expand Down Expand Up @@ -185,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)

// Check if the tags are in the correct position.
$pos = 0;
foreach ($required as $tag => $true) {
foreach (self::REQUIRED_TAGS as $tag => $true) {
if (in_array($tag, $foundTags, true) === false) {
$error = 'Missing %s tag in file comment';
$data = [$tag];
Expand Down
23 changes: 14 additions & 9 deletions src/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
class InlineCommentSniff implements Sniff
{

/**
* Characters which are accepted to end a sentence.
*
* @var array<string, string>
*/
private const VALID_SENTENCE_END_CHARS = [
'full-stops' => '.',
'exclamation marks' => '!',
'or question marks' => '?',
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -227,17 +238,11 @@ public function process(File $phpcsFile, $stackPtr)
// Only check the end of comment character if the start of the comment
// is a letter, indicating that the comment is just standard text.
if (preg_match('/^\p{L}/u', $commentText) === 1) {
$commentCloser = $commentText[(strlen($commentText) - 1)];
$acceptedClosers = [
'full-stops' => '.',
'exclamation marks' => '!',
'or question marks' => '?',
];

if (in_array($commentCloser, $acceptedClosers, true) === false) {
$commentCloser = $commentText[(strlen($commentText) - 1)];
if (in_array($commentCloser, self::VALID_SENTENCE_END_CHARS, true) === false) {
$error = 'Inline comments must end in %s';
$ender = '';
foreach ($acceptedClosers as $closerName => $symbol) {
foreach (self::VALID_SENTENCE_END_CHARS as $closerName => $symbol) {
$ender .= ' '.$closerName.',';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class LongConditionClosingCommentSniff implements Sniff
{

/**
* The openers that we are interested in.
* The condition openers that we are interested in.
*
* @var array<int|string>
* @var array<int|string, int|string>
*/
private static $openers = [
T_SWITCH,
T_IF,
T_FOR,
T_FOREACH,
T_WHILE,
T_TRY,
T_CASE,
T_MATCH,
private const CONDITION_OPENERS = [
T_SWITCH => T_SWITCH,
T_IF => T_IF,
T_FOR => T_FOR,
T_FOREACH => T_FOREACH,
T_WHILE => T_WHILE,
T_TRY => T_TRY,
T_CASE => T_CASE,
T_MATCH => T_MATCH,
];

/**
Expand Down Expand Up @@ -84,7 +84,7 @@ public function process(File $phpcsFile, $stackPtr)
$endBrace = $tokens[$stackPtr];

// We are only interested in some code blocks.
if (in_array($startCondition['code'], self::$openers, true) === false) {
if (isset(self::CONDITION_OPENERS[$startCondition['code']]) === false) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class PostStatementCommentSniff implements Sniff
* If post statement comments are found within the condition
* parenthesis of these structures, leave them alone.
*
* @var array
* @var array<int|string, true>
*/
private $controlStructureExceptions = [
private const CONTROL_STRUCTURE_EXCEPTIONS = [
T_IF => true,
T_ELSEIF => true,
T_SWITCH => true,
Expand Down Expand Up @@ -92,7 +92,7 @@ public function process(File $phpcsFile, $stackPtr)
$nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
foreach ($nestedParens as $open => $close) {
if (isset($tokens[$open]['parenthesis_owner']) === true
&& isset($this->controlStructureExceptions[$tokens[$tokens[$open]['parenthesis_owner']]['code']]) === true
&& isset(self::CONTROL_STRUCTURE_EXCEPTIONS[$tokens[$tokens[$open]['parenthesis_owner']]['code']]) === true
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ComparisonOperatorUsageSniff implements Sniff
/**
* A list of valid comparison operators.
*
* @var array
* @var array<int|string, true>
*/
private static $validOps = [
private const VALID_OPERATIONS = [
T_IS_IDENTICAL => true,
T_IS_NOT_IDENTICAL => true,
T_LESS_THAN => true,
Expand All @@ -36,7 +36,7 @@ class ComparisonOperatorUsageSniff implements Sniff
*
* @var array<int|string, string>
*/
private static $invalidOps = [
private const INVALID_OPERATIONS = [
T_IS_EQUAL => '===',
T_IS_NOT_EQUAL => '!==',
T_BOOLEAN_NOT => '=== FALSE',
Expand Down Expand Up @@ -149,15 +149,15 @@ public function process(File $phpcsFile, $stackPtr)

for ($i = $start; $i <= $end; $i++) {
$type = $tokens[$i]['code'];
if (isset(self::$invalidOps[$type]) === true) {
if (isset(self::INVALID_OPERATIONS[$type]) === true) {
$error = 'Operator %s prohibited; use %s instead';
$data = [
$tokens[$i]['content'],
self::$invalidOps[$type],
self::INVALID_OPERATIONS[$type],
];
$phpcsFile->addError($error, $i, 'NotAllowed', $data);
$foundOps++;
} else if (isset(self::$validOps[$type]) === true) {
} else if (isset(self::VALID_OPERATIONS[$type]) === true) {
$foundOps++;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class NonExecutableCodeSniff implements Sniff
* `throw` can be used as an expression since PHP 8.0.
* {@link https://wiki.php.net/rfc/throw_expression}
*
* @var array
* @var array<int, int>
*/
private $expressionTokens = [
private const EXPRESSION_TOKENS = [
T_EXIT => T_EXIT,
T_THROW => T_THROW,
];
Expand Down Expand Up @@ -68,7 +68,7 @@ public function process(File $phpcsFile, $stackPtr)
$prev = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);

// Tokens which can be used in inline expressions need special handling.
if (isset($this->expressionTokens[$tokens[$stackPtr]['code']]) === true) {
if (isset(self::EXPRESSION_TOKENS[$tokens[$stackPtr]['code']]) === true) {
// If this token is preceded by a logical operator, it only relates to one line
// and should be ignored. For example: fopen() or die().
// Note: There is one exception: throw expressions can not be used with xor.
Expand Down
49 changes: 27 additions & 22 deletions src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@
class DoubleQuoteUsageSniff implements Sniff
{

/**
* Escape chars which are supported in double quoted strings, but not in single quoted strings.
*
* @var array<string>
*/
private const ESCAPE_CHARS = [
'\0',
'\1',
'\2',
'\3',
'\4',
'\5',
'\6',
'\7',
'\n',
'\r',
'\f',
'\t',
'\v',
'\x',
'\b',
'\e',
'\u',
'\'',
];


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -91,28 +117,7 @@ public function process(File $phpcsFile, $stackPtr)
return $skipTo;
}//end if

$allowedChars = [
'\0',
'\1',
'\2',
'\3',
'\4',
'\5',
'\6',
'\7',
'\n',
'\r',
'\f',
'\t',
'\v',
'\x',
'\b',
'\e',
'\u',
'\'',
];

foreach ($allowedChars as $testChar) {
foreach (self::ESCAPE_CHARS as $testChar) {
if (strpos($workingString, $testChar) !== false) {
return $skipTo;
}
Expand Down
Loading