Skip to content

AC-601: Create phpcs static check for AbstractBlockTest #218

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 19, 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
98 changes: 98 additions & 0 deletions Magento2/Sniffs/Legacy/AbstractBlockSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento. 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 AbstractBlockSniff implements Sniff
{
private const CHILD_HTML_METHOD = 'getChildHtml';
private const CHILD_CHILD_HTML_METHOD = 'getChildChildHtml';

/**
* Error violation code.
*
* @var string
*/
protected $errorCode = 'FoundCountOfParametersIncorrect';

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

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if (!isset($phpcsFile->getTokens()[$stackPtr + 1]['content'])) {
return;
}

$content = $phpcsFile->getTokens()[$stackPtr + 1]['content'];

if (!$this->isApplicable($content)) {
return;
}

$paramsCount = $this->getParametersCount($phpcsFile, $stackPtr + 1);
if ($content === self::CHILD_HTML_METHOD && $paramsCount >= 3) {
$phpcsFile->addError(
'3rd parameter is not needed anymore for getChildHtml()',
$stackPtr,
$this->errorCode
);
}
if ($content === self::CHILD_CHILD_HTML_METHOD && $paramsCount >= 4) {
$phpcsFile->addError(
'4th parameter is not needed anymore for getChildChildHtml()',
$stackPtr,
$this->errorCode
);
}
}

/**
* Return if it is applicable to do the check
*
* @param string $content
* @return bool
*/
private function isApplicable(string $content): bool
{
return in_array($content, [self::CHILD_HTML_METHOD, self::CHILD_CHILD_HTML_METHOD]);
}

/**
* Get the quantity of parameters on a method
*
* @param File $phpcsFile
* @param int $methodHtmlPosition
* @return int
*/
private function getParametersCount(File $phpcsFile, int $methodHtmlPosition): int
{
$closePosition = $phpcsFile->getTokens()[$methodHtmlPosition +1]['parenthesis_closer'];
$getTokenAsContent = $phpcsFile->getTokensAsString(
$methodHtmlPosition + 2,
($closePosition - $methodHtmlPosition) - 2
);
if ($getTokenAsContent) {
$parameters = explode(',', $getTokenAsContent);
return count($parameters);
}
return 0;
}
}
36 changes: 36 additions & 0 deletions Magento2/Tests/Legacy/AbstractBlockUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

return $this->getChildHtml(
function($param){
$param->something();
},
'aa'
);

$this->getChildHtml(
function($param){
$param->something();
},
'aa',
2
);

return $this->getChildHtml('aa', 'bb');

return $this->getChildHtml('aa', 'bb', 1, true);

$this->getChildHtml();

$this->testMethod()->getChildHtml('aa', 'bb', 'cc');

$this->getChildChildHtml('aa', true, 'cc');

$this->getChildChildHtml('aa', true, 'cc', 1);

$this->getChildChildHtml('aa' . 'bb', !(1 + 1) * (int) $this, 'cc', 1);

private function getChildHtml($aa, $bb, $cc = 'cc')
{
}

$this->getChilChilddHtml();
33 changes: 33 additions & 0 deletions Magento2/Tests/Legacy/AbstractBlockUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class AbstractBlockUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [
10 => 1,
20 => 1,
24 => 1,
28 => 1,
30 => 1
];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
<severity>10</severity>
<type>error</type>
</rule>
<rule ref="Magento2.Legacy.AbstractBlock">
<severity>10</severity>
<type>error</type>
</rule>

<!-- Severity 9 warnings: Possible security and issues that may cause bugs. -->
<rule ref="Generic.Files.ByteOrderMark">
Expand Down