Skip to content

Added sniff for deprecated model methods. #187 #189

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 6 commits into from
Aug 17, 2020
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
79 changes: 79 additions & 0 deletions Magento2/Sniffs/Methods/DeprecatedModelMethodSniff.php
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
);
}
}
}
}
13 changes: 13 additions & 0 deletions Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.inc
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);
31 changes: 31 additions & 0 deletions Magento2/Tests/Methods/DeprecatedModelMethodUnitTest.php
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,
];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Methods.DeprecatedModelMethod">
<severity>8</severity>
<type>warning</type>
</rule>

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