Skip to content

AC-676: Create phpcs static check for ObsoleteConnectionTest #310

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

Merged
merged 5 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteConnectionSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class ObsoleteConnectionSniff implements Sniff
{
private const ERROR_CODE_METHOD = 'FoundObsoleteMethod';

/**
* @var string[]
*/
private $obsoleteMethods = [
'_getReadConnection',
'_getWriteConnection',
'_getReadAdapter',
'_getWriteAdapter',
'getReadConnection',
'getWriteConnection',
'getReadAdapter',
'getWriteAdapter',
];

/**
* @inheritdoc
*/
public function register()
{
return [
T_OBJECT_OPERATOR,
T_FUNCTION
];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$this->validateObsoleteMethod($phpcsFile, $stackPtr);
}

/**
* Check if obsolete methods are used
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function validateObsoleteMethod(File $phpcsFile, int $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. Please use getConnection method instead.", $method),
$stackPtr,
self::ERROR_CODE_METHOD
);
}
}
}
}
36 changes: 36 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$this->_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();
36 changes: 36 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteConnectionUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ObsoleteConnectionUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [
7 => 1,
10 => 1,
14 => 1,
18 => 1,
22 => 1,
25 => 1,
29 => 1,
36 => 1
];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.ObsoleteConnection">
<severity>8</severity>
<type>warning</type>
</rule>

<!-- Severity 7 warnings: General code issues. -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
Expand Down