Method __construct
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function __construct($expr)
{
$tokenTypes = [
'SPACE' => ['\s+', 'processTokenId'],
'SYMBOL' => ['[a-zA-Z][\w]*', 'processTokenSymbol'],
Missing class import via use statement (line '41', column '20'). Open
$this->EOF = new VTExpressionToken('EOF');
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
Missing class import via use statement (line '63', column '16'). Open
$token = new VTExpressionToken($tokenName);
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
Missing class import via use statement (line '86', column '14'). Open
return new VTEXpressionSymbol($token);
- Read upRead up
- Exclude checks
MissingImport
Since: 2.7.0
Importing all external classes in a file through use statements makes them clearly visible.
Example
function make() {
return new \stdClass();
}
Source http://phpmd.org/rules/cleancode.html#MissingImport
The method nextToken uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. Open
} else {
$token->value = $this->tokenTypes[$tokenName][1]($match[$i]);
}
- Read upRead up
- Exclude checks
ElseExpression
Since: 1.4.0
An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.
Example
class Foo
{
public function bar($flag)
{
if ($flag) {
// one branch
} else {
// another branch
}
}
}
Source https://phpmd.org/rules/cleancode.html#elseexpression
Avoid using static access to class 'App\Purifier' in method '__construct'. Open
preg_match_all($tokenRe, App\Purifier::decodeHtml($expr), $matches, PREG_SET_ORDER);
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Define a constant instead of duplicating this literal "processTokenSymbol" 6 times. Open
'SYMBOL' => ['[a-zA-Z][\w]*', 'processTokenSymbol'],
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Reference to undeclared property \VTExpressionToken->value
Open
$token->value = \call_user_func([$this, $this->tokenTypes[$tokenName][1]], $match[$i]);
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenTypes
(Did you mean $tokenTypes) Open
$this->tokenTypes = $tokenTypes;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->EOF
Open
return $this->EOF;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenNames
Open
$tokenName = $this->tokenNames[$i - 1];
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->EOF
Open
$this->EOF = new VTExpressionToken('EOF');
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenTypes
Open
$token->value = $this->tokenTypes[$tokenName][1]($match[$i]);
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenNames
(Did you mean $tokenNames) Open
$this->tokenNames = $tokenNames;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenTypes
Open
$token->value = \call_user_func([$this, $this->tokenTypes[$tokenName][1]], $match[$i]);
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->idx
(Did you mean $idx) Open
$this->idx = $idx + 1;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->idx
Open
$this->idx = 0;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->matches
Open
$matches = $this->matches;
- Exclude checks
Reference to undeclared property \VTExpressionToken->value
Open
$token->value = $this->tokenTypes[$tokenName][1]($match[$i]);
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->idx
Open
$idx = $this->idx;
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->tokenTypes
Open
if (method_exists($this, $this->tokenTypes[$tokenName][1])) {
- Exclude checks
Reference to undeclared property \VTExpressionTokenizer->matches
(Did you mean $matches) Open
$this->matches = $matches;
- Exclude checks
Similar blocks of code found in 2 locations. Consider refactoring. Open
$tokenTypes = [
'SPACE' => ['\s+', 'processTokenId'],
'SYMBOL' => ['[a-zA-Z][\w]*', 'processTokenSymbol'],
'ESCAPED_SYMBOL' => ['?:`([^`]+)`', 'processTokenSymbol'],
//"STRING" => array('?:(?:"((?:\\\\"|[^"])+)"|'."'((?:\\\\'|[^'])+)')", 'stripcslashes'),
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 122.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Each class must be in a namespace of at least one level (a top-level vendor name) Open
class VTExpressionTokenizer
- Exclude checks
Avoid variables with short names like $i. Configured minimum length is 3. Open
$i = 1;
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Avoid variables with short names like $re. Configured minimum length is 3. Open
[$re] = $code;
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}
Source https://phpmd.org/rules/naming.html#shortvariable
Spaces must be used to indent lines; tabs are not allowed Open
'ESCAPED_SYMBOL' => ['?:`([^`]+)`', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'FLOAT' => ['\d+[.]\d+', 'floatval'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$match = $matches[$idx];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->EOF = new VTExpressionToken('EOF');
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$token->value = \call_user_func([$this, $this->tokenTypes[$tokenName][1]], $match[$i]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->processTokenId($token);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'OPERATOR' => ['[+]|[-]|[*]|>=|<=|[<]|[>]|==|\/', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenNames = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
foreach ($tokenTypes as $tokenName => $code) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
[$re] = $code;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $token;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'SPACE' => ['\s+', 'processTokenId'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
//"STRING" => array('?:"((?:\\\\"|[^"])+)"', 'stripcslashes'),
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'OPEN_BRACKET' => ['[(]', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'CLOSE_BRACKET' => ['[)]', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function nextToken()
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $this->EOF;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$token->value = $this->tokenTypes[$tokenName][1]($match[$i]);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'SYMBOL' => ['[a-zA-Z][\w]*', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if ($idx == \count($matches)) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return new VTEXpressionSymbol($token);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function __construct($expr)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'COMMA' => ['[,]', 'processTokenSymbol'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenReArr[] = '(' . $re . ')';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$matches = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$matches = $this->matches;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function processTokenSymbol($token)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'INTEGER' => ['\d+', 'intval'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->matches = $matches;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
if (method_exists($this, $this->tokenTypes[$tokenName][1])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenTypes = [
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
{
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
'STRING' => ["?:'((?:\\\\'|[^'])+)'", 'stripcslashes'],
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->tokenTypes = $tokenTypes;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenNames[] = $tokenName;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenRe = '/' . implode('|', $tokenReArr) . '/';
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$idx = $this->idx;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->idx = $idx + 1;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
private function processTokenId($token)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
return $token;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
//"STRING" => array('?:(?:"((?:\\\\"|[^"])+)"|'."'((?:\\\\'|[^'])+)')", 'stripcslashes'),
- Exclude checks
Line exceeds 120 characters; contains 129 characters Open
// NOTE: Any new Operator added should be updated in VTParser.inc::$precedence and operation at VTExpressionEvaluater
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->tokenNames = $tokenNames;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenName = $this->tokenNames[$i - 1];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
} else {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
// NOTE: Any new Operator added should be updated in VTParser.inc::$precedence and operation at VTExpressionEvaluater
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$tokenReArr = [];
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
preg_match_all($tokenRe, App\Purifier::decodeHtml($expr), $matches, PREG_SET_ORDER);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
public function tests($token)
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->idx = 0;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$i = 1;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$this->processTokenSymbol($token);
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
while (empty($match[$i])) {
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
++$i;
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
}
- Exclude checks
Spaces must be used to indent lines; tabs are not allowed Open
$token = new VTExpressionToken($tokenName);
- Exclude checks