Skip to content

Commit edca711

Browse files
authored
Merge pull request #189 from milindsingh/deprecated-methods
Added sniff for deprecated model methods. #187
2 parents 81660df + 902b134 commit edca711

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
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+
11+
/**
12+
* Detects possible use of deprecated model methods.
13+
*/
14+
class DeprecatedModelMethodSniff implements Sniff
15+
{
16+
const RESOURCE_METHOD = "getResource";
17+
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
protected $warningMessage = "The use of the deprecated method 'getResource()' 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+
];
53+
}
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function process(File $phpcsFile, $stackPtr)
58+
{
59+
$tokens = $phpcsFile->getTokens();
60+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);
61+
$resourcePosition = $phpcsFile->findNext(
62+
T_STRING,
63+
$stackPtr + 1,
64+
$endOfStatement,
65+
false,
66+
self::RESOURCE_METHOD
67+
);
68+
if ($resourcePosition !== false) {
69+
$methodPosition = $phpcsFile->findNext([T_STRING, T_VARIABLE], $resourcePosition + 1, $endOfStatement);
70+
if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods, true)) {
71+
$phpcsFile->addWarning(
72+
sprintf($this->warningMessage, $tokens[$methodPosition]['content']),
73+
$stackPtr,
74+
$this->warningCode
75+
);
76+
}
77+
}
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$model->getResource()->save($model);
4+
5+
$model->getResource()->load($model, $id);
6+
7+
$model->getResource()->delete($model);
8+
9+
$model->getResource()->myCustomMethod();
10+
11+
$model->myCustomMethod();
12+
13+
$model->anotherMethodWithResource()->save($model);
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+
3 => 1,
27+
5 => 1,
28+
7 => 1,
29+
];
30+
}
31+
}

Magento2/ruleset.xml

+4
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)