Skip to content

Commit 5fa9179

Browse files
author
Lars Roettig
committed
#24: [New Rule] Static methods SHOULD NOT be used
1 parent da890a4 commit 5fa9179

File tree

5 files changed

+480
-264
lines changed

5 files changed

+480
-264
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// phpcs:ignore Magento.Files.LineLength.MaxExceeded
23+
protected $warningMessage = 'The use of `static` methods is discouraged';
24+
25+
/**
26+
* Warning violation code.
27+
*
28+
* @var string
29+
*/
30+
protected $warningCode = 'StaticFunction';
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function register()
36+
{
37+
return [T_STATIC];
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function process(File $phpcsFile, $stackPtr)
44+
{
45+
$posOfFunction = $phpcsFile->findNext(T_FUNCTION, $stackPtr) + 1;
46+
$tokens = array_slice($phpcsFile->getTokens(), $stackPtr, $posOfFunction - $stackPtr);
47+
48+
$allowedTypes = ['T_STATIC' => '', 'T_WHITESPACE' => '', 'T_FUNCTION' => ''];
49+
50+
foreach ($tokens as $token) {
51+
$type = $token['type'];
52+
if (!array_key_exists($type, $allowedTypes)) {
53+
break;
54+
}
55+
56+
if ($type === 'T_FUNCTION') {
57+
$phpcsFile->addWarning($this->warningMessage, $posOfFunction, $this->warningCode);
58+
}
59+
}
60+
}
61+
}
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,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tests\Functions;
8+
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
11+
/**
12+
* Class StaticFunctionSniffTest
13+
*/
14+
class StaticFunctionUnitTest extends AbstractSniffUnitTest
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
protected function getErrorList()
20+
{
21+
return [];
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function getWarningList()
28+
{
29+
return [
30+
8 => 1,
31+
17 => 1
32+
];
33+
}
34+
}

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)