Skip to content

Fix magento/magento-coding-standard#214 with class not found #215

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
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
128 changes: 128 additions & 0 deletions Magento2/Helpers/Tokenizer/AbstractTokenizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Helpers\Tokenizer;

/**
* Template constructions tokenizer
*/
abstract class AbstractTokenizer
{
/**
* Current index in string
*
* @var int
*/
protected $_currentIndex;

/**
* String for tokenize
*
* @var string
*/
protected $_string;

/**
* Move current index to next char.
*
* If index out of bounds returns false
*
* @return boolean
*/
public function next()
{
if ($this->_currentIndex + 1 >= strlen($this->_string)) {
return false;
}

$this->_currentIndex++;
return true;
}

/**
* Move current index to previous char.
*
* If index out of bounds returns false
*
* @return boolean
*/
public function prev()
{
if ($this->_currentIndex - 1 < 0) {
return false;
}

$this->_currentIndex--;
return true;
}

/**
* Move current index backwards.
*
* If index out of bounds returns false
*
* @param int $distance number of characters to backtrack
* @return bool
*/
public function back($distance)
{
if ($this->_currentIndex - $distance < 0) {
return false;
}

$this->_currentIndex -= $distance;
return true;
}

/**
* Return current char
*
* @return string
*/
public function char()
{
return $this->_string[$this->_currentIndex];
}

/**
* Set string for tokenize
*
* @param string $value
* @return void
*/
public function setString($value)
{
//phpcs:ignore Magento2.Functions.DiscouragedFunction
Copy link
Author

@edspc edspc Aug 9, 2021

Choose a reason for hiding this comment

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

Additionally added this line for the pipeline.

$this->_string = rawurldecode($value);
$this->reset();
}

/**
* Move char index to begin of string
*
* @return void
*/
public function reset()
{
$this->_currentIndex = 0;
}

/**
* Return true if current char is white-space
*
* @return boolean
*/
public function isWhiteSpace()
{
return $this->_string === '' ?: trim($this->char()) !== $this->char();
}

/**
* Tokenize string
*
* @return array
*/
abstract public function tokenize();
}
76 changes: 76 additions & 0 deletions Magento2/Helpers/Tokenizer/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Helpers\Tokenizer;

/**
* Template constructions parameters tokenizer
*/
class Parameter extends AbstractTokenizer
{
/**
* Tokenize string and return getted parameters
*
* @return array
*/
public function tokenize()
{
$parameters = [];
$parameterName = '';
do {
if ($this->isWhiteSpace()) {
continue;
}

if ($this->char() !== '=') {
$parameterName .= $this->char();
} else {
$parameters[$parameterName] = $this->getValue();
$parameterName = '';
}
} while ($this->next());
return $parameters;
}

/**
* Get string value in parameters through tokenize
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function getValue()
{
$this->next();
$value = '';
if ($this->isWhiteSpace()) {
return $value;
}
$quoteStart = $this->char() == "'" || $this->char() == '"';

if ($quoteStart) {
$breakSymbol = $this->char();
} else {
$breakSymbol = false;
$value .= $this->char();
}

while ($this->next()) {
if (!$breakSymbol && $this->isWhiteSpace()) {
break;
} elseif ($breakSymbol && $this->char() == $breakSymbol) {
break;
} elseif ($this->char() == '\\') {
$this->next();
if ($this->char() != '\\') {
$value .= '\\';
}
$value .= $this->char();
} else {
$value .= $this->char();
}
}
return $value;
}
}
Loading