Skip to content

Commit

Permalink
Adding support for multiline conditions & and other valid syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
jails committed Mar 29, 2013
1 parent 900456a commit 1725b6c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 24 deletions.
57 changes: 33 additions & 24 deletions test/rules/HasCorrectTabIndention.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,50 +94,49 @@ protected function _beginningTabCountPredicted($lineIndex, $testable) {
$endingTokens = array(T_ENDFOR, T_ENDFOREACH, T_ENDIF, T_ENDSWITCH, T_ENDWHILE);
$switch = false;

if ($lineLen > 0 && ($line[0] === ")" || $line[0] === "}" || $line[0] === "]")) {
if ($lineLen > 0 && ($line[0] === ')' || $line[0] === '}' || $line[0] === ']')) {
$ending = $testable->findNextContent(array('}'), $currentTokens);
$child = isset($tokens[$ending]) ? $tokens[$ending] : false;
$parent = isset($tokens[$child['parent']]) ? $tokens[$child['parent']] : false;
if ($parent && $parent['id'] === T_SWITCH && $line[0] === "}") {
if ($parent && $parent['id'] === T_SWITCH && $line[0] === '}') {
$this->_currentCount -= 2;
} else {
$this->_currentCount -= 1;
}
}
if ($lineLen > 0 && $line[$lineLen - 1] === ":") {
$elif = strpos($line, "elseif") !== false || strpos($line, "else if") !== false;
if ($lineLen > 0 && $line[$lineLen - 1] === ':') {
$elif = strpos($line, 'elseif') !== false || strpos($line, 'else if') !== false;
$else = $line[$lineLen - 5] . $line[$lineLen - 4];
$else .= $line[$lineLen - 3] . $line[$lineLen - 2];
if ($elif || $else === "else") {
if ($elif || $else === 'else') {
$this->_currentCount -= 1;
}
}

$currentCount = $this->_currentCount;

$termOp = $lineLen > 1 && $line[0] === "-" && $line[1] === ">";
$boolAnd = $boolOr = $logAnd = $logOr = false;
$decimal = $prevLen > 0 && $prevLine[$prevLen - 1] === ".";
if ($prevLen > 2) {
$last = $prevLine[$prevLen - 3] . $prevLine[$prevLen - 2] . $prevLine[$prevLen - 1];
$logAnd = strtolower($last) === 'and';
$logicOp = false;
foreach (array('&&', '||', 'and', 'or', 'xor', 'AND', 'OR', 'XOR') as $op) {
$op = preg_quote($op);
if (preg_match("/\s+$op$/", $prevLine)) {
$logicOp = true;
}
}
if ($prevLen > 1) {
$last = $prevLine[$prevLen - 2] . $prevLine[$prevLen - 1];
$boolAnd = $last === '&&';
$boolOr = $last === '||';
$logOr = strtolower($last) === 'or';
$termOp = false;
if (preg_match('/^->/', $line)) {
$termOp = true;
}

if ($termOp || $boolAnd || $boolOr || $logAnd || $logOr || $decimal) {
if ($logicOp || $termOp) {
if (!$tokens[reset($currentTokens)]['brackets']) {
$currentCount += 1;
}
} elseif (substr($prevLine, -1) === '.') {
$currentCount += 1;
}

$switch = false;
$find = false;
$find = $find || substr($line, -6) === "break;";
$find = $find || substr($line, -8) === "default:";
$find = $find || ($line[$lineLen - 1] === ":" && strpos($line, "case") !== false);
$find = preg_match('/(break;|default:|case\s(.*?):)$/', $line);
if ($find) {
$childContent = array('case', 'default', 'break');
$child = $tokens[$testable->findNextContent($childContent, $currentTokens)];
Expand All @@ -147,20 +146,30 @@ protected function _beginningTabCountPredicted($lineIndex, $testable) {
}
}

$find = !$switch && in_array($line[$lineLen - 1], array("{", ":", "(", "["));
$find = !$switch && in_array($line[$lineLen - 1], array('{', ':'));
$found = false;
$inBrace = 0;
foreach ($currentTokens as $tokenKey) {
if (in_array($tokens[$tokenKey]['id'], $endingTokens, true)) {
$id = $tokens[$tokenKey]['id'];
if (in_array($id, $endingTokens, true)) {
$currentCount -= 1;
$this->_currentCount -= 1;
}
if ($find && $tokens[$tokenKey]['id'] === T_SWITCH) {
if ($find && $id === T_SWITCH) {
$this->_currentCount += 2;
$found = true;
}
$content = $tokens[$tokenKey]['content'];
if ($content === '(' || $content === '[') {
$inBrace++;
} elseif ($content === ')' || $content === ']') {
$inBrace--;
}
}
if ($find && !$found) {
$this->_currentCount += 1;
} elseif ($inBrace > 0) {
$this->_currentCount += 1;
}
return $currentCount;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/cases/test/rules/HasCorrectTabIndentionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,45 @@ public function testFailingTabIndentPropagation() {
$this->assertRuleFail($code, $this->rule);
$this->assertCount(1, $this->rule->violations());
}

public function testIgnoreBracesInString() {
$code = <<<EOD
\$code = 'hello(';
echo bob;
EOD;
$this->assertRulePass($code, $this->rule);
}

public function testClosureAsParameter() {
$code = <<<EOD
return \$this->_filter(__METHOD__, \$args, function(\$self, \$args) {
\$variable = \$this->_protected;
if (isset(\$this->_path[\$variable])) {
return \$this->path[\$variable];
}
});
EOD;
$this->assertRulePass($code, $this->rule);
}

public function testMutlilineConditions() {
$code = <<<EOD
\$expression = (isset(\$variable[\$key]) && (
\$class instanceof static::\$_classes['item'] ||
\$condtions
));
EOD;
$this->assertRulePass($code, $this->rule);

$code = <<<EOD
\$expression = (
\$var1 &&
\$var2 ||
\$var3
);
EOD;
$this->assertRulePass($code, $this->rule);
}
}

?>

0 comments on commit 1725b6c

Please sign in to comment.