Skip to content

Commit

Permalink
Generic/LowerCaseType: refactor [1] - move array to property
Browse files Browse the repository at this point in the history
Move the `$phpTypes` array to a property and add the new types introduced in PHP 8.0 to it.
  • Loading branch information
jrfnl committed Nov 2, 2020
1 parent 8084948 commit 7e0f822
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@
class LowerCaseTypeSniff implements Sniff
{

/**
* Native types supported by PHP.
*
* @var array
*/
private $phpTypes = [
'self' => true,
'parent' => true,
'array' => true,
'callable' => true,
'bool' => true,
'float' => true,
'int' => true,
'string' => true,
'iterable' => true,
'void' => true,
'object' => true,
'mixed' => true,
'static' => true,
'false' => true,
'null' => true,
];


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

$phpTypes = [
'self' => true,
'parent' => true,
'array' => true,
'callable' => true,
'bool' => true,
'float' => true,
'int' => true,
'string' => true,
'iterable' => true,
'void' => true,
'object' => true,
];

$props = $phpcsFile->getMethodProperties($stackPtr);

// Strip off potential nullable indication.
$returnType = ltrim($props['return_type'], '?');
$returnTypeLower = strtolower($returnType);

if ($returnType !== ''
&& isset($phpTypes[$returnTypeLower]) === true
&& isset($this->phpTypes[$returnTypeLower]) === true
) {
// A function return type.
if ($returnTypeLower !== $returnType) {
Expand Down Expand Up @@ -129,7 +138,7 @@ public function process(File $phpcsFile, $stackPtr)
$typeHintLower = strtolower($typeHint);

if ($typeHint !== ''
&& isset($phpTypes[$typeHintLower]) === true
&& isset($this->phpTypes[$typeHintLower]) === true
) {
// A function return type.
if ($typeHintLower !== $typeHint) {
Expand Down

0 comments on commit 7e0f822

Please sign in to comment.