-
Notifications
You must be signed in to change notification settings - Fork 160
AC-683: Create phpcs static check for TableTest #309
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 5 commits into
magento:develop
from
svera:AC-683_tableTest
Oct 6, 2021
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf64205
AC-683: Create phpcs static check for TableTest
svera eb3ad47
AC-683: Create phpcs static check for TableTest
svera a1c933a
AC-683: Create phpcs static check for TableTest
svera 1452ed3
AC-683: Create phpcs static check for TableTest
svera ceeba56
AC-683: Create phpcs static check for TableTest
svera 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,234 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Magento2\Sniffs\Legacy; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
/** | ||
* Coverage of obsolete table names usage | ||
*/ | ||
class TableNameSniff implements Sniff | ||
{ | ||
/** | ||
* Methods which receive table names as parameters, with the argument position in which table names are passed | ||
* | ||
* @var array | ||
*/ | ||
private $argPositionInMethods = [ | ||
'getTableName' => [0], | ||
'_setMainTable' => [0], | ||
'setMainTable' => [0], | ||
'getTable' => [0], | ||
'setTable' => [0], | ||
'getTableRow' => [0], | ||
'deleteTableRow' => [0], | ||
'updateTableRow' => [0], | ||
'updateTable' => [0], | ||
'tableExists' => [0], | ||
'joinField' => [1], | ||
'joinTable' => [0], | ||
'getFkName' => [0, 2], | ||
'getIdxName' => [0], | ||
'addVirtualGridColumn' => [1], | ||
]; | ||
|
||
/** | ||
* String representation of error. | ||
* | ||
* @var string | ||
*/ | ||
private const ERROR_MESSAGE = 'Legacy table names with slash must be fixed to direct table names. Found: %s'; | ||
|
||
/** | ||
* Error violation code. | ||
* | ||
* @var string | ||
*/ | ||
private const ERROR_CODE = 'FoundLegacyTableName'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_OBJECT_OPERATOR, | ||
T_VARIABLE, | ||
T_DOUBLE_ARROW | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) { | ||
$this->checkOccurrencesInMethods($phpcsFile, $stackPtr, $tokens); | ||
} elseif ($tokens[$stackPtr]['code'] === T_DOUBLE_ARROW) { | ||
$this->checkOccurrencesInArray($phpcsFile, $stackPtr, $tokens); | ||
} else { | ||
$this->checkOccurrencesInProperty($phpcsFile, $stackPtr, $tokens); | ||
} | ||
} | ||
|
||
/** | ||
* Check if passed file is a resource but not a collection | ||
* | ||
* @param string $filePath | ||
* @return bool | ||
*/ | ||
private function isResourceButNotCollection(string $filePath): bool | ||
{ | ||
$filePath = str_replace('\\', '/', $filePath); | ||
$parts = explode('/', $filePath); | ||
return array_search('Resource', $parts) !== false && array_search('Collection.php', $parts) === false; | ||
} | ||
|
||
/** | ||
* Check references to table names in methods | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @param array $tokens | ||
*/ | ||
private function checkOccurrencesInMethods(File $phpcsFile, int $stackPtr, array $tokens): void | ||
{ | ||
$methodNamePos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); | ||
$methodName = $tokens[$methodNamePos]['content']; | ||
if (array_key_exists($methodName, $this->argPositionInMethods) === false) { | ||
return; | ||
} | ||
$firstArgumentPos = $phpcsFile->findNext([T_CONSTANT_ENCAPSED_STRING, T_VARIABLE], $methodNamePos + 1); | ||
|
||
foreach ($this->argPositionInMethods[$methodName] as $argPosition) { | ||
$paramPos = $firstArgumentPos; | ||
for ($i = 0; $i < $argPosition; $i++) { | ||
$paramPos = $phpcsFile->findNext( | ||
[T_CONSTANT_ENCAPSED_STRING, T_VARIABLE], | ||
$paramPos + 1, | ||
$phpcsFile->findNext(T_CLOSE_PARENTHESIS, $paramPos + 1) | ||
); | ||
} | ||
if (strpos($tokens[$paramPos]['content'], '/') !== false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
self::ERROR_MESSAGE, | ||
$tokens[$paramPos]['content'], | ||
), | ||
$paramPos, | ||
self::ERROR_CODE | ||
); | ||
} | ||
} | ||
|
||
if ($this->isResourceButNotCollection($phpcsFile->getFilename())) { | ||
if ($tokens[$stackPtr]['content'] !== '_init') { | ||
return; | ||
} | ||
|
||
$paramPos = $phpcsFile->findNext(T_PARAM_NAME, $stackPtr + 1); | ||
if (strpos($tokens[$paramPos]['content'], '/') !== false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
self::ERROR_MESSAGE, | ||
$tokens[$paramPos]['content'], | ||
), | ||
$paramPos, | ||
self::ERROR_CODE | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check references to table names in the $_aggregationTable property | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @param array $tokens | ||
*/ | ||
private function checkOccurrencesInProperty(File $phpcsFile, int $stackPtr, array $tokens): void | ||
{ | ||
if ($methodName = $tokens[$stackPtr]['content'] !== '$_aggregationTable') { | ||
return; | ||
} | ||
|
||
$tableNamePos = $phpcsFile->findNext( | ||
T_CONSTANT_ENCAPSED_STRING, | ||
$stackPtr + 1, | ||
$phpcsFile->findEndOfStatement($stackPtr + 1) | ||
); | ||
|
||
if (strpos($tokens[$tableNamePos]['content'], '/') !== false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
self::ERROR_MESSAGE, | ||
$tokens[$tableNamePos]['content'], | ||
), | ||
$tableNamePos, | ||
self::ERROR_CODE | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Check references to table names in arrays | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @param array $tokens | ||
*/ | ||
private function checkOccurrencesInArray(File $phpcsFile, int $stackPtr, array $tokens): void | ||
{ | ||
$aliasPos = $phpcsFile->findPrevious( | ||
T_CONSTANT_ENCAPSED_STRING, | ||
$stackPtr -1 | ||
); | ||
|
||
$alias = trim($tokens[$aliasPos]['content'], '\'"'); | ||
|
||
if ($this->endsWith($alias, '_table') === false) { | ||
return; | ||
} | ||
|
||
$tableNamePos = $phpcsFile->findNext( | ||
T_CONSTANT_ENCAPSED_STRING, | ||
$aliasPos + 1 | ||
); | ||
|
||
if (strpos($tokens[$tableNamePos]['content'], '/') !== false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
self::ERROR_MESSAGE, | ||
$tokens[$tableNamePos]['content'], | ||
), | ||
$tableNamePos, | ||
self::ERROR_CODE | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if $haystack ends with $needle | ||
* | ||
* @param string $haystack | ||
* @param string $needle | ||
* @return bool | ||
*/ | ||
private function endsWith(string $haystack, string $needle): bool | ||
{ | ||
$length = strlen($needle); | ||
if ($length === 0) { | ||
return true; | ||
} | ||
return substr($haystack, -$length) === $needle; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
$productTable = $this->_resource->getTableName('/catalog_category_product'); | ||
|
||
$collection->joinField( | ||
'inventory_in_stock', | ||
'/cataloginventory_stock_item', | ||
'is_in_stock', | ||
'product_id=entity_id', | ||
'(' . join(') OR (', $cond) . ')' | ||
); | ||
|
||
$collection->getFkName( | ||
'inventory_in_stock', | ||
$collection, | ||
'/is_in_stock' | ||
); | ||
|
||
$select = $connection->select()->from( | ||
['main_table' => 'magento/sample_table'] | ||
); | ||
|
||
$select = $connection->select()->from( | ||
['main_table_name' => 'magento/sample_table'] | ||
); | ||
|
||
$select = $connection->select()->from( | ||
['main_table' => 'magento_sample_table'] | ||
); | ||
|
||
class Collection extends \Magento\Sales\Model\ResourceModel\Report\Order\Collection | ||
{ | ||
/** | ||
* Aggregated Data Table | ||
* | ||
* @var string | ||
*/ | ||
protected $_aggregationTable = 'magento/sales_order_aggregated_updated'; | ||
} | ||
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 TableNameUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList($testFile = '') | ||
{ | ||
return [ | ||
3 => 1, | ||
7 => 1, | ||
16 => 1, | ||
20 => 1, | ||
38 => 1, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList($testFile = '') | ||
{ | ||
return []; | ||
} | ||
} |
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.