function findAll(&$fileString, $options = null) { if (empty($fileString) || !is_string($fileString)) { throw new Exception("Not given a proper file string."); } if (!isset($options)) { $options = $this->defaultOptions; } if (!isset($options->noFuncList)) { throw new Exception("Required property of NoFuncCodeRule item, noFuncList is missing."); } $nIndecies = RegExCodeReviewer::getNewlineIndecies($fileString); $issues = array(); foreach ($options->noFuncList as $noFuncKey => $noFuncObj) { if (empty($noFuncObj->funcName)) { throw new Exception("Required property of noFuncList item, funcName is missing."); } $matchCount = preg_match_all("/[^a-zA-Z0-9]{$noFuncObj->funcName}\\(/", $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('NoFuncCodeRule', $options->filePath, 1, 1, false, $noFuncObj->reason . " function({$noFuncObj->funcName}) was used.", $line + 1, $char, $match[0], null); } } } return $issues; }
/** * Given the text of a file, and any file specific options, finds all applications * of this code rule. * * @param string $fileString a string representation of a code file to find * and apply the rule to. Given as reference so that it may make changes to * the string if needed, but usually shouldn't. * @param object $options a value object used to override any default options set for this rule. * @return array a list of CodeIssues this rule generated from application to the fileString */ function findAll(&$fileString, $options = null) { if (empty($fileString) || !is_string($fileString)) { throw new Exception("Not given a proper file string."); } if (!isset($options)) { $options = $this->defaultOptions; } $nIndecies = RegExCodeReviewer::getNewlineIndecies($fileString); $issues = array(); $matchCount = preg_match_all('/\\<\\?\\s/', $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('PHPTagsCodeRule', $options->filePath, 0, 1, false, "Opening PHP tag does not use the '<?php' form.", $line + 1, $char, $match[0], null); } } $matchCount = preg_match_all('/\\<\\?[pP][hH][pP][^\\s]/', $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('PHPTagsCodeRule', $options->filePath, 2, 2, false, "Opening PHP tag({$match[0]}...) does not have a whitespace postfix.", $line + 1, $char, $match[0], null); } } $matchCount = preg_match_all('/\\<\\?(?:[pP][hH][pP])?\\s/', $fileString, $ruleMatches); if ($matchCount > 1) { return $issues; } $matchCount = preg_match_all('/\\?\\>\\s*$/', $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('PHPTagsCodeRule', $options->filePath, 2, 3, false, "Closing php tag was found at the end of script file.", $line + 1, $char, $match[0], null); } } return $issues; }
/** * Given the text of a file, and any file specific options, finds all applications * of this code rule. * * @param string $fileString a string representation of a code file to find * and apply the rule to. Given as reference so that it may make changes to * the string if needed, but usually shouldn't. * @param object $options a value object used to override any default options set for this rule. * @return array a list of CodeIssues this rule generated from application to the fileString */ function findAll(&$fileString, $options = null) { if (empty($fileString) || !is_string($fileString)) { throw new Exception("Not given a proper file string."); } if (!isset($options)) { $options = $this->defaultOptions; } if (!isset($options->rules)) { throw new Excpetion("Required property of GenRegExCodeRule item, rules is missing."); } $nIndecies = RegExCodeReviewer::getNewlineIndecies($fileString); $issues = array(); $defaultRuleID = 0; foreach ($options->rules as $ruleName => $rule) { if (!isset($rule->regEx)) { throw new Exception("Required property of {$ruleName} item, regEx is missing."); } if (!isset($rule->level)) { $rule->level = 0; } if (!isset($rule->ruleID)) { $rule->ruleID = $defaultRuleID++; } if (!isset($rule->description)) { $rule->description = "Failed Rule ({$rule->regEx})"; } $matchCount = preg_match_all($rule->regEx, $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('GenRegExCodeRule', $options->filePath, $rule->level, $rule->ruleID, false, $rule->description, $line + 1, $char, $match[0], null); } } } return $issues; }
/** * Given the text of a file, and any file specific options, finds all applications * of this code rule. * * @param string $fileString a string representation of a code file to find * and apply the rule to. Given as reference so that it may make changes to * the string if needed, but usually shouldn't. * @param object $options a value object used to override any default options set for this rule. * @return array a list of CodeIssues this rule generated from application to the fileString */ function findAll(&$fileString, $options = null) { if (empty($fileString) || !is_string($fileString)) { throw new Exception("Not given a proper file string."); } if (!isset($options)) { $options = $this->defaultOptions; } $nIndecies = RegExCodeReviewer::getNewlineIndecies($fileString); $issues = array(); $matchCount = preg_match_all('/\\$[a-z0-9_]*[A-Z]+[a-z0-9_]*|\\-\\>[a-z0-9_]*[A-Z]+[a-z0-9_]*[^a-zA-Z0-9_\\(]/', $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $name = $match[0]; if (isset($options->exceptions->{$name}) && $options->exceptions->{$name}) { continue; } $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('VarNameCodeRule', $options->filePath, 0, 1, false, "Variable ({$match[0]}) has a capital letter.", $line + 1, $char, $match[0], null); } } $matchCount = preg_match_all('/\\$[a-z0-9A-Z_]*[_]+[a-z0-9A-Z_]*|\\-\\>[a-z0-9A-Z_]*[_]+[a-z0-9A-Z_]*[^a-zA-Z0-9_\\(]/', $fileString, $ruleMatches, PREG_OFFSET_CAPTURE); if ($matchCount) { foreach ($ruleMatches[0] as $match) { $name = $match[0]; if (isset($options->exceptions->{$name}) && $options->exceptions->{$name}) { continue; } $line = RegExCodeReviewer::findInsertIndex($nIndecies, $match[1]); $char = $match[1] - $nIndecies[$line]; $issues[] = new CodeIssue('VarNameCodeRule', $options->filePath, 0, 2, false, "Variable ({$match[0]}) has an underscore.", $line + 1, $char, $match[0], null); } } return $issues; }