Skip to content

Commit 34c66bd

Browse files
Merge pull request #96 from magento-commerce/imported-eliseacornejo-magento-coding-standard-310
[Imported] AC-676: Create phpcs static check for ObsoleteConnectionTest
2 parents b8d2c96 + ec36aef commit 34c66bd

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class ObsoleteConnectionSniff implements Sniff
14+
{
15+
private const ERROR_CODE_METHOD = 'FoundObsoleteMethod';
16+
17+
/**
18+
* @var string[]
19+
*/
20+
private $obsoleteMethods = [
21+
'_getReadConnection',
22+
'_getWriteConnection',
23+
'_getReadAdapter',
24+
'_getWriteAdapter',
25+
'getReadConnection',
26+
'getWriteConnection',
27+
'getReadAdapter',
28+
'getWriteAdapter',
29+
];
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function register()
35+
{
36+
return [
37+
T_OBJECT_OPERATOR,
38+
T_FUNCTION
39+
];
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function process(File $phpcsFile, $stackPtr)
46+
{
47+
$this->validateObsoleteMethod($phpcsFile, $stackPtr);
48+
}
49+
50+
/**
51+
* Check if obsolete methods are used
52+
*
53+
* @param File $phpcsFile
54+
* @param int $stackPtr
55+
*/
56+
private function validateObsoleteMethod(File $phpcsFile, int $stackPtr)
57+
{
58+
$tokens = $phpcsFile->getTokens();
59+
$stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
60+
61+
foreach ($this->obsoleteMethods as $method) {
62+
if ($tokens[$stringPos]['content'] === $method) {
63+
$phpcsFile->addWarning(
64+
sprintf("Contains obsolete method: %s. Please use getConnection method instead.", $method),
65+
$stackPtr,
66+
self::ERROR_CODE_METHOD
67+
);
68+
}
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$this->_getReadConnection();
8+
9+
$connection = new Connection();
10+
return $connection->_getWriteConnection();
11+
12+
$this->getMethod(
13+
function($param){
14+
$param->_getWriteAdapter();
15+
}
16+
);
17+
18+
$writeAdapter = $this->getWriteAdapter();
19+
20+
protected function getConnection()
21+
{
22+
return $this->_resource->getReadConnection($this->connection);
23+
}
24+
25+
return $this->_getReadAdapter();
26+
27+
$this->getReadAdapterMyMehtod();
28+
29+
private function getReadAdapter()
30+
{
31+
32+
}
33+
34+
$getWriteAdapter = new WriteAdapter();
35+
36+
$getWriteAdapter = $this->getWriteAdapter();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class ObsoleteConnectionUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList()
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList()
24+
{
25+
return [
26+
7 => 1,
27+
10 => 1,
28+
14 => 1,
29+
18 => 1,
30+
22 => 1,
31+
25 => 1,
32+
29 => 1,
33+
36 => 1
34+
];
35+
}
36+
}

Magento2/ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@
313313
<severity>8</severity>
314314
<type>warning</type>
315315
</rule>
316+
<rule ref="Magento2.Legacy.ObsoleteConnection">
317+
<severity>8</severity>
318+
<type>warning</type>
319+
</rule>
316320

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

0 commit comments

Comments
 (0)