Skip to content

Commit 1d380aa

Browse files
committed
Added sniff for deprecated model methods. #187
1 parent 283c9bb commit 1d380aa

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Sniffs\Methods;
7+
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Util\Tokens;
11+
12+
/**
13+
* Detects possible use of deprecated model methods.
14+
*/
15+
class DeprecatedModelMethodSniff implements Sniff
16+
{
17+
/**
18+
* String representation of warning.
19+
*
20+
* @var string
21+
*/
22+
protected $warningMessage = "Possible use of the deprecated model method 'getResource()'" .
23+
" to '%s' the data detected.";
24+
25+
/**
26+
* Warning violation code.
27+
*
28+
* @var string
29+
*/
30+
protected $warningCode = 'FoundDeprecatedModelMethod';
31+
32+
/**
33+
* List of deprecated method.
34+
*
35+
* @var array
36+
*/
37+
protected $methods = [
38+
'save',
39+
'load',
40+
'delete'
41+
];
42+
43+
protected $severity = 0;
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
public function register()
49+
{
50+
return [
51+
T_OBJECT_OPERATOR,
52+
T_DOUBLE_COLON
53+
];
54+
}
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function process(File $phpcsFile, $stackPtr)
59+
{
60+
$tokens = $phpcsFile->getTokens();
61+
$methodPosition = $phpcsFile->findNext(T_STRING, $stackPtr + 1);
62+
63+
if ($methodPosition !== false &&
64+
in_array($tokens[$methodPosition]['content'], $this->methods)
65+
) {
66+
$resourcePosition = $phpcsFile->findPrevious([T_STRING, T_VARIABLE], $stackPtr - 1);
67+
if ($resourcePosition !== false) {
68+
$methodName = $tokens[$resourcePosition]['content'];
69+
if ($methodName === "getResource") {
70+
$phpcsFile->addWarning(
71+
sprintf($this->warningMessage, $tokens[$methodPosition]['content']),
72+
$stackPtr,
73+
$this->warningCode
74+
);
75+
}
76+
}
77+
}
78+
}
79+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$model->getResource()->save();
3+
$model->getResource()->load();
4+
$model->getResource()->delete();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Methods;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class DeprecatedModelMethodUnitTest 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+
2 => 1,
27+
3 => 1,
28+
4 => 1
29+
];
30+
}
31+
}

Magento2/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@
215215
<severity>8</severity>
216216
<type>warning</type>
217217
</rule>
218+
<rule ref="Magento2.Methods.DeprecatedModelMethod">
219+
<severity>8</severity>
220+
<type>warning</type>
221+
</rule>
218222

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

0 commit comments

Comments
 (0)