Skip to content
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

[PHP 7.1 Compatibility] Void became a reserved word #8609

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use Magento\Backend\App\Action;

class Void extends \Magento\Backend\App\Action
class VoidAction extends Action
{
/**
* Authorization level of a basic admin session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Magento\Sales\Controller\Adminhtml\Order\Invoice;

class Void extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
class VoidAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\View
{
/**
* Void invoice action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
namespace Magento\Sales\Test\Unit\Controller\Adminhtml\Order\Creditmemo;

/**
* Class VoidTest
* Class VoidActionTest
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class VoidTest extends \PHPUnit_Framework_TestCase
class VoidActionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Sales\Controller\Adminhtml\Order\Creditmemo\AddComment
Expand Down Expand Up @@ -179,7 +179,7 @@ protected function setUp()

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->controller = $objectManager->getObject(
\Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Void::class,
\Magento\Sales\Controller\Adminhtml\Order\Creditmemo\VoidAction::class,
[
'context' => $this->contextMock,
'creditmemoLoader' => $this->loaderMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Magento\Sales\Api\InvoiceRepositoryInterface;

/**
* Class VoidTest
* Class VoidActionTest
* @package Magento\Sales\Controller\Adminhtml\Order\Invoice
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class VoidTest extends \PHPUnit_Framework_TestCase
class VoidActionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
Expand Down Expand Up @@ -166,7 +166,7 @@ protected function setUp()
->getMockForAbstractClass();

$this->controller = $objectManager->getObject(
\Magento\Sales\Controller\Adminhtml\Order\Invoice\Void::class,
\Magento\Sales\Controller\Adminhtml\Order\Invoice\VoidAction::class,
[
'context' => $contextMock,
'resultForwardFactory' => $this->resultForwardFactoryMock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@
class ReservedWordsSniff implements PHP_CodeSniffer_Sniff
{
/**
* source: http://php.net/manual/en/reserved.other-reserved-words.php
* The following words cannot be used to name a class, interface or trait,
* and they are also prohibited from being used in namespaces.
*
* @var array PHP 7 reserved words for name spaces
* @link http://php.net/manual/en/reserved.other-reserved-words.php
*
* @var string[]
*/
protected $reservedWords = [
'int',
'float',
'bool',
'string',
'true',
'false',
'null',
'resource',
'object',
'mixed',
'numeric',
'int' => '7',
'float' => '7',
'bool' => '7',
'string' => '7',
'true' => '7',
'false' => '7',
'null' => '7',
'void' => '7.1',
'iterable' => '7.1',
'resource' => '7',
'object' => '7',
'mixed' => '7',
'numeric' => '7',
];

/**
* {@inheritdoc}
*/
public function register()
{
return [T_NAMESPACE, T_CLASS];
return [T_CLASS, T_INTERFACE, T_TRAIT, T_NAMESPACE];
}

/**
Expand All @@ -44,20 +49,23 @@ public function register()
* @param int $stackPtr
* @return void
*/
protected function validateNameSpace(PHP_CodeSniffer_File $sourceFile, $stackPtr)
protected function validateNamespace(PHP_CodeSniffer_File $sourceFile, $stackPtr)
{
$skippedTokens = ['T_NS_SEPARATOR', 'T_WHITESPACE'];
//skip "namespace" and whitespace
$stackPtr += 2;
$tokens = $sourceFile->getTokens();
while ('T_SEMICOLON' != $tokens[$stackPtr]['type']) {
if (in_array($tokens[$stackPtr]['type'], $skippedTokens)) {
$stackPtr++;
while ($stackPtr < $sourceFile->numTokens && $tokens[$stackPtr]['code'] !== T_SEMICOLON) {
if ($tokens[$stackPtr]['code'] === T_WHITESPACE || $tokens[$stackPtr]['code'] === T_NS_SEPARATOR) {
$stackPtr++; //skip "namespace" and whitespace
continue;
}
$nameSpacePart = strtolower($tokens[$stackPtr]['content']);
if (in_array($nameSpacePart, $this->reservedWords)) {
$sourceFile->addError('\'' . $nameSpacePart . '\' is a reserved word in PHP 7.', $stackPtr);
$namespacePart = $tokens[$stackPtr]['content'];
if (isset($this->reservedWords[strtolower($namespacePart)])) {
$sourceFile->addError(
'Cannot use "%s" in namespace as it is reserved since PHP %s',
$stackPtr,
'Namespace',
[$namespacePart, $this->reservedWords[$namespacePart]]
);
}
$stackPtr++;
}
Expand All @@ -73,12 +81,15 @@ protected function validateNameSpace(PHP_CodeSniffer_File $sourceFile, $stackPtr
protected function validateClass(PHP_CodeSniffer_File $sourceFile, $stackPtr)
{
$tokens = $sourceFile->getTokens();
//skipped "class" and whitespace
$stackPtr += 2;
$stackPtr += 2; //skip "class" and whitespace
$className = strtolower($tokens[$stackPtr]['content']);

if (in_array($className, $this->reservedWords)) {
$sourceFile->addError('Class name \'' . $className . '\' is a reserved word in PHP 7', $stackPtr);
if (isset($this->reservedWords[$className])) {
$sourceFile->addError(
'Cannot use "%s" as class name as it is reserved since PHP %s',
$stackPtr,
'Class',
[$className, $this->reservedWords[$className]]
);
}
}

Expand All @@ -88,12 +99,14 @@ protected function validateClass(PHP_CodeSniffer_File $sourceFile, $stackPtr)
public function process(PHP_CodeSniffer_File $sourceFile, $stackPtr)
{
$tokens = $sourceFile->getTokens();
switch ($tokens[$stackPtr]['type']) {
case "T_CLASS":
switch ($tokens[$stackPtr]['code']) {
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
$this->validateClass($sourceFile, $stackPtr);
break;
case "T_NAMESPACE":
$this->validateNameSpace($sourceFile, $stackPtr);
case T_NAMESPACE:
$this->validateNamespace($sourceFile, $stackPtr);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/App/Router/ActionList.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ActionList
'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'instanceof',
'insteadof','interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected',
'public', 'require', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var',
'while', 'xor',
'while', 'xor', 'void',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@antonkril why do we even need such check instead of simple fallback to a VoidAction in case Void class is not available?

];

/**
Expand Down