Skip to content

AC-678: Create phpcs sniff for ObsoleteResponseTest #311

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 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteResponseSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class ObsoleteResponseSniff implements Sniff
{
private const WARNING_CODE_METHOD = 'FoundObsoleteResponseMethod';

/**
* @var string[]
*/
private $obsoleteResponseMethods = [
'loadLayout',
'renderLayout',
'_redirect',
'_forward',
'_setActiveMenu',
'_addBreadcrumb',
'_addContent',
'_addLeft',
'_addJs',
'_moveBlockToContainer',
];

/**
* @inheritdoc
*/
public function register()
{
return [
T_OBJECT_OPERATOR,
T_FUNCTION
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1);

foreach ($this->obsoleteResponseMethods as $method) {
if ($tokens[$stringPos]['content'] === $method) {
$phpcsFile->addWarning(
sprintf('Contains obsolete response method: %s.', $method),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be not clear how these static tests failures should be addressed. Can we add a bit more information and replacement suggestion to the message:

I.e. "\Magento\Framework\App\ViewInterface::renderLayout method is deprecated. Please use \Magento\Framework\Controller\ResultInterface::renderResult instead."
...
"\Magento\Framework\App\Action\Action::_redirect" method is deprecated. Please use \Magento\Backend\Model\View\Result\Redirect instead.
...
"\Magento\Backend\App\AbstractAction::_addBreadcrumb method is deprecated. Please use \Magento\Backend\Model\View\Result\Page::addBreadcrumb instead."
and so on

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be implemented by having $obsoleteResponseMethods as associative array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please @sivaschenko review! thanks

$stackPtr,
self::WARNING_CODE_METHOD
);
}
}
}
}
39 changes: 39 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteResponseUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$this->_view->loadLayout(['default', 'test'], true, true, false);
$this->_view->renderLayout();

protected function _addBreadcrumb($label, $title = null, $link = null)
{
$this->getLayout()->getBlock('test')->addLink($label, $title, $link);
}

$this->editPost = $objectManagerHelper->getObject(
TestClass::class,
[
'_redirect' => $this->redirect,
]
);

$this->_redirect('test/path');

$this->_forward('grid');

$this->_initAction()->_setActiveMenu(
'Magento_Invitation::report_magento_invitation_order'
)->_addBreadcrumb(
__('Invitation Report by Customers'),
__('Invitation Report by Order Conversion Rate')
)->_addLeft(
)->_addJs(
$this->_view->getLayout()->createBlock(TestBlock::class)->setTemplate('Test::test.phtml')
);

private function _addContent(AbstractBlock $block)
{
return $this->_moveBlockToContainer($block, 'content');
}
40 changes: 40 additions & 0 deletions Magento2/Tests/Legacy/ObsoleteResponseUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ObsoleteResponseUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList($testFile = '')
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
return [
7 => 1,
8 => 1,
10 => 1,
22 => 1,
24 => 1,
26 => 1,
28 => 1,
31 => 1,
32 => 1,
36 => 1,
38 => 1
];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.ObsoleteResponse">
<severity>8</severity>
<type>warning</type>
</rule>

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