Skip to content
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

Various updates to enum sniff fixes #1

Merged
merged 13 commits into from
Jan 19, 2022
Merged
2 changes: 1 addition & 1 deletion src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ public function getFilename()


/**
* Returns the declaration names for classes, interfaces, traits, and functions.
* Returns the declaration name for classes, interfaces, traits, enums, and functions.
*
* @param int $stackPtr The position of the declaration token which
* declared the class, interface, trait, or function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class YourClass {}
interface MyInterface {}
interface YourInterface {}
trait MyTrait {}
enum MyEnum {}
trait YourTrait {}
enum MyEnum {}
enum YourEnum {}
class MyClass {}
interface MyInterface {}
trait MyTrait {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
class MyClass {}
interface MyInterface {}
trait MyTrait {}
enum MyEnum {}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ public function getWarningList($testFile='')
switch ($testFile) {
case 'DuplicateClassNameUnitTest.1.inc':
return [
9 => 1,
10 => 1,
11 => 1,
12 => 1,
13 => 1,
];
break;
case 'DuplicateClassNameUnitTest.2.inc':
return [
2 => 1,
3 => 1,
4 => 1,
5 => 1,
];
break;
case 'DuplicateClassNameUnitTest.5.inc':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,20 @@ abstract class My_Class {
public function _MY_CLASS() {}
}

enum My_Enum {
public function my_class() {}
public function _MY_CLASS() {}
enum Suit: string implements Colorful, CardGame {
// Magic methods.
function __call($name, $args) {}
static function __callStatic($name, $args) {}
function __invoke() {}

// Valid Method Name.
public function getSomeValue() {}

// Double underscore non-magic methods not allowed.
function __myFunction() {}
function __my_function() {}

// Non-camelcase.
public function parseMyDSN() {}
public function get_some_value() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public function getErrorList()
147 => 2,
158 => 1,
159 => 1,
170 => 1,
171 => 1,
179 => 1,
180 => 2,
183 => 1,
184 => 1,
];

return $errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ trait ___ {}

trait Invalid__Name {}

enum Valid_Name {}
enum Valid_Name: string {}

enum invalid_Name {}
enum invalid_Name : String {}

enum invalid_name {}

enum Invalid_name {}
enum Invalid_name: Int {}

enum VALID_Name {}

enum VALID_NAME {}

enum VALID_Name {}
enum VALID_Name : int {}

enum ValidName {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,23 @@ abstract class My_Class {
public function _MY_CLASS() {}
}

enum My_Enum {
public function my_class() {}
public function _MY_CLASS() {}
enum Suit: string implements Colorful, CardGame {
// Magic methods.
function __call($name, $args) {}
static function __callStatic($name, $args) {}
function __invoke() {}

// Valid Method Name.
public function parseMyDSN() {}
private function _getAnotherValue() {}

// Double underscore non-magic methods not allowed.
function __myFunction() {}
function __my_function() {}

// Non-camelcase.
public function get_some_value() {}

// Private without underscore prefix.
private function getMe() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ public function getErrorList()
212 => 1,
213 => 1,
214 => 1,
225 => 1,
226 => 2,
235 => 1,
236 => 2,
239 => 1,
242 => 1,
];

}//end getErrorList()
Expand Down
3 changes: 2 additions & 1 deletion src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ enum SomeEnum1

enum SomeEnum2
{
use FirstTrait;

use FirstTrait;

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getErrorList()
165 => 1,
170 => 1,
208 => 1,
218 => 1,
219 => 3,
];

}//end getErrorList()
Expand Down
2 changes: 1 addition & 1 deletion src/Standards/Squiz/Sniffs/Classes/ValidClassNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr)
// starting with the number will be multiple tokens.
$opener = $tokens[$stackPtr]['scope_opener'];
$nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
$nameEnd = $phpcsFile->findNext(T_WHITESPACE, $nameStart, $opener);
$nameEnd = $phpcsFile->findNext([T_WHITESPACE, T_COLON], $nameStart, $opener);
if ($nameEnd === false) {
$name = $tokens[$nameStart]['content'];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ClassFileNameUnitTest {}
interface ClassFileNameUnitTest {}
trait ClassFileNameUnitTest {}
enum ClassFileNameUnitTest {}

enum ClassFileNameUnitTest: int {}

// Invalid filename matching class name (case sensitive).
class classFileNameUnitTest {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Abstract Class MyClass Extends MyClass {}
Final Class MyClass Implements MyInterface {}
Interface MyInterface {}
Trait MyTrait {}
Enum MyEnum {}
Enum MyEnum IMPLEMENTS Colorful {}

class MyClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ abstract class MyClass extends MyClass {}
final class MyClass implements MyInterface {}
interface MyInterface {}
trait MyTrait {}
enum MyEnum {}
enum MyEnum implements Colorful {}

class MyClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getErrorList()
3 => 3,
4 => 1,
5 => 1,
6 => 1,
6 => 2,
10 => 1,
11 => 1,
14 => 1,
Expand Down
10 changes: 5 additions & 5 deletions src/Standards/Squiz/Tests/Classes/ValidClassNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ trait Base
}

// Valid enum name.
enum ValidCamelCaseClass {}
enum ValidCamelCaseClass: string {}


// Incorrect usage of camel case.
Expand All @@ -147,22 +147,22 @@ enum Invalid_Camel_Case_Class_With_Underscores {}


// All lowercase.
enum invalidlowercaseclass {}
enum invalidlowercaseclass: INT {}
enum invalid_lowercase_class_with_underscores {}


// All uppercase.
enum VALIDUPPERCASECLASS {}
enum VALIDUPPERCASECLASS: int {}
enum INVALID_UPPERCASE_CLASS_WITH_UNDERSCORES {}


// Mix camel case with uppercase.
enum ValidCamelCaseClassWithUPPERCASE {}
enum ValidCamelCaseClassWithUPPERCASE : string {}


// Usage of numeric characters.
enum ValidCamelCaseClassWith1Number {}
enum ValidCamelCaseClassWith12345Numbers {}
enum ValidCamelCaseClassWith12345Numbers : string {}
enum ValidCamelCaseClassEndingWithNumber5 {}

enum Testing{}
Expand Down
16 changes: 8 additions & 8 deletions src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ final class MyClass
final public function test() {}
}

/**
* Comment should be ignored.
*
*/
enum MyEnum {

}

