Skip to content

Commit 9af88d2

Browse files
committed
REPO-70: [EQP][Sniffs Consolidation] Assign severity to each sniff
- review fixes
2 parents 9f42f25 + d55b20b commit 9af88d2

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sniffs\Functions;
8+
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Files\File;
11+
12+
/**
13+
* Detects static function definitions.
14+
*/
15+
class StaticFunctionSniff implements Sniff
16+
{
17+
/**
18+
* String representation of warning.
19+
*
20+
* @var string
21+
*/
22+
protected $warningMessage = 'Static method cannot be intercepted and its use is discouraged.';
23+
24+
/**
25+
* Warning violation code.
26+
*
27+
* @var string
28+
*/
29+
protected $warningCode = 'StaticFunction';
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function register()
35+
{
36+
return [T_STATIC];
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function process(File $phpcsFile, $stackPtr)
43+
{
44+
$posOfFunction = $phpcsFile->findNext(T_FUNCTION, $stackPtr) + 1;
45+
$tokens = array_slice($phpcsFile->getTokens(), $stackPtr, $posOfFunction - $stackPtr);
46+
47+
$allowedTypes = [T_STATIC => true, T_WHITESPACE => true, T_FUNCTION => true];
48+
foreach ($tokens as $token) {
49+
$code = $token['code'];
50+
if (!array_key_exists($code, $allowedTypes)) {
51+
break;
52+
}
53+
54+
if ($code === T_FUNCTION) {
55+
$phpcsFile->addWarning($this->warningMessage, $posOfFunction, $this->warningCode);
56+
}
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Foo\Bar;
4+
5+
interface FooInterface
6+
{
7+
public static
8+
function aStaticMethod();
9+
10+
public function normalMethod();
11+
}
12+
13+
class Foo implements FooInterface
14+
{
15+
private static $property;
16+
17+
public static function aStaticMethod()
18+
{
19+
return self::$property;
20+
}
21+
22+
public function normalMethod()
23+
{
24+
return $this;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tests\Functions;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class StaticFunctionSniffTest
12+
*/
13+
class StaticFunctionUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
protected function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function getWarningList()
27+
{
28+
return [
29+
8 => 1,
30+
17 => 1
31+
];
32+
}
33+
}

Magento/ruleset.xml

+7
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<severity>8</severity>
118118
<type>warning</type>
119119
</rule>
120+
<rule ref="Magento.Functions.StaticFunction">
121+
<severity>6</severity>
122+
</rule>
120123
<rule ref="Magento.Files.LineLength">
121124
<properties>
122125
<property name="lineLimit" value="120"/>
@@ -398,6 +401,10 @@
398401
<severity>6</severity>
399402
<type>warning</type>
400403
</rule>
404+
<rule ref="Squiz.Operators.ValidLogicalOperators">
405+
<severity>6</severity>
406+
<type>warning</type>
407+
</rule>
401408
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace">
402409
<severity>6</severity>
403410
<type>warning</type>

0 commit comments

Comments
 (0)