-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SetDefinitionCommandSniff.php
51 lines (40 loc) · 1.39 KB
/
SetDefinitionCommandSniff.php
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
50
51
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\EasyCodingStandard\Sniffs;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
final class SetDefinitionCommandSniff implements Sniff
{
private bool $isConfigure = false;
public function register(): array
{
return [T_CLASS, T_FUNCTION, T_OBJECT_OPERATOR];
}
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
switch (true) {
case T_CLASS === $tokens[$stackPtr]['code']:
if (!str_ends_with((string) $tokens[$stackPtr + 2]['content'], 'Command')) {
return \count($tokens) + 1;
}
break;
case T_FUNCTION === $tokens[$stackPtr]['code']:
$this->isConfigure = 'configure' === $tokens[$stackPtr + 2]['content'];
break;
case T_OBJECT_OPERATOR === $tokens[$stackPtr]['code']:
if ($this->isConfigure && 'setDefinition' === $tokens[$stackPtr + 1]['content']) {
$phpcsFile->addError('Do not use the setDefinition() method to configure commands. Use addArgument() and addOption() instead.', $stackPtr, self::class);
}
break;
}
return \count($tokens) + 1;
}
}