/*
* N.B.: The below test line must be the last test in the file.
* Testing that a new line after an inline comment when it's the last non-whitespace
Expand All @@ -173,11 +181,3 @@ final class MyClass
*/

// For this test line having an empty line below it, is fine.

/**
* Comment should be ignored.
*
*/
enum MyEnum {

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ final class MyClass
final public function test() {}
}

/**
* Comment should be ignored.
*
*/
enum MyEnum {

}

/*
* N.B.: The below test line must be the last test in the file.
* Testing that a new line after an inline comment when it's the last non-whitespace
Expand All @@ -166,11 +174,3 @@ final class MyClass
*/

// For this test line having an empty line below it, is fine.

/**
* Comment should be ignored.
*
*/
enum MyEnum {

}
6 changes: 3 additions & 3 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ trait Something {
}

enum Something {
function getReturnType() {
echo 'no error';
}
function getReturnType() {
echo 'no error';
}
}

$a = new class {
Expand Down
4 changes: 4 additions & 0 deletions src/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ $b = new class()
}

enum MyEnum {
private function notStatic () {
$this->doSomething();
}

public static function myFunc() {
$this->doSomething();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getErrorList()
80 => 1,
84 => 1,
99 => 1,
121 => 1,
125 => 1,
];

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ $expr = match( $foo ){
};
echo $expr;

enum SomeEnum
{
if($true) {

enum SomeEnum {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ $expr = match($foo){

echo $expr;

enum SomeEnum
{
if($true) {

enum SomeEnum {}

}