-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForbidMethodDeclarationSniff.php
More file actions
49 lines (41 loc) · 1.37 KB
/
ForbidMethodDeclarationSniff.php
File metadata and controls
49 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types=1);
namespace IxDFCodingStandard\Sniffs\Classes;
use IxDFCodingStandard\Helpers\ClassHelper;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class ForbidMethodDeclarationSniff implements Sniff
{
public const FORBIDDEN_METHOD_DECLARATION = 'ForbiddenMethodDeclaration';
/**
* A list of forbidden to declare methods.
* @var array<string, string>
*/
public $forbiddenMethods = [];
/** @return list<int> */
public function register(): array
{
return [
\T_CLASS,
];
}
/** @param int $classPointer */
public function process(File $phpcsFile, $classPointer): void
{
/** @var class-string $fqcn */
$fqcn = ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer);
foreach ($this->forbiddenMethods as $typeAndMethod => $replacement) {
[$type, $method] = explode('::', $typeAndMethod);
if (! is_subclass_of($fqcn, $type)) {
continue;
}
if (! method_exists($fqcn, $method)) {
continue;
}
$phpcsFile->addError(
sprintf('Method “%s” is forbidden, use “%s” instead.', $typeAndMethod, $replacement),
$classPointer,
self::FORBIDDEN_METHOD_DECLARATION
);
}
}
}