Skip to content

Generic/AbstractClassNamePrefix: improve code coverage #641

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function process(File $phpcsFile, $stackPtr)

$className = $phpcsFile->getDeclarationName($stackPtr);
if ($className === null) {
// We are not interested in anonymous classes.
// Live coding or parse error.
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

abstract class IncorrectName {} // Error.

abstract class AbstractCorrectName {}

abstract class IncorrectNameAbstract {} // Error.

abstract
/*comment*/
class
InvalidNameabstract
extends
BarClass {} // Error.

abstract class /*comment*/ IncorrectAbstractName {} // Error.

// Anonymous classes can't be declared as abstract (and don't have a name anyhow).
$anon = new class {};

// Make sure that if the class is not abstract, the sniff does not check the name.
class AbstractClassName {}

// Class name is always checked, doesn't matter if the class is declared conditionally.
if (!class_exists('AbstractClassCorrectName')) {
abstract class AbstractClassCorrectName {}
}
if (!class_exists('ClassAbstractIncorrectName')) {
abstract class ClassAbstractIncorrectName implements FooInterface {} // Error.
}

$var = 'abstract class TextStringsAreDisregarded';

class NotAnAbstractClassSoNoPrefixRequired {}

abstract class abstractOkCaseOfPrefixIsNotEnforced {}

final class FinalClassShouldNotTriggerWarning {}

readonly class ReadonlyClassShouldNotTriggerWarning {}

abstract readonly class AbstractReadonlyClassWithPrefixShouldNotTriggerWarning {}

abstract readonly class ReadonlyAbstractClassShouldTriggerWarningWhenPrefixIsMissingA {} // Error.
readonly abstract class ReadonlyAbstractClassShouldTriggerWarningWhenPrefixIsMissingB {} // Error.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (no class name).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

abstract class

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ final class AbstractClassNamePrefixUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the test file.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
3 => 1,
13 => 1,
18 => 1,
23 => 1,
42 => 1,
];
switch ($testFile) {
case 'AbstractClassNamePrefixUnitTest.1.inc':
return [
3 => 1,
7 => 1,
11 => 1,
16 => 1,
29 => 1,
44 => 1,
45 => 1,
];
default:
return [];
}

}//end getErrorList()

Expand Down