diff --git a/Magento/Sniffs/Functions/GetterStateSniff.php b/Magento/Sniffs/Functions/GetterStateSniff.php new file mode 100644 index 00000000..8f186daa --- /dev/null +++ b/Magento/Sniffs/Functions/GetterStateSniff.php @@ -0,0 +1,103 @@ +getDeclarationName($stackPtr); + + if ($methodName === null || strpos($methodName, 'get') !== 0) { + // Ignore closures and no getters + return; + } + + $tokens = $phpcsFile->getTokens(); + if (!isset($tokens[$stackPtr]['scope_closer'])) { + // Probably an interface method no check + return; + } + + $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]; + $thisScopeCloser = array_merge(Tokens::$bracketTokens, + [T_SEMICOLON => T_SEMICOLON, T_COMMA => T_COMMA, T_COLON => T_COLON]); + + for ($i = ($open + 1); $i < $close; $i++) { + $token = $tokens[$i]; + $code = $token['code']; + + if (array_key_exists($code, $thisScopeCloser)) { + // Detect line end scope change to function scope. + $isObjectScope = false; + } + + if (array_key_exists($code, $isObjectScopeToken)) { + $isObjectScope = true; + } + + if ($token['content'] === '$this') { + $isObjectScope = true; + } + + $isRelevant = $isObjectScope === true && $code !== T_DOUBLE_ARROW; + + if ($isRelevant && array_key_exists($code, Tokens::$assignmentTokens)) { + + $isWrappedByIf = false; + // Detect if the property warped by an if tag. + $ifTag = $phpcsFile->findPrevious(T_IF, $i); + if ($ifTag !== false) { + $open = $tokens[$ifTag]['scope_opener']; + $close = $tokens[$ifTag]['scope_closer']; + $isWrappedByIf = $open <= $i && $close >= $i; + } + + if ($isWrappedByIf === false) { + $phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode); + } + } + } + } +} \ No newline at end of file diff --git a/Magento/Tests/Functions/GetterStateUnitTest.inc b/Magento/Tests/Functions/GetterStateUnitTest.inc new file mode 100644 index 00000000..b83b579b --- /dev/null +++ b/Magento/Tests/Functions/GetterStateUnitTest.inc @@ -0,0 +1,138 @@ +property = 1223; + return $this->property; + } + + /** + * @return int + */ + public function getPropertyCached() + { + if ($this->property === null) { + $this->property = 1223; + } + + return $this->property; + } + + public function getPropertyLocal() + { + $local = $this->property; + $localArray = [ + 'payment' => [ + 'test' => [ + 'isActive' => $this->config->isActive(), + 'title' => $this->config->getTitle() + ] + ] + ]; + return $this->property; + } + + private function getSalesChannelForOrder($order) + { + $websiteId = (int)$order->getStore()->getWebsiteId(); + $websiteCode = $this->websiteRepository->getById($websiteId)->getCode(); + + return $this->salesChannelFactory->create([ + 'data' => [ + 'type' => '', + 'code' => $websiteCode + ] + ]); + } + + const MODE_AUTO = 0; + + const MODE_MANUAL = 1; + + public function getOptionsArray() + { + return [ + self::MODE_AUTO => __('Automatically'), + self::MODE_MANUAL => __('Manually') + ]; + } + + public function testigetFoo() + { + $this->property = 1223; + return $this->property; + } + + /** + * @return int + */ + public function normalMethod() + { + $localVariable = 12; + return $localVariable; + } + + public function getStorageModel($storage = null, $params = []) + { + if ($storage === null) { + $storage = $this->_coreFileStorage->getCurrentStorageCode(); + } + + switch ($storage) { + case self::STORAGE_MEDIA_FILE_SYSTEM: + $model = $this->_fileFactory->create(); + break; + default: + return false; + } + + if (isset($params['init']) && $params['init']) { + $model->init(); + } + + return $model; + } +} + +$d = function ($test) { + $test = 123; +}; + diff --git a/Magento/Tests/Functions/GetterStateUnitTest.php b/Magento/Tests/Functions/GetterStateUnitTest.php new file mode 100644 index 00000000..8f4b7843 --- /dev/null +++ b/Magento/Tests/Functions/GetterStateUnitTest.php @@ -0,0 +1,36 @@ + 1, + 29 => 1, + 30 => 1, + 43 => 1, + ]; + } +} diff --git a/Magento/ruleset.xml b/Magento/ruleset.xml index 6c659ec2..c6f249ac 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -121,6 +121,10 @@ 8 warning + + 8 + warning + 8 warning diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6d1afb07..b55ba133 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,4 +8,9 @@ + + + Magento/Sniffs + +