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 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
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' => 'Please use \Magento\Framework\View\Layout\Builder::build instead.',
'renderLayout' => 'Please use \Magento\Framework\Controller\ResultInterface::renderResult instead.',
'_redirect' => 'Please use \Magento\Backend\Model\View\Result\Redirect::render instead.',
'_forward' => 'Please use \Magento\Backend\Model\View\Result\Forward::forward instead.',
'_setActiveMenu' => 'Please use \Magento\Backend\Model\View\Result\Page::setActiveMenu instead.',
'_addBreadcrumb' => 'Please use \Magento\Backend\Model\View\Result\Page::addBreadcrumb instead.',
'_addContent' => 'Please use \Magento\Backend\Model\View\Result\Page::addContent instead.',
'_addLeft' => 'Please use \Magento\Backend\Model\View\Result\Page::addLeft instead.',
'_addJs' => 'Please use \Magento\Backend\Model\View\Result\Page::addJs instead.',
'_moveBlockToContainer' => 'Please use \Magento\Backend\Model\View\Result\Page::moveBlockToContainer instead.',
];

/**
* @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 => $errorMessage) {
if ($tokens[$stringPos]['content'] === $method) {
$phpcsFile->addWarning(
sprintf('%s method is deprecated. %s', $method, $errorMessage),
$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 @@ -322,6 +322,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