Esempio n. 1
0
 /**
  * Fix a spefic problem as reported by PHPCS
  *
  * @param string  $fixMessage
  * @param string  $fixCode
  * @param string  $pattern
  * @param integer $row
  * @param integer $col
  *
  * @return boolean
  */
 protected function _fixProblem($fixMessage, $fixCode, $pattern, $row, $col)
 {
     $debug = false;
     $CodeRow = $this->_CodeRows[$row];
     $NextRow = false;
     $PrevRow = false;
     if (isset($this->_CodeRows[$row + 1])) {
         $NextRow = $this->_CodeRows[$row + 1];
     }
     if (isset($this->_CodeRows[$row - 1])) {
         $PrevRow = $this->_CodeRows[$row - 1];
     }
     $original = $CodeRow->getCodeRow();
     $matches = array();
     $pattExpression = '[^\\)]+';
     $pattFunctionCall = '[a-zA-Z0-9\\_]+';
     $controlStructures = array("if", "else", "elseif", "do", "while", "switch", "for", "foreach");
     $controlStructuresTxt = implode("|", $controlStructures);
     // Init
     $this->_fixedLog[$fixCode][$row]["error"] = "";
     // Get matched variables from message based on pattern
     if (preg_match_all("#" . $pattern . "#", $fixMessage, $matches_raw)) {
         unset($matches_raw[0]);
         foreach ($matches_raw as $i => $match) {
             $matches[$i - 1] = $match[0];
         }
         if (count($matches) == 2) {
             list($expected, $found) = $matches;
             $needed = @($expected - $found);
         } elseif (count($matches) == 1) {
             list($expected) = $matches;
         }
     }
     switch ($fixCode) {
         case "EXPECTED":
             // '( '
             $CodeRow->regplace('(\\()[\\s]+', '(', 'T_ALLOTHER', 1);
             // ' )'
             $CodeRow->regplace('[\\s]+(\\))', ')', 'T_ALLOTHER', 1);
             // '){'
             $CodeRow->regplace('\\){', ') {', 'T_ALLOTHER', 1);
             // 'else{'
             $CodeRow->regplace('(' . $controlStructuresTxt . ')[\\s]+{', $this->_getPostFormatBackSpaceCB() . ' $1 {', 'T_ALLOTHER', 1);
             // '			   else {'
             $CodeRow->regplace('^[\\s]+(' . $controlStructuresTxt . ')[\\s]*{', $this->_getPostFormatBackSpaceCB() . ' $1 {', 'T_ALLOTHER', 1);
             // 'elseif (!$insensitive && substr_count($l, $pattern)) {'
             $CodeRow->regplace('^[\\s]*(elseif)', $this->_getPostFormatBackSpaceCB() . ' $1 ', 'T_ALLOTHER', -1);
             // '}else{' || '}  else	  {'
             $CodeRow->regplace('}[\\s]*(' . $controlStructuresTxt . ')[\\s]*{', '} $1 {', 'T_ALLOTHER', -1);
             // 'while($row = mysql_fetch_array($res)) {'
             $CodeRow->regplace('(' . $controlStructuresTxt . ')\\(', '$1 (', 'T_ALLOTHER', -1);
             // 'count ('
             if (preg_match_all('/(' . $pattFunctionCall . ') \\(/', $CodeRow->getCodeRow(), $m)) {
                 $functionCalls = $m[1];
                 foreach ($functionCalls as $functionCall) {
                     // Don't do this for control structures!
                     if (in_array($functionCall, $controlStructures)) {
                         continue;
                     }
                     $CodeRow->regplace('(' . $functionCall . ') \\(', '$1(', 'T_ALLOTHER', -1);
                 }
             }
             // 'if ($v) {$keep = !$keep;'
             $CodeRow->regplace('{[\\s]*(\\S+)', '{ ' . $this->_getPostFormatAddNewline() . $CodeRow->getIndentation(+4) . '$1', 'T_ALLOTHER', -1);
             /*				if ($expected == '"if (...) {\n"; found "if (...) {"') {
             					$CodeRow->insertAt($CodeRow->getPosBraceOpen(+1),
             						$this->_getPostFormatAddNewline() . $CodeRow->getIndentation(+4));
             				}
             */
             break;
         case "TOO_LNG":
             // "Line exceeds 85 characters; contains 96 characters
             $CodeRow->wrap($this->_getPostFormatAddNewline(), 85, $CodeRow->getIndentation(+4));
             break;
         case "IVD_DSC":
             // You must use "/**" style comments for a function comment
         // You must use "/**" style comments for a function comment
         case "MIS_DSC":
             // Missing function/doc comment
             $indent = $CodeRow->getIndent();
             $DocBlockWriter = new DocBlockWriter();
             $DocBlockWriter->setDocBLockDefaults($this->_docBlockDefaults);
             $DocBlockWriter->setIndent($indent);
             $DocBlockWriter->setNewLineChar($this->_getPostFormatAddNewline());
             if (!isset($expected)) {
                 $this->_fixedLog[$fixCode][$row]["error"] .= "var expected was not set!!!";
             } elseif ($expected == "function") {
                 $CodeRow->insertAt($CodeRow->getIndent(+1), $DocBlockWriter->generateFunction($CodeRow->getCodeRow()));
             } else {
                 if ($expected == "class") {
                     $CodeRow->insertAt($CodeRow->getIndent(+1), $DocBlockWriter->generateClass($CodeRow->getCodeRow()));
                 } elseif ($expected == "file") {
                     $CodeRow->insertAt(1, $DocBlockWriter->generateFile());
                 }
             }
             if (count($DocBlockWriter->errors)) {
                 $this->_fixedLog[$fixCode][$row]["error"] .= "DocBlock Errors: " . implode('; ', $DocBlockWriter->errors);
             }
             break;
         case "IVD_PSC":
             // Perl-style comments are not allowed. Use "// Comment.\" or "/* comment */" instead.
             $CodeRow->regplace('\\#(\\s*)', '// ', 'T_COMMENT', 1);
             break;
         case "MIS_LNG_TAG":
             // Short PHP opening tag used. Found "<?" Expected "<?php".
             $CodeRow->replace('<?', '<?php', 'T_OPEN_TAG');
             break;
         case "MIS_UPC_CNS":
             // Constants must be uppercase; expected IS_NUMERIC but found is_numeric
             $CodeRow->replace($found, $expected, 'T_ALLOTHER', 1);
             break;
         case "MIS_ALN_EQL":
             // Equals sign not aligned correctly
             // Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
             // Before Equal
             // Insert will actually backspace on negative amount
             $posEq = $CodeRow->getPosEqual();
             $posBefEq = $posEq - 1;
             $chrBefEq = $CodeRow->getCharAt($posBefEq);
             $posIns = $posEq;
             if (in_array($chrBefEq, array("."))) {
                 $posIns = $posBefEq;
             }
             $CodeRow->insertAt($posIns, " ", $needed);
             // After Equal
             $CodeRow->regplace('=([a-zA-Z0-9\\$\\_]+)', '= $1', 'T_ALLOTHER', 1);
             break;
         case "IND":
             // Line indented incorrectly; expected 12 spaces, found 16
             $CodeRow->setIndent($expected);
             break;
         case "MIS_SPC_AFT_CMA":
             // No space found after comma in function call
             $CodeRow->regplace(',([^ ]|$)', ', $1', 'T_ALLOTHER', -1);
             break;
         case "MIS_NWL_ARN_CLS_BRC":
             // Closing brace must be on a line by itself
             $CodeRow->insertAt($CodeRow->getPosBraceClose(), $this->_getPostFormatAddNewline() . $CodeRow->getIndentation());
             break;
         case "MIS_NWL_ARN_OPN_BRC":
             // Opening function brace should be on a new line
             $CodeRow->insertAt($CodeRow->getPosBraceOpen(), $this->_getPostFormatAddNewline() . $CodeRow->getIndentation());
             break;
         case "FND_SWS_BFR_CMA":
             // Space found before comma in function call
             $CodeRow->regplace('(\\s+),', ',', 'T_ALLOTHER', 1);
             break;
         case "FND_SPC_PTH":
             // Space surrounding parentheses
             list($spc_loc, $pth_typ) = $matches;
             $a = $b = $pth = "";
             $pth = $pth_typ == 'opening' ? 'c' : '(\\))';
             $spc = '[\\s+]';
             if ($spc_loc == 'before') {
                 $a = $spc;
             } else {
                 $b = $spc;
             }
             $debug = "[{$spc_loc}][{$pth_typ}] replacing " . $a . $pth . $b;
             $CodeRow->regplace($a . $pth . $b, '$1', 'T_ALLOTHER', 1);
             break;
         default:
             $this->_fixedLog[$fixCode][$row]["error"] .= "No such fix: " . $fixCode . "!!!";
             return false;
             break;
     }
     $this->wasModifiedBy[$row][] = $fixCode;
     // Default case returns false, so all matched cases don't have to.
     if ($debug) {
         $this->_fixedLog[$fixCode][$row]["debug"] = $debug;
     }
     $modified = $CodeRow->getCodeRow();
     if ($modified == $original) {
         $this->_fixedLog[$fixCode][$row]["error"] .= "Nothing was modified!!!";
     }
     $this->_fixedLog[$fixCode][$row]["match"] = implode(", ", $matches);
     return true;
 }