Skip to content

Commit 51fdf68

Browse files
author
Lars Roettig
committed
#26: Add Sniff for Gettersnot change state
1 parent 2e5e855 commit 51fdf68

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
use PHP_CodeSniffer\Util\Tokens;
12+
13+
/**
14+
* Detects getter that change the state of an object.
15+
*/
16+
class GetterStateSniff implements Sniff
17+
{
18+
19+
/**
20+
* String representation of warning.
21+
*
22+
* @var string
23+
*/
24+
protected $warningMessage = 'Getters SHOULD NOT change the state of an object.';
25+
26+
/**
27+
* Warning violation code.
28+
*
29+
* @var string
30+
*/
31+
protected $warningCode = 'GetterState';
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function register()
37+
{
38+
return [T_FUNCTION];
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function process(File $phpcsFile, $stackPtr)
45+
{
46+
$methodName = $phpcsFile->getDeclarationName($stackPtr);
47+
48+
if ($methodName === null || strpos($methodName, 'get') === false) {
49+
// Ignore closures and no getters
50+
return;
51+
}
52+
53+
$tokens = $phpcsFile->getTokens();
54+
$open = $tokens[$stackPtr]['scope_opener'];
55+
$close = $tokens[$stackPtr]['scope_closer'];
56+
57+
$isObjectScope = false;
58+
$isObjectScopeToken = [T_SELF => T_SELF, T_PARENT => T_PARENT, T_STATIC => T_STATIC];
59+
60+
for ($i = ($open + 1); $i < $close; $i++) {
61+
$token = $tokens[$i];
62+
63+
if (T_SEMICOLON === $token['code']) {
64+
// Detect line end scope change to function scope.
65+
$isObjectScope = false;
66+
}
67+
68+
if (array_key_exists($token['code'], $isObjectScopeToken)) {
69+
$isObjectScope = true;
70+
}
71+
72+
if ($token['content'] === '$this') {
73+
$isObjectScope = true;
74+
}
75+
76+
if ($isObjectScope === true && array_key_exists($token['code'], Tokens::$assignmentTokens)) {
77+
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
78+
}
79+
}
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Foo\Bar;
4+
5+
6+
abstract class Bar{
7+
8+
public static $foobar = 100;
9+
}
10+
11+
class Foo extends Bar
12+
{
13+
/**
14+
* @var int
15+
*/
16+
private static $staticProperty;
17+
18+
/**
19+
* @var int
20+
*/
21+
private $property;
22+
23+
/**
24+
* @return int
25+
*/
26+
public function getStaticProperty()
27+
{
28+
self::$staticProperty = 12;
29+
static::$staticProperty -= 12;
30+
self::$staticProperty .= 12;
31+
return self::$staticProperty;
32+
}
33+
34+
/**
35+
* @return int
36+
*/
37+
public function getProperty()
38+
{
39+
$this->property = 1223;
40+
return $this->property;
41+
}
42+
43+
/**
44+
* @return int
45+
*/
46+
public function normalMethod()
47+
{
48+
$localVariable = 12;
49+
return $localVariable;
50+
}
51+
}
52+
53+
$d = function ($test) {
54+
$test = 123;
55+
};
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 GetterStateUnitTest
13+
*/
14+
class GetterStateUnitTest 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+
28 => 1,
31+
29 => 1,
32+
30 => 1,
33+
39 => 1,
34+
];
35+
}
36+
}

phpunit.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@
88
<php>
99
<env name="PHPCS_IGNORE_TESTS" value="Generic,MySource,PEAR,PSR1,PSR2,Squiz,Zend"/>
1010
</php>
11+
<filter>
12+
<whitelist addUncoveredFilesFromWhitelist="false">
13+
<directory suffix=".php">Magento/Sniffs</directory>
14+
</whitelist>
15+
</filter>
1116
</phpunit>

0 commit comments

Comments
 (0)