Skip to content

Modernize: use class constants for constant arrays (public API) #1043

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

Draft
wants to merge 9 commits into
base: phpcs-4.0/feature/squiz-variablecomment-make-tagnotallowed-modular
Choose a base branch
from
13 changes: 11 additions & 2 deletions src/Sniffs/AbstractVariableSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ abstract class AbstractVariableSniff extends AbstractScopeSniff
*
* Used by various naming convention sniffs.
*
* @var array
* @var array<string, true>
*/
protected $phpReservedVars = [
protected const PHP_RESERVED_VARS = [
'_SERVER' => true,
'_GET' => true,
'_POST' => true,
Expand All @@ -43,6 +43,15 @@ abstract class AbstractVariableSniff extends AbstractScopeSniff
'php_errormsg' => true,
];

/**
* List of PHP Reserved variables.
*
* @var array<string, true>
*
* @deprecated 4.0.0 Use the AbstractVariableSniff::PHP_RESERVED_VARS constant instead.
*/
protected $phpReservedVars = self::PHP_RESERVED_VARS;


/**
* Constructs an AbstractVariableTest.
Expand Down
15 changes: 12 additions & 3 deletions src/Standards/Generic/Sniffs/Files/ByteOrderMarkSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ class ByteOrderMarkSniff implements Sniff
*
* Use encoding names as keys and hex BOM representations as values.
*
* @var array
* @var array<string, string>
*/
protected $bomDefinitions = [
protected const BOM_DEFINITIONS = [
'UTF-8' => 'efbbbf',
'UTF-16 (BE)' => 'feff',
'UTF-16 (LE)' => 'fffe',
];

/**
* List of supported BOM definitions.
*
* @var array<string, string>
*
* @deprecated 4.0.0 Use the ByteOrderMarkSniff::BOM_DEFINITIONS constant instead.
*/
protected $bomDefinitions = self::BOM_DEFINITIONS;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -60,7 +69,7 @@ public function process(File $phpcsFile, $stackPtr)

$tokens = $phpcsFile->getTokens();

foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
foreach (static::BOM_DEFINITIONS as $bomName => $expectedBomHex) {
$bomByteLength = (strlen($expectedBomHex) / 2);
$htmlBomHex = bin2hex(substr($tokens[$stackPtr]['content'], 0, $bomByteLength));
if ($htmlBomHex === $expectedBomHex) {
Expand Down
15 changes: 12 additions & 3 deletions src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ class InlineHTMLSniff implements Sniff
*
* Use encoding names as keys and hex BOM representations as values.
*
* @var array
* @var array<string, string>
*/
protected $bomDefinitions = [
protected const BOM_DEFINITIONS = [
'UTF-8' => 'efbbbf',
'UTF-16 (BE)' => 'feff',
'UTF-16 (LE)' => 'fffe',
];

/**
* List of supported BOM definitions.
*
* @var array<string, string>
*
* @deprecated 4.0.0 Use the InlineHTMLSniff::BOM_DEFINITIONS constant instead.
*/
protected $bomDefinitions = self::BOM_DEFINITIONS;


/**
* Returns an array of tokens this test wants to listen for.
Expand All @@ -54,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
{
// Allow a byte-order mark.
$tokens = $phpcsFile->getTokens();
foreach ($this->bomDefinitions as $expectedBomHex) {
foreach (static::BOM_DEFINITIONS as $expectedBomHex) {
$bomByteLength = (strlen($expectedBomHex) / 2);
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
if ($htmlBomHex === $expectedBomHex && strlen($tokens[0]['content']) === $bomByteLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class CamelCapsFunctionNameSniff extends AbstractScopeSniff
/**
* A list of all PHP magic methods.
*
* @var array
* @var array<string, true>
*/
protected $magicMethods = [
protected const MAGIC_METHODS = [
'construct' => true,
'destruct' => true,
'call' => true,
Expand All @@ -47,9 +47,9 @@ class CamelCapsFunctionNameSniff extends AbstractScopeSniff
*
* These come from PHP modules such as SOAPClient.
*
* @var array
* @var array<string, true>
*/
protected $methodsDoubleUnderscore = [
protected const DOUBLE_UNDERSCORE_METHODS = [
'dorequest' => true,
'getcookies' => true,
'getfunctions' => true,
Expand All @@ -67,9 +67,9 @@ class CamelCapsFunctionNameSniff extends AbstractScopeSniff
/**
* A list of all PHP magic functions.
*
* @var array
* @var array<string, true>
*/
protected $magicFunctions = ['autoload' => true];
protected const MAGIC_FUNCTIONS = ['autoload' => true];

/**
* If TRUE, the string must not have two capital letters next to each other.
Expand All @@ -78,6 +78,33 @@ class CamelCapsFunctionNameSniff extends AbstractScopeSniff
*/
public $strict = true;

/**
* A list of all PHP magic methods.
*
* @var array<string, true>
*
* @deprecated 4.0.0 Use the CamelCapsFunctionNameSniff::MAGIC_METHODS constant instead.
*/
protected $magicMethods = self::MAGIC_METHODS;

/**
* A list of all PHP non-magic methods starting with a double underscore.
*
* @var array<string, true>
*
* @deprecated 4.0.0 Use the CamelCapsFunctionNameSniff::DOUBLE_UNDERSCORE_METHODS constant instead.
*/
protected $methodsDoubleUnderscore = self::DOUBLE_UNDERSCORE_METHODS;

/**
* A list of all PHP magic functions.
*
* @var array<string, true>
*
* @deprecated 4.0.0 Use the CamelCapsFunctionNameSniff::MAGIC_FUNCTIONS constant instead.
*/
protected $magicFunctions = self::MAGIC_FUNCTIONS;


/**
* Constructs a Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff.
Expand Down Expand Up @@ -130,8 +157,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
// Is this a magic method. i.e., is prefixed with "__" ?
if (preg_match('|^__[^_]|', $methodName) !== 0) {
$magicPart = substr($methodNameLc, 2);
if (isset($this->magicMethods[$magicPart]) === true
|| isset($this->methodsDoubleUnderscore[$magicPart]) === true
if (isset(static::MAGIC_METHODS[$magicPart]) === true
|| isset(static::DOUBLE_UNDERSCORE_METHODS[$magicPart]) === true
) {
return;
}
Expand Down Expand Up @@ -197,7 +224,7 @@ protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
// Is this a magic function. i.e., it is prefixed with "__".
if (preg_match('|^__[^_]|', $functionName) !== 0) {
$magicPart = strtolower(substr($functionName, 2));
if (isset($this->magicFunctions[$magicPart]) === true) {
if (isset(static::MAGIC_FUNCTIONS[$magicPart]) === true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ class CharacterBeforePHPOpeningTagSniff implements Sniff
*
* Use encoding names as keys and hex BOM representations as values.
*
* @var array
* @var array<string, string>
*/
protected $bomDefinitions = [
protected const BOM_DEFINITIONS = [
'UTF-8' => 'efbbbf',
'UTF-16 (BE)' => 'feff',
'UTF-16 (LE)' => 'fffe',
];

/**
* List of supported BOM definitions.
*
* @var array<string, string>
*
* @deprecated 4.0.0 Use the CharacterBeforePHPOpeningTagSniff::BOM_DEFINITIONS constant instead.
*/
protected $bomDefinitions = self::BOM_DEFINITIONS;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -56,7 +65,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($stackPtr > 0) {
// Allow a byte-order mark.
$tokens = $phpcsFile->getTokens();
foreach ($this->bomDefinitions as $expectedBomHex) {
foreach (static::BOM_DEFINITIONS as $expectedBomHex) {
$bomByteLength = (strlen($expectedBomHex) / 2);
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
if ($htmlBomHex === $expectedBomHex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ class SubversionPropertiesSniff implements Sniff
* exact value the property should have or NULL if the
* property should just be set but the value is not fixed.
*
* @var array
* @var array<string, string>
*/
protected $properties = [
protected const REQUIRED_PROPERTIES = [
'svn:keywords' => 'Author Id Revision',
'svn:eol-style' => 'native',
];

/**
* The Subversion properties that should be set.
*
* @var array<string, string>
*
* @deprecated 4.0.0 Use the SubversionPropertiesSniff::REQUIRED_PROPERTIES constant instead.
*/
protected $properties = self::REQUIRED_PROPERTIES;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -61,10 +70,10 @@ public function process(File $phpcsFile, $stackPtr)
return $phpcsFile->numTokens;
}

$allProperties = ($properties + $this->properties);
$allProperties = ($properties + static::REQUIRED_PROPERTIES);
foreach ($allProperties as $key => $value) {
if (isset($properties[$key]) === true
&& isset($this->properties[$key]) === false
&& isset(static::REQUIRED_PROPERTIES[$key]) === false
) {
$error = 'Unexpected Subversion property "%s" = "%s"';
$data = [
Expand All @@ -76,25 +85,25 @@ public function process(File $phpcsFile, $stackPtr)
}

if (isset($properties[$key]) === false
&& isset($this->properties[$key]) === true
&& isset(static::REQUIRED_PROPERTIES[$key]) === true
) {
$error = 'Missing Subversion property "%s" = "%s"';
$data = [
$key,
$this->properties[$key],
static::REQUIRED_PROPERTIES[$key],
];
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
continue;
}

if ($properties[$key] !== null
&& $properties[$key] !== $this->properties[$key]
&& $properties[$key] !== static::REQUIRED_PROPERTIES[$key]
) {
$error = 'Subversion property "%s" = "%s" does not match "%s"';
$data = [
$key,
$properties[$key],
$this->properties[$key],
static::REQUIRED_PROPERTIES[$key],
];
$phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
}
Expand Down
19 changes: 14 additions & 5 deletions src/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class FileCommentSniff implements Sniff
/**
* Tags in correct order and related info.
*
* @var array
* @var array<string, array<string, bool>>
*/
protected $tags = [
protected const EXPECTED_TAGS = [
'@category' => [
'required' => true,
'allow_multiple' => false,
Expand Down Expand Up @@ -68,6 +68,15 @@ class FileCommentSniff implements Sniff
],
];

/**
* Tags in correct order and related info.
*
* @var array<string, array<string, bool>>
*
* @deprecated 4.0.0 Use the FileCommentSniff::EXPECTED_TAGS constant instead.
*/
protected $tags = self::EXPECTED_TAGS;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -235,11 +244,11 @@ protected function processTags($phpcsFile, $stackPtr, $commentStart)
$tagTokens = [];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
$name = $tokens[$tag]['content'];
if (isset($this->tags[$name]) === false) {
if (isset(static::EXPECTED_TAGS[$name]) === false) {
continue;
}

if ($this->tags[$name]['allow_multiple'] === false && isset($tagTokens[$name]) === true) {
if (static::EXPECTED_TAGS[$name]['allow_multiple'] === false && isset($tagTokens[$name]) === true) {
$error = 'Only one %s tag is allowed in a %s comment';
$data = [
$name,
Expand All @@ -265,7 +274,7 @@ protected function processTags($phpcsFile, $stackPtr, $commentStart)

// Check if the tags are in the correct position.
$pos = 0;
foreach ($this->tags as $tag => $tagData) {
foreach (static::EXPECTED_TAGS as $tag => $tagData) {
if (isset($tagTokens[$tag]) === false) {
if ($tagData['required'] === true) {
$error = 'Missing %s tag in %s comment';
Expand Down
Loading