Skip to content
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

TE-10562 Added rule to check deprecated usage of decodeJson() #114

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
composer.phar
composer.lock
phpunit.phar
.phpunit.result.cache

tests/Common/_support/
tests/Zed/_support/
Expand Down
116 changes: 116 additions & 0 deletions src/Common/Method/DeprecatedObjectUsageJsonDecodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace ArchitectureSniffer\Common\Method;

use ArchitectureSniffer\Common\DeprecationTrait;
use PHPMD\AbstractNode;
use PHPMD\AbstractRule;
use PHPMD\Node\ASTNode;
use PHPMD\Node\ClassNode;
use PHPMD\Node\MethodNode;
use PHPMD\Rule\ClassAware;

class DeprecatedObjectUsageJsonDecodeRule extends AbstractRule implements ClassAware
{
use DeprecationTrait;

/**
* @var string
*/
protected const METHOD_NAME = 'decodeJson';

/**
* @var string
*/
protected const CORRECT_VALUE = 'true';

/**
* @var string
*/
protected const RULE_DEPRECATED_USAGE_FOUND = 'Param #2 `$assoc` of `decodeJson` must be `true` as return of type `object` is not accepted. Method: %s.';
dereuromark marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param \PHPMD\AbstractNode $node
*
* @return void
*/
public function apply(AbstractNode $node)
{
if (
!$node instanceof ClassNode
|| $this->isClassDeprecated($node)
) {
return;
}

foreach ($node->getMethods() as $methodNode) {
$this->verifyMethod($methodNode);
}
}

/**
* @param \PHPMD\Node\MethodNode $methodNode
*
* @return void
*/
protected function verifyMethod(MethodNode $methodNode): void
{
if ($this->isMethodDeprecated($methodNode)) {
return;
}

$childNodes = $methodNode->findChildrenOfType('MethodPostfix');

foreach ($childNodes as $childNode) {
if ($childNode->getName() !== static::METHOD_NAME) {
continue;
}

$this->verifyUsages($childNode, $methodNode);
}
}

/**
* @param \PHPMD\Node\ASTNode $node
* @param \PHPMD\Node\MethodNode $methodNode
*
* @return void
*/
protected function verifyUsages(ASTNode $node, MethodNode $methodNode): void
{
$arguments = $node->getFirstChildOfType('Arguments')->findChildrenOfType('Literal');
if ($arguments === []) {
$this->addRuleViolation($methodNode);

return;
}

foreach ($arguments as $argument) {
if ($argument->getName() == static::CORRECT_VALUE) {
continue;
}

$this->addRuleViolation($methodNode);
}
}

/**
* @param \PHPMD\Node\MethodNode $methodNode
*
* @return void
*/
protected function addRuleViolation(MethodNode $methodNode): void
{
$message = sprintf(
static::RULE_DEPRECATED_USAGE_FOUND,
$methodNode->getFullQualifiedName(),
);

$this->addViolation($methodNode, [$message]);
}
}
8 changes: 8 additions & 0 deletions src/Common/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
<priority>1</priority>
</rule>

<rule
name="DeprecatedObjectUsageJsonDecodeRule"
message="{0}"
class="ArchitectureSniffer\Common\Method\DeprecatedObjectUsageJsonDecodeRule">

<priority>1</priority>
</rule>

<!-- COMMON FACTORY RULES -->
<rule
name="FactoryGetContainNoNewRule"
Expand Down
34 changes: 34 additions & 0 deletions tests/Common/Method/DeprecatedObjectUsageJsonDecodeRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace ArchitectureSnifferTest\Common\Method;

use ArchitectureSniffer\Common\Method\DeprecatedObjectUsageJsonDecodeRule;
use ArchitectureSnifferTest\AbstractArchitectureSnifferRuleTest;

class DeprecatedObjectUsageJsonDecodeRuleTest extends AbstractArchitectureSnifferRuleTest
{
/**
* @return void
*/
public function testRuleDoesNotApplyWhenUsageCorrect(): void
{
$bridgePathRule = new DeprecatedObjectUsageJsonDecodeRule();
$bridgePathRule->setReport($this->getReportMock(0));
$bridgePathRule->apply($this->getClassNode());
}

/**
* @return void
*/
public function testRuleApplyWhenUsageIncorrect(): void
{
$bridgePathRule = new DeprecatedObjectUsageJsonDecodeRule();
$bridgePathRule->setReport($this->getReportMock(2));
$bridgePathRule->apply($this->getClassNode());
}
}
2 changes: 2 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
require_once '_data/Common/Bridge/BridgeMethodsInterfaceTest/testRuleAppliesWhenBridgeInterfaceMethodsAreNotCorrect.php';
require_once '_data/Common/Bridge/BridgeMethodsInterfaceTest/testRuleDoesNotApplyWhenBridgeInterfaceMethodsAreCorrect.php';
require_once '_data/Common/Factory/FactoryCreateContainOneNewRuleTest/testRuleAppliesWhenCreateContainsWrongWayOfQueryCreation.php';
require_once '_data/Common/Method/DeprecatedObjectUsageJsonDecodeRuleTest/testRuleApplyWhenUsageIncorrect.php';
require_once '_data/Common/Method/DeprecatedObjectUsageJsonDecodeRuleTest/testRuleDoesNotApplyWhenUsageCorrect.php';
require_once '_data/Common/Method/ExternalMethodExtensionReturnTypeRuleTest/testRuleAppliesWhenClassExtendsExternalAndDoesNotHaveReturnType.php';
require_once '_data/Common/Method/ExternalMethodExtensionReturnTypeRuleTest/testRuleAppliesWhenClassImplementsExternalAndDoesNotHaveReturnType.php';
require_once '_data/Common/Method/ExternalMethodExtensionReturnTypeRuleTest/testRuleDoesNotApplyWhenClassExtendInternalAndDoesNotHaveReturnType.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace ArchitectureSnifferTest\Common\DeprecatedObjectUsageJsonDecodeRuleTest;

class Foo
{
protected $utilEncodingService;

/**
* @return bool
*/
public function testOne(): bool
{
return $this->utilEncodingService->decodeJson($value);
}

/**
* @return bool
*/
public function testTwo(): bool
{
return $this->utilEncodingService->decodeJson($value, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace ArchitectureSnifferTest\Common\DeprecatedObjectUsageJsonDecodeRuleTest;

class Bar
{
protected $utilEncodingService;

/**
* @return bool
*/
public function test(): bool
{
return $this->utilEncodingService->decodeJson($value, true);
}
}