Skip to content

Commit

Permalink
Merge branch 'develop' into ticket-162
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei authored Sep 2, 2020
2 parents 76f5f2d + ea0eb81 commit 5b82e31
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ php:
- 7.1
- 7.2
- 7.3
install: composer install --no-interaction --prefer-source
- 7.4
cache:
directories:
- $HOME/.composer/cache/files
install: composer install --no-interaction --prefer-dist
script:
- vendor/bin/phpunit
- vendor/bin/phpcs --standard=Magento2 Magento2/ --extensions=php
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
);
}
}
}
}
2 changes: 1 addition & 1 deletion Magento2/Sniffs/Security/InsecureFunctionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InsecureFunctionSniff extends ForbiddenFunctionsSniff
public $forbiddenFunctions = [
'assert' => null,
'create_function' => null,
'exec' => null,
'exec' => '\Magento\Framework\Shell::execute',
'md5' => 'improved hash functions (SHA-256, SHA-512 etc.)',
'passthru' => null,
'pcntl_exec' => null,
Expand Down
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"version": "5",
"require": {
"php": ">=5.6.0",
"squizlabs/php_codesniffer": "^3.4",
"squizlabs/php_codesniffer": "^3.5",
"webonyx/graphql-php": ">=0.12.6 <1.0"
},
"require-dev": {
Expand Down
16 changes: 9 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b82e31

Please sign in to comment.