Skip to content

Commit

Permalink
Changed token checks to keyed arrays.
Browse files Browse the repository at this point in the history
See #1
  • Loading branch information
Rarst committed Dec 24, 2019
1 parent dfbea87 commit b7cfa73
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions src/CognitiveComplexity/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,57 @@ final class Analyzer
*
* @var int[]|string[]
*/
private $increments = [
T_IF,
T_ELSE,
T_ELSEIF,
T_SWITCH,
T_FOR,
T_FOREACH,
T_WHILE,
T_DO,
T_CATCH,
private const increments = [
T_IF => T_IF,
T_ELSE => T_ELSE,
T_ELSEIF => T_ELSEIF,
T_SWITCH => T_SWITCH,
T_FOR => T_FOR,
T_FOREACH => T_FOREACH,
T_WHILE => T_WHILE,
T_DO => T_DO,
T_CATCH => T_CATCH,
];

/** @var int[]|string[] */
private $booleanOperators = [
T_BOOLEAN_AND, // &&
T_BOOLEAN_OR, // ||
private const booleanOperators = [
T_BOOLEAN_AND => T_BOOLEAN_AND, // &&
T_BOOLEAN_OR => T_BOOLEAN_OR, // ||
];

/** @var int[]|string[] */
private $operatorChainBreaks = [
T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS,
T_SEMICOLON,
T_INLINE_THEN,
T_INLINE_ELSE,
private const operatorChainBreaks = [
T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS,
T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
T_SEMICOLON => T_SEMICOLON,
T_INLINE_THEN => T_INLINE_THEN,
T_INLINE_ELSE => T_INLINE_ELSE,
];

/**
* B3. Nesting increments
* @var int[]|string[]
*/
private $nestingIncrements = [
T_IF,
T_INLINE_THEN,
T_SWITCH,
T_FOR,
T_FOREACH,
T_WHILE,
T_DO,
T_CATCH,
private const nestingIncrements = [
T_IF => T_IF,
T_INLINE_THEN => T_INLINE_THEN,
T_SWITCH => T_SWITCH,
T_FOR => T_FOR,
T_FOREACH => T_FOREACH,
T_WHILE => T_WHILE,
T_DO => T_DO,
T_CATCH => T_CATCH,
];

/**
* B1. Increments
* @var int[]
*/
private $breakingTokens = [T_CONTINUE, T_GOTO, T_BREAK];
private const breakingTokens = [
T_CONTINUE => T_CONTINUE,
T_GOTO => T_GOTO,
T_BREAK => T_BREAK,
];

/**
* @param mixed[] $tokens
Expand Down Expand Up @@ -108,11 +112,11 @@ public function computeForFunctionFromTokensAndPosition(array $tokens, int $posi

++$this->cognitiveComplexity;

if (in_array($currentToken['code'], $this->breakingTokens, true)) {
if (isset(self::breakingTokens[$currentToken['code']])) {
continue;
}

$isNestingIncrement = in_array($currentToken['code'], $this->nestingIncrements, true);
$isNestingIncrement = isset(self::nestingIncrements[$currentToken['code']]);
$measuredNestingLevel = $this->getMeasuredNestingLevel($currentToken, $tokens, $position);

// B3. Nesting increment
Expand All @@ -132,13 +136,13 @@ public function computeForFunctionFromTokensAndPosition(array $tokens, int $posi
private function resolveBooleanOperatorChain(array $token): void
{
// Whenever we cross anything that interrupts possible condition we reset chain.
if ($this->lastBooleanOperator && in_array($token['code'], $this->operatorChainBreaks, true)) {
if ($this->lastBooleanOperator && isset(self::operatorChainBreaks[$token['code']])) {
$this->lastBooleanOperator = 0;

return;
}

if (!in_array($token['code'], $this->booleanOperators, true)) {
if (!isset(self::booleanOperators[$token['code']])) {
return;
}

Expand Down Expand Up @@ -174,7 +178,7 @@ private function resolveTryControlStructure(array $token): void
*/
private function isIncrementingToken(array $token, array $tokens, int $position): bool
{
if (in_array($token['code'], $this->increments, true)) {
if (isset(self::increments[$token['code']])) {
return true;
}

Expand All @@ -184,7 +188,7 @@ private function isIncrementingToken(array $token, array $tokens, int $position)
}

// B1. goto LABEL, break LABEL, continue LABEL
if (in_array($token['code'], $this->breakingTokens, true)) {
if (isset(self::breakingTokens[$token['code']])) {
$nextToken = $tokens[$position + 1]['code'];
if ($nextToken !== T_SEMICOLON) {
return true;
Expand Down

0 comments on commit b7cfa73

Please sign in to comment.