-
Notifications
You must be signed in to change notification settings - Fork 160
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
magento-devops-reposync-svc
merged 6 commits into
magento:develop
from
eliseacornejo:AC-601
Aug 19, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d650e1
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo 515563c
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo 750260e
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo 3479d07
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo 6144294
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo 0b9aaa2
AC-601: Create phpcs static check for AbstractBlockTest
eliseacornejo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
declare( strict_types = 1 ); | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
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'; | ||
|
||
/** | ||
* Warning 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 | ||
sivaschenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @return bool | ||
*/ | ||
private function isApplicable(String $content): bool | ||
sivaschenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.