Skip to content

Commit d55b20b

Browse files
authored
Merge pull request #41 from magento/24-static-method-sniff
#24: [New Rule] Static methods SHOULD NOT be used
2 parents da890a4 + 59c0d4f commit d55b20b

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-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

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
<rule ref="Magento.Exceptions.Namespace">
7373
<severity>8</severity>
7474
</rule>
75+
<rule ref="Magento.Functions.StaticFunction">
76+
<severity>6</severity>
77+
</rule>
7578
<rule ref="Magento.Files.LineLength">
7679
<properties>
7780
<property name="lineLimit" value="120"/>

0 commit comments

Comments
 (0)