-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathAbstractBlockSniff.php
94 lines (83 loc) · 2.62 KB
/
AbstractBlockSniff.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento2\Sniffs\Legacy;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class AbstractBlockSniff implements Sniff
{
private const CHILD_HTML_METHOD = 'getChildHtml';
private const CHILD_CHILD_HTML_METHOD = 'getChildChildHtml';
private const ERROR_CODE_THIRD_PARAMETER = 'ThirdParameterNotNeeded';
private const ERROR_CODE_FOURTH_PARAMETER = 'FourthParameterNotNeeded';
/**
* @inheritdoc
*/
public function register(): array
{
return [
T_OBJECT_OPERATOR
];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if (!isset($phpcsFile->getTokens()[$stackPtr + 1]['content'])) {
return;
}
$content = $phpcsFile->getTokens()[$stackPtr + 1]['content'];
if (!$this->isApplicable($content)) {
return;
}
$paramsCount = $this->getParametersCount($phpcsFile, $stackPtr + 1);
if ($content === self::CHILD_HTML_METHOD && $paramsCount >= 3) {
$phpcsFile->addError(
'3rd parameter is not needed anymore for getChildHtml()',
$stackPtr,
self::ERROR_CODE_THIRD_PARAMETER
);
}
if ($content === self::CHILD_CHILD_HTML_METHOD && $paramsCount >= 4) {
$phpcsFile->addError(
'4th parameter is not needed anymore for getChildChildHtml()',
$stackPtr,
self::ERROR_CODE_FOURTH_PARAMETER
);
}
}
/**
* Return if it is applicable to do the check
*
* @param string $content
* @return bool
*/
private function isApplicable(string $content): bool
{
return in_array($content, [self::CHILD_HTML_METHOD, self::CHILD_CHILD_HTML_METHOD]);
}
/**
* Get the quantity of parameters on a method
*
* @param File $phpcsFile
* @param int $methodHtmlPosition
* @return int
*/
private function getParametersCount(File $phpcsFile, int $methodHtmlPosition): int
{
$closePosition = $phpcsFile->getTokens()[$methodHtmlPosition +1]['parenthesis_closer'];
$getTokenAsContent = $phpcsFile->getTokensAsString(
$methodHtmlPosition + 2,
($closePosition - $methodHtmlPosition) - 2
);
if ($getTokenAsContent) {
$parameters = explode(',', $getTokenAsContent);
return count($parameters);
}
return 0;
}
}