Skip to content

Commit cbe573e

Browse files
committed
Modernize: Util\Common: use class constant for constant array
As the property was in the public API, the class constant is also public and the property has been deprecated, to be removed in PHPCS 5.0. Includes changing the array format from a list of strings to an associative array with the key and value for each entry holding the same string to allow for using `isset()` instead of `in_array()`.
1 parent 8ee5e66 commit cbe573e

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
435435
$suggestedTypeHint = 'callable';
436436
} else if (strpos($suggestedName, 'callback') !== false) {
437437
$suggestedTypeHint = 'callable';
438-
} else if (in_array($suggestedName, Common::$allowedTypes, true) === false) {
438+
} else if (isset(Common::ALLOWED_TYPES[$suggestedName]) === false) {
439439
$suggestedTypeHint = $suggestedName;
440440
}
441441

src/Util/Common.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,29 @@ class Common
1818
/**
1919
* An array of variable types for param/var we will check.
2020
*
21-
* @var string[]
21+
* @var array<string, string>
2222
*/
23-
public static $allowedTypes = [
24-
'array',
25-
'boolean',
26-
'float',
27-
'integer',
28-
'mixed',
29-
'object',
30-
'string',
31-
'resource',
32-
'callable',
23+
public const ALLOWED_TYPES = [
24+
'array' => 'array',
25+
'boolean' => 'boolean',
26+
'float' => 'float',
27+
'integer' => 'integer',
28+
'mixed' => 'mixed',
29+
'object' => 'object',
30+
'string' => 'string',
31+
'resource' => 'resource',
32+
'callable' => 'callable',
3333
];
3434

35+
/**
36+
* An array of variable types for param/var we will check.
37+
*
38+
* @var array<string, string>
39+
*
40+
* @deprecated 4.0.0 Use the Common::ALLOWED_TYPES constant instead.
41+
*/
42+
public static $allowedTypes = self::ALLOWED_TYPES;
43+
3544

3645
/**
3746
* Return TRUE if the path is a PHAR file.
@@ -466,7 +475,7 @@ public static function suggestType($varType)
466475
return '';
467476
}
468477

469-
if (in_array($varType, self::$allowedTypes, true) === true) {
478+
if (isset(self::ALLOWED_TYPES[$varType]) === true) {
470479
return $varType;
471480
} else {
472481
$lowerVarType = strtolower($varType);
@@ -512,7 +521,7 @@ public static function suggestType($varType)
512521
} else {
513522
return 'array';
514523
}//end if
515-
} else if (in_array($lowerVarType, self::$allowedTypes, true) === true) {
524+
} else if (isset(self::ALLOWED_TYPES[$lowerVarType]) === true) {
516525
// A valid type, but not lower cased.
517526
return $lowerVarType;
518527
} else {

tests/Core/Util/Common/SuggestTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testSuggestTypeAllowedType($varType)
6060
public static function dataSuggestTypeAllowedType()
6161
{
6262
$data = [];
63-
foreach (Common::$allowedTypes as $type) {
63+
foreach (Common::ALLOWED_TYPES as $type) {
6464
$data['Type: '.$type] = [$type];
6565
}
6666

@@ -97,7 +97,7 @@ public function testSuggestTypeAllowedTypeWrongCase($varType, $expected)
9797
public static function dataSuggestTypeAllowedTypeWrongCase()
9898
{
9999
$data = [];
100-
foreach (Common::$allowedTypes as $type) {
100+
foreach (Common::ALLOWED_TYPES as $type) {
101101
$data['Mixed case: '.$type] = [
102102
'varType' => ucfirst($type),
103103
'expected' => $type,

0 commit comments

Comments
 (0)