forked from magento/magento-coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into ticket-162
- Loading branch information
Showing
8 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Sniffs\Methods; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
/** | ||
* Detects possible use of deprecated model methods. | ||
*/ | ||
class DeprecatedModelMethodSniff implements Sniff | ||
{ | ||
const RESOURCE_METHOD = "getResource"; | ||
|
||
/** | ||
* String representation of warning. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningMessage = "The use of the deprecated method 'getResource()' to '%s' the data detected."; | ||
|
||
/** | ||
* Warning violation code. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningCode = 'FoundDeprecatedModelMethod'; | ||
|
||
/** | ||
* List of deprecated method. | ||
* | ||
* @var array | ||
*/ | ||
protected $methods = [ | ||
'save', | ||
'load', | ||
'delete' | ||
]; | ||
|
||
protected $severity = 0; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_OBJECT_OPERATOR | ||
]; | ||
} | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr); | ||
$resourcePosition = $phpcsFile->findNext( | ||
T_STRING, | ||
$stackPtr + 1, | ||
$endOfStatement, | ||
false, | ||
self::RESOURCE_METHOD | ||
); | ||
if ($resourcePosition !== false) { | ||
$methodPosition = $phpcsFile->findNext([T_STRING, T_VARIABLE], $resourcePosition + 1, $endOfStatement); | ||
if ($methodPosition !== false && in_array($tokens[$methodPosition]['content'], $this->methods, true)) { | ||
$phpcsFile->addWarning( | ||
sprintf($this->warningMessage, $tokens[$methodPosition]['content']), | ||
$stackPtr, | ||
$this->warningCode | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$model->getResource()->save($model); | ||
|
||
$model->getResource()->load($model, $id); | ||
|
||
$model->getResource()->delete($model); | ||
|
||
$model->getResource()->myCustomMethod(); | ||
|
||
$model->myCustomMethod(); | ||
|
||
$model->anotherMethodWithResource()->save($model); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento2\Tests\Methods; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class DeprecatedModelMethodUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return [ | ||
3 => 1, | ||
5 => 1, | ||
7 => 1, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.