From eda77b3d5d1ed780560c295ef280003cff6aa261 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Thu, 7 Oct 2021 10:53:49 +0200 Subject: [PATCH 1/4] AC-676: Create phpcs static check for ObsoleteConnectionTest --- .../Sniffs/Legacy/ObsoleteConnectionSniff.php | 71 +++++++++++++++++++ .../Legacy/ObsoleteConnectionUnitTest.inc | 36 ++++++++++ .../Legacy/ObsoleteConnectionUnitTest.php | 36 ++++++++++ 3 files changed, 143 insertions(+) create mode 100644 Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php create mode 100644 Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.inc create mode 100644 Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php diff --git a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php new file mode 100644 index 00000000..ac48af93 --- /dev/null +++ b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php @@ -0,0 +1,71 @@ +validateObsoleteMethod($phpcsFile, $stackPtr); + } + + /** + * Check if obsolete methods are used + * + * @param $phpcsFile + * @param $stackPtr + */ + private function validateObsoleteMethod($phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); + + foreach ($this->obsoleteMethods as $method) { + if ($tokens[$stringPos]['content'] === $method) { + $phpcsFile->addWarning( + sprintf("Contains obsolete method: %s.", $method), + $stackPtr, + self::ERROR_CODE_METHOD + ); + } + } + } +} diff --git a/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.inc b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.inc new file mode 100644 index 00000000..70c71642 --- /dev/null +++ b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.inc @@ -0,0 +1,36 @@ +_getReadConnection(); + +$connection = new Connection(); +return $connection->_getWriteConnection(); + +$this->getMethod( + function($param){ + $param->_getWriteAdapter(); + } +); + +$writeAdapter = $this->getWriteAdapter(); + +protected function getConnection() +{ + return $this->_resource->getReadConnection($this->connection); +} + +return $this->_getReadAdapter(); + +$this->getReadAdapterMyMehtod(); + +private function getReadAdapter() +{ + +} + +$getWriteAdapter = new WriteAdapter(); + +$getWriteAdapter = $this->getWriteAdapter(); diff --git a/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php new file mode 100644 index 00000000..cb0afe53 --- /dev/null +++ b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php @@ -0,0 +1,36 @@ + 1, + 6 => 1, + 10 => 1, + 14 => 1, + 18 => 1, + 21 => 1, + 25 => 1, + 32 => 1 + ]; + } +} From 5a34fd1e0a4f02094e54d6767a5f46f705b002a9 Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Thu, 7 Oct 2021 11:00:57 +0200 Subject: [PATCH 2/4] AC-676: Create phpcs static check for ObsoleteConnectionTest --- Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php | 8 ++++---- Magento2/ruleset.xml | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php index cb0afe53..a451d2cf 100644 --- a/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php +++ b/Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php @@ -23,14 +23,14 @@ public function getErrorList() public function getWarningList() { return [ - 3 => 1, - 6 => 1, + 7 => 1, 10 => 1, 14 => 1, 18 => 1, - 21 => 1, + 22 => 1, 25 => 1, - 32 => 1 + 29 => 1, + 36 => 1 ]; } } diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 1b1c7f1b..c9d22463 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -313,6 +313,10 @@ 8 warning + + 8 + warning + From 0bea0bc0f3017fd0b9b6af3bc8f840b7cb946cbf Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Thu, 7 Oct 2021 11:06:29 +0200 Subject: [PATCH 3/4] AC-676: Create phpcs static check for ObsoleteConnectionTest --- Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php index ac48af93..63949a38 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php @@ -15,7 +15,7 @@ class ObsoleteConnectionSniff implements Sniff private const ERROR_CODE_METHOD = 'FoundObsoleteMethod'; /** - * @var string[] + * @var string[] */ private $obsoleteMethods = [ '_getReadConnection', @@ -49,11 +49,11 @@ public function process(File $phpcsFile, $stackPtr) /** * Check if obsolete methods are used - * - * @param $phpcsFile - * @param $stackPtr + * + * @param File $phpcsFile + * @param int $stackPtr */ - private function validateObsoleteMethod($phpcsFile, $stackPtr) + private function validateObsoleteMethod(File $phpcsFile, int $stackPtr) { $tokens = $phpcsFile->getTokens(); $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); From ec36aef508e2910f8d4866e932fc7cea98ccf8ae Mon Sep 17 00:00:00 2001 From: Elisea Cornejo Date: Wed, 13 Oct 2021 11:51:00 +0200 Subject: [PATCH 4/4] AC-676: Create phpcs static check for ObsoleteConnectionTest --- Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php index 63949a38..a1425cab 100644 --- a/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php +++ b/Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php @@ -61,7 +61,7 @@ private function validateObsoleteMethod(File $phpcsFile, int $stackPtr) foreach ($this->obsoleteMethods as $method) { if ($tokens[$stringPos]['content'] === $method) { $phpcsFile->addWarning( - sprintf("Contains obsolete method: %s.", $method), + sprintf("Contains obsolete method: %s. Please use getConnection method instead.", $method), $stackPtr, self::ERROR_CODE_METHOD );