Skip to content

Commit 7185370

Browse files
authored
Merge pull request #641 from rodrigoprimo/test-coverage-abstract-class-name-prefix
Generic/AbstractClassNamePrefix: improve code coverage
2 parents 3ba1aff + b80228e commit 7185370

File tree

5 files changed

+70
-68
lines changed

5 files changed

+70
-68
lines changed

src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function process(File $phpcsFile, $stackPtr)
4545

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
abstract class IncorrectName {} // Error.
4+
5+
abstract class AbstractCorrectName {}
6+
7+
abstract class IncorrectNameAbstract {} // Error.
8+
9+
abstract
10+
/*comment*/
11+
class
12+
InvalidNameabstract
13+
extends
14+
BarClass {} // Error.
15+
16+
abstract class /*comment*/ IncorrectAbstractName {} // Error.
17+
18+
// Anonymous classes can't be declared as abstract (and don't have a name anyhow).
19+
$anon = new class {};
20+
21+
// Make sure that if the class is not abstract, the sniff does not check the name.
22+
class AbstractClassName {}
23+
24+
// Class name is always checked, doesn't matter if the class is declared conditionally.
25+
if (!class_exists('AbstractClassCorrectName')) {
26+
abstract class AbstractClassCorrectName {}
27+
}
28+
if (!class_exists('ClassAbstractIncorrectName')) {
29+
abstract class ClassAbstractIncorrectName implements FooInterface {} // Error.
30+
}
31+
32+
$var = 'abstract class TextStringsAreDisregarded';
33+
34+
class NotAnAbstractClassSoNoPrefixRequired {}
35+
36+
abstract class abstractOkCaseOfPrefixIsNotEnforced {}
37+
38+
final class FinalClassShouldNotTriggerWarning {}
39+
40+
readonly class ReadonlyClassShouldNotTriggerWarning {}
41+
42+
abstract readonly class AbstractReadonlyClassWithPrefixShouldNotTriggerWarning {}
43+
44+
abstract readonly class ReadonlyAbstractClassShouldTriggerWarningWhenPrefixIsMissingA {} // Error.
45+
readonly abstract class ReadonlyAbstractClassShouldTriggerWarningWhenPrefixIsMissingB {} // Error.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (no class name).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
abstract class

src/Standards/Generic/Tests/NamingConventions/AbstractClassNamePrefixUnitTest.inc

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Standards/Generic/Tests/NamingConventions/AbstractClassNamePrefixUnitTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,26 @@ final class AbstractClassNamePrefixUnitTest extends AbstractSniffUnitTest
2525
* The key of the array should represent the line number and the value
2626
* should represent the number of errors that should occur on that line.
2727
*
28+
* @param string $testFile The name of the test file.
29+
*
2830
* @return array<int, int>
2931
*/
30-
public function getErrorList()
32+
public function getErrorList($testFile='')
3133
{
32-
return [
33-
3 => 1,
34-
13 => 1,
35-
18 => 1,
36-
23 => 1,
37-
42 => 1,
38-
];
34+
switch ($testFile) {
35+
case 'AbstractClassNamePrefixUnitTest.1.inc':
36+
return [
37+
3 => 1,
38+
7 => 1,
39+
11 => 1,
40+
16 => 1,
41+
29 => 1,
42+
44 => 1,
43+
45 => 1,
44+
];
45+
default:
46+
return [];
47+
}
3948

4049
}//end getErrorList()
4150

0 commit comments

Comments
 (0)