Skip to content

Commit

Permalink
PHP 7.4 | Generic/LowerCaseType: add support for typed properties
Browse files Browse the repository at this point in the history
PHP 7.4 introduced typed properties, but this sniff did not examine those yet.

Includes unit tests.
  • Loading branch information
jrfnl committed Nov 2, 2020
1 parent b4313a7 commit d1fdf32
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/LowerCaseTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function register()
$tokens = Tokens::$castTokens;
$tokens[] = T_FUNCTION;
$tokens[] = T_CLOSURE;
$tokens[] = T_VARIABLE;
return $tokens;

}//end register()
Expand Down Expand Up @@ -81,6 +83,41 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

/*
* Check property types.
*/

if ($tokens[$stackPtr]['code'] === T_VARIABLE) {
try {
$props = $phpcsFile->getMemberProperties($stackPtr);
} catch (RuntimeException $e) {
// Not an OO property.
return;
}

// Strip off potential nullable indication.
$type = ltrim($props['type'], '?');

if ($type !== '') {
$error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s"';
$errorCode = 'PropertyTypeFound';

if (strpos($type, '|') !== false) {
$this->processUnionType(
$phpcsFile,
$props['type_token'],
$props['type_end_token'],
$error,
$errorCode
);
} else if (isset($this->phpTypes[strtolower($type)]) === true) {
$this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode);
}
}

return;
}//end if

/*
* Check function return type.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | Int | \Package\Other_Class | FAL
function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ NULL {}

function unionReturnTypesB ($var): \Package\ClassName | Int | \Package\Other_Class | FALSE {}

class TypedProperties
{
protected ClassName $class;
public Int $int;
private ?BOOL $bool;
public Self $self;
protected PaRenT $parent;
private ARRAY $array;
public Float $float;
protected ?STRING $string;
private IterablE $iterable;
public Object $object;
protected Mixed $mixed;

public Iterable|FALSE|NULL $unionTypeA;
protected SELF|Parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB;
private ClassName|/*comment*/Float|STRING|False $unionTypeC;
public sTRing | aRRaY | FaLSe $unionTypeD;
}
20 changes: 20 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ function unionParamTypesB (\Package\ClassName | int | \Package\Other_Class | fal
function unionReturnTypesA ($var): bool|array| /* nullability operator not allowed in union */ null {}

function unionReturnTypesB ($var): \Package\ClassName | int | \Package\Other_Class | false {}

class TypedProperties
{
protected ClassName $class;
public int $int;
private ?bool $bool;
public self $self;
protected parent $parent;
private array $array;
public float $float;
protected ?string $string;
private iterable $iterable;
public object $object;
protected mixed $mixed;

public iterable|false|null $unionTypeA;
protected self|parent /* comment */ |\Fully\Qualified\ClassName|UnQualifiedClass $unionTypeB;
private ClassName|/*comment*/float|string|false $unionTypeC;
public string | array | false $unionTypeD;
}
14 changes: 14 additions & 0 deletions src/Standards/Generic/Tests/PHP/LowerCaseTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function getErrorList()
51 => 2,
53 => 1,
55 => 2,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
64 => 1,
65 => 1,
66 => 1,
67 => 1,
68 => 1,
69 => 1,
71 => 3,
72 => 2,
73 => 3,
74 => 3,
];

}//end getErrorList()
Expand Down

0 comments on commit d1fdf32

Please sign in to comment.