-
Notifications
You must be signed in to change notification settings - Fork 160
#26: Add Sniff for Getters not change state #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51fdf68
#26: Add Sniff for Gettersnot change state
9b34291
#26: Add Sniff for Gettersnot change state
230d2ad
#26: Add Sniff for Gettersnot change state
cc2b385
#26: Add Sniff for Getters not change state
d8663b2
Merge branch 'develop' into 26-getter-sniff
e470c0f
Improve impelementation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sniffs\Functions; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
/** | ||
* Detects getter that change the state of an object. | ||
*/ | ||
class GetterStateSniff implements Sniff | ||
{ | ||
|
||
/** | ||
* String representation of warning. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningMessage = 'Getters SHOULD NOT change the state of an object.'; | ||
|
||
/** | ||
* Warning violation code. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningCode = 'GetterState'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function register() | ||
{ | ||
return [T_FUNCTION]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$methodName = $phpcsFile->getDeclarationName($stackPtr); | ||
|
||
if ($methodName === null || strpos($methodName, 'get') === false) { | ||
// Ignore closures and no getters | ||
return; | ||
} | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
$open = $tokens[$stackPtr]['scope_opener']; | ||
$close = $tokens[$stackPtr]['scope_closer']; | ||
|
||
$isObjectScope = false; | ||
$isObjectScopeToken = [T_SELF => T_SELF, T_PARENT => T_PARENT, T_STATIC => T_STATIC]; | ||
|
||
for ($i = ($open + 1); $i < $close; $i++) { | ||
$token = $tokens[$i]; | ||
|
||
if (T_SEMICOLON === $token['code']) { | ||
// Detect line end scope change to function scope. | ||
$isObjectScope = false; | ||
} | ||
|
||
if (array_key_exists($token['code'], $isObjectScopeToken)) { | ||
$isObjectScope = true; | ||
} | ||
|
||
if ($token['content'] === '$this') { | ||
$isObjectScope = true; | ||
} | ||
|
||
if ($isObjectScope === true && array_key_exists($token['code'], Tokens::$assignmentTokens)) { | ||
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Foo\Bar; | ||
|
||
|
||
abstract class Bar{ | ||
|
||
public static $foobar = 100; | ||
} | ||
|
||
class Foo extends Bar | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private static $staticProperty; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $property; | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStaticProperty() | ||
{ | ||
self::$staticProperty = 12; | ||
static::$staticProperty -= 12; | ||
self::$staticProperty .= 12; | ||
return self::$staticProperty; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getProperty() | ||
{ | ||
$this->property = 1223; | ||
return $this->property; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function normalMethod() | ||
{ | ||
$localVariable = 12; | ||
return $localVariable; | ||
} | ||
} | ||
|
||
$d = function ($test) { | ||
$test = 123; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Tests\Functions; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
/** | ||
* Class GetterStateUnitTest | ||
*/ | ||
class GetterStateUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function getErrorList() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function getWarningList() | ||
{ | ||
return [ | ||
28 => 1, | ||
29 => 1, | ||
30 => 1, | ||
39 => 1, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.