getErrors() public method

Returns the errors raised from processing this file.
public getErrors ( ) : array
return array
Example #1
0
 private function handlePHPCSErrors(File $file, \PHP_CodeSniffer_File $phpcsFile)
 {
     $maxLine = strlen(max(array_keys($phpcsFile->getErrors())));
     $byLine = $this->prepareFileContent($file);
     foreach ($phpcsFile->getErrors() as $lineNumber => $errorsByLine) {
         $message = sprintf("   | Line %{$maxLine}s:%s\n", $lineNumber, $byLine[$lineNumber + 1]);
         foreach ($errorsByLine as $column => $errorsCollection) {
             foreach ($errorsCollection as $error) {
                 $message .= sprintf("   |%{$maxLine}s| Error at column %s: %s\n", '', $column, $error['message']);
             }
         }
         $this->pushError($message);
     }
 }
Example #2
0
 public function cswExecute()
 {
     // PHP_CodeSniffer - silent prepare
     ob_start();
     $this->setTokenListeners($this->cswStandard, array());
     $this->populateCustomRules();
     $this->populateTokenListeners();
     $tlisteners = $this->getTokenSniffs();
     ob_end_clean();
     // PHP_CodeSniffer - silent process each item and collect results
     foreach ($this->cswData as $index => $item) {
         ob_start();
         $pcsFile = new PHP_CodeSniffer_File('', $tlisteners['file'], $this->allowedFileExtensions, $this->ruleset, $this);
         $pcsFile->start($item['code']);
         $this->cswData[$index]['output'] = $this->storeOutput ? ob_get_contents() : 'output disabled';
         ob_end_clean();
         // free some memory
         unset($this->cswData[$index]['code']);
         // prepare full report
         $this->cswData[$index]['messages'] = $this->mergeMessages($pcsFile->getErrors(), $pcsFile->getWarnings(), $item['code_lines']);
         // prepare report for lines
         $this->cswData[$index]['report_for_lines'] = $this->createReportForLines($this->cswData[$index]);
     }
     // return data
     return $this->cswData;
 }
 /**
  * Gather all error messages by line number from phpcs file result
  *
  * @param PHP_CodeSniffer_File $file Codesniffer File object
  * @return array
  */
 public function gatherErrors(PHP_CodeSniffer_File $file)
 {
     $foundErrors = $file->getErrors();
     $allErrors = array();
     foreach ($foundErrors as $line => $lineErrors) {
         foreach ($lineErrors as $column => $errors) {
             foreach ($errors as $error) {
                 if (!isset($allErrors[$line])) {
                     $allErrors[$line] = array();
                 }
                 $allErrors[$line][] = $error;
             }
         }
     }
     return $allErrors;
 }
 /**
  * Generate a list of test failures for a given sniffed file.
  *
  * @param PHP_CodeSniffer_File $file The file being tested.
  *
  * @return array
  * @throws PHP_CodeSniffer_Exception
  */
 public function generateFailureMessages(PHP_CodeSniffer_File $file)
 {
     $testFile = $file->getFilename();
     $foundErrors = $file->getErrors();
     $foundWarnings = $file->getWarnings();
     $expectedErrors = $this->getErrorList(basename($testFile));
     $expectedWarnings = $this->getWarningList(basename($testFile));
     if (is_array($expectedErrors) === false) {
         throw new PHP_CodeSniffer_Exception('getErrorList() must return an array');
     }
     if (is_array($expectedWarnings) === false) {
         throw new PHP_CodeSniffer_Exception('getWarningList() must return an array');
     }
     /*
      We merge errors and warnings together to make it easier
      to iterate over them and produce the errors string. In this way,
      we can report on errors and warnings in the same line even though
      it's not really structured to allow that.
     */
     $allProblems = array();
     $failureMessages = array();
     foreach ($foundErrors as $line => $lineErrors) {
         foreach ($lineErrors as $column => $errors) {
             if (isset($allProblems[$line]) === false) {
                 $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
             }
             $foundErrorsTemp = array();
             foreach ($allProblems[$line]['found_errors'] as $foundError) {
                 $foundErrorsTemp[] = $foundError;
             }
             $errorsTemp = array();
             foreach ($errors as $foundError) {
                 $errorsTemp[] = $foundError['message'] . ' (' . $foundError['source'] . ')';
             }
             $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp);
         }
         if (isset($expectedErrors[$line]) === true) {
             $allProblems[$line]['expected_errors'] = $expectedErrors[$line];
         } else {
             $allProblems[$line]['expected_errors'] = 0;
         }
         unset($expectedErrors[$line]);
     }
     //end foreach
     foreach ($expectedErrors as $line => $numErrors) {
         if (isset($allProblems[$line]) === false) {
             $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
         }
         $allProblems[$line]['expected_errors'] = $numErrors;
     }
     foreach ($foundWarnings as $line => $lineWarnings) {
         foreach ($lineWarnings as $column => $warnings) {
             if (isset($allProblems[$line]) === false) {
                 $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
             }
             $foundWarningsTemp = array();
             foreach ($allProblems[$line]['found_warnings'] as $foundWarning) {
                 $foundWarningsTemp[] = $foundWarning;
             }
             $warningsTemp = array();
             foreach ($warnings as $warning) {
                 $warningsTemp[] = $warning['message'] . ' (' . $warning['source'] . ')';
             }
             $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp);
         }
         if (isset($expectedWarnings[$line]) === true) {
             $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line];
         } else {
             $allProblems[$line]['expected_warnings'] = 0;
         }
         unset($expectedWarnings[$line]);
     }
     //end foreach
     foreach ($expectedWarnings as $line => $numWarnings) {
         if (isset($allProblems[$line]) === false) {
             $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
         }
         $allProblems[$line]['expected_warnings'] = $numWarnings;
     }
     // Order the messages by line number.
     ksort($allProblems);
     foreach ($allProblems as $line => $problems) {
         $numErrors = count($problems['found_errors']);
         $numWarnings = count($problems['found_warnings']);
         $expectedErrors = $problems['expected_errors'];
         $expectedWarnings = $problems['expected_warnings'];
         $errors = '';
         $foundString = '';
         if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) {
             $lineMessage = "[LINE {$line}]";
             $expectedMessage = 'Expected ';
             $foundMessage = 'in ' . basename($testFile) . ' but found ';
             if ($expectedErrors !== $numErrors) {
                 $expectedMessage .= "{$expectedErrors} error(s)";
                 $foundMessage .= "{$numErrors} error(s)";
                 if ($numErrors !== 0) {
                     $foundString .= 'error(s)';
                     $errors .= implode(PHP_EOL . ' -> ', $problems['found_errors']);
                 }
                 if ($expectedWarnings !== $numWarnings) {
                     $expectedMessage .= ' and ';
                     $foundMessage .= ' and ';
                     if ($numWarnings !== 0) {
                         if ($foundString !== '') {
                             $foundString .= ' and ';
                         }
                     }
                 }
             }
             if ($expectedWarnings !== $numWarnings) {
                 $expectedMessage .= "{$expectedWarnings} warning(s)";
                 $foundMessage .= "{$numWarnings} warning(s)";
                 if ($numWarnings !== 0) {
                     $foundString .= 'warning(s)';
                     if (empty($errors) === false) {
                         $errors .= PHP_EOL . ' -> ';
                     }
                     $errors .= implode(PHP_EOL . ' -> ', $problems['found_warnings']);
                 }
             }
             $fullMessage = "{$lineMessage} {$expectedMessage} {$foundMessage}.";
             if ($errors !== '') {
                 $fullMessage .= " The {$foundString} found were:" . PHP_EOL . " -> {$errors}";
             }
             $failureMessages[] = $fullMessage;
         }
         //end if
     }
     //end foreach
     return $failureMessages;
 }
 /**
  * Generate a list of test failures for a given sniffed file.
  *
  * @param PHP_CodeSniffer_File $file The file being tested.
  *
  * @return array
  * @throws PHP_CodeSniffer_Exception
  */
 public function generateFailureMessages(PHP_CodeSniffer_File $file)
 {
     $testFile = $file->getFilename();
     $foundErrors = $file->getErrors();
     $foundWarnings = $file->getWarnings();
     $expectedErrors = $this->getErrorList(basename($testFile));
     $expectedWarnings = $this->getWarningList(basename($testFile));
     if (!is_array($expectedErrors)) {
         throw new PHP_CodeSniffer_Exception('getErrorList() must return an array');
     }
     if (!is_array($expectedWarnings)) {
         throw new PHP_CodeSniffer_Exception('getWarningList() must return an array');
     }
     /*
      We merge errors and warnings together to make it easier
      to iterate over them and produce the errors string. In this way,
      we can report on errors and warnings in the same line even though
      it's not really structured to allow that.
     */
     $allProblems = [];
     $failureMessages = [];
     foreach ($foundErrors as $line => $lineErrors) {
         if (!array_key_exists($line, $allProblems)) {
             $allProblems[$line] = ['expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => [], 'found_warnings' => []];
         }
         foreach ($lineErrors as $column => $errors) {
             $errorsTemp = [];
             foreach ($errors as $foundError) {
                 $errorsTemp[] = $foundError['message'];
             }
             $allProblems[$line]['found_errors'] = array_merge($allProblems[$line]['found_errors'], $errorsTemp);
         }
         $allProblems[$line]['expected_errors'] = array_key_exists($line, $expectedErrors) ? $expectedErrors[$line] : 0;
         unset($expectedErrors[$line]);
     }
     foreach ($expectedErrors as $line => $numErrors) {
         if (!array_key_exists($line, $allProblems)) {
             $allProblems[$line] = ['expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => [], 'found_warnings' => []];
         }
         $allProblems[$line]['expected_errors'] = $numErrors;
     }
     foreach ($foundWarnings as $line => $lineWarnings) {
         if (!array_key_exists($line, $allProblems)) {
             $allProblems[$line] = ['expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => [], 'found_warnings' => []];
         }
         foreach ($lineWarnings as $column => $warnings) {
             $warningsTemp = [];
             foreach ($warnings as $warning) {
                 $warningsTemp[] = $warning['message'];
             }
             $allProblems[$line]['found_warnings'] = array_merge($allProblems[$line]['found_warnings'], $warningsTemp);
         }
         $allProblems[$line]['expected_warnings'] = array_key_exists($line, $expectedWarnings) ? $expectedWarnings[$line] : 0;
         unset($expectedWarnings[$line]);
     }
     foreach ($expectedWarnings as $line => $numWarnings) {
         if (!array_key_exists($line, $allProblems)) {
             $allProblems[$line] = ['expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => [], 'found_warnings' => []];
         }
         $allProblems[$line]['expected_warnings'] = $numWarnings;
     }
     ksort($allProblems);
     foreach ($allProblems as $line => $problems) {
         $numErrors = count($problems['found_errors']);
         $numWarnings = count($problems['found_warnings']);
         $expectedErrors = $problems['expected_errors'];
         $expectedWarnings = $problems['expected_warnings'];
         $errors = '';
         $foundString = '';
         if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) {
             $lineMessage = "[LINE {$line}]";
             $expectedMessage = 'Expected ';
             $foundMessage = 'in ' . basename($testFile) . ' but found ';
             if ($expectedErrors !== $numErrors) {
                 $expectedMessage .= "{$expectedErrors} error(s)";
                 $foundMessage .= "{$numErrors} error(s)";
                 if ($numErrors !== 0) {
                     $foundString .= 'error(s)';
                     $errors .= implode("\n -> ", $problems['found_errors']);
                 }
                 if ($expectedWarnings !== $numWarnings) {
                     $expectedMessage .= ' and ';
                     $foundMessage .= ' and ';
                     if ($numWarnings !== 0 && $foundString !== '') {
                         $foundString .= ' and ';
                     }
                 }
             }
             if ($expectedWarnings !== $numWarnings) {
                 $expectedMessage .= "{$expectedWarnings} warning(s)";
                 $foundMessage .= "{$numWarnings} warning(s)";
                 if ($numWarnings !== 0) {
                     $foundString .= 'warning(s)';
                     if ($errors !== '') {
                         $errors .= "\n -> ";
                     }
                     $errors .= implode("\n -> ", $problems['found_warnings']);
                 }
             }
             $fullMessage = "{$lineMessage} {$expectedMessage} {$foundMessage}.";
             if ($errors !== '') {
                 $fullMessage .= " The {$foundString} found were:\n -> {$errors}";
             }
             $failureMessages[] = $fullMessage;
         }
     }
     return $failureMessages;
 }
Example #6
0
 /**
  * Pre-process and package violations for all files.
  *
  * Used by error reports to get a packaged list of all errors in each file.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
  *
  * @return array
  */
 public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
 {
     $report = array('filename' => $phpcsFile->getFilename(), 'errors' => $phpcsFile->getErrorCount(), 'warnings' => $phpcsFile->getWarningCount(), 'fixable' => $phpcsFile->getFixableCount(), 'messages' => array());
     if ($report['errors'] === 0 && $report['warnings'] === 0) {
         // Prefect score!
         return $report;
     }
     $errors = array();
     // Merge errors and warnings.
     foreach ($phpcsFile->getErrors() as $line => $lineErrors) {
         if (is_array($lineErrors) === false) {
             continue;
         }
         foreach ($lineErrors as $column => $colErrors) {
             $newErrors = array();
             foreach ($colErrors as $data) {
                 $newErrors[] = array('message' => $data['message'], 'source' => $data['source'], 'severity' => $data['severity'], 'fixable' => $data['fixable'], 'type' => 'ERROR');
             }
             //end foreach
             $errors[$line][$column] = $newErrors;
         }
         //end foreach
         ksort($errors[$line]);
     }
     //end foreach
     foreach ($phpcsFile->getWarnings() as $line => $lineWarnings) {
         if (is_array($lineWarnings) === false) {
             continue;
         }
         foreach ($lineWarnings as $column => $colWarnings) {
             $newWarnings = array();
             foreach ($colWarnings as $data) {
                 $newWarnings[] = array('message' => $data['message'], 'source' => $data['source'], 'severity' => $data['severity'], 'fixable' => $data['fixable'], 'type' => 'WARNING');
             }
             //end foreach
             if (isset($errors[$line]) === false) {
                 $errors[$line] = array();
             }
             if (isset($errors[$line][$column]) === true) {
                 $errors[$line][$column] = array_merge($newWarnings, $errors[$line][$column]);
             } else {
                 $errors[$line][$column] = $newWarnings;
             }
         }
         //end foreach
         ksort($errors[$line]);
     }
     //end foreach
     ksort($errors);
     $report['messages'] = $errors;
     return $report;
 }
 /**
  * Generate a list of test failures for a given sniffed file.
  *
  * @param PHP_CodeSniffer_File $file The file being tested.
  *
  * @return array
  *
  * @throws PHP_CodeSniffer_Exception When the getErrorList() or getWarningList() return value is invalid.
  *
  * @SuppressWarnings(PHPMD.Superglobals)
  * @SuppressWarnings(PHPMD.CamelCaseVariableName)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function generateFailureMessages(PHP_CodeSniffer_File $file)
 {
     $testFile = $file->getFilename();
     $foundErrors = $file->getErrors();
     $foundWarnings = $file->getWarnings();
     $expectedErrors = $this->getErrorList(basename($testFile));
     $expectedWarnings = $this->getWarningList(basename($testFile));
     if (is_array($expectedErrors) === false) {
         throw new PHP_CodeSniffer_Exception('getErrorList() must return an array');
     }
     if (is_array($expectedWarnings) === false) {
         throw new PHP_CodeSniffer_Exception('getWarningList() must return an array');
     }
     /*
         We merge errors and warnings together to make it easier
         to iterate over them and produce the errors string. In this way,
         we can report on errors and warnings in the same line even though
         it's not really structured to allow that.
     */
     $allProblems = array();
     $failureMessages = array();
     foreach ($foundErrors as $line => $lineErrors) {
         foreach ($lineErrors as $column => $errors) {
             if (isset($allProblems[$line]) === false) {
                 $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
             }
             $foundErrorsTemp = array();
             foreach ($allProblems[$line]['found_errors'] as $foundError) {
                 $foundErrorsTemp[] = $foundError;
             }
             $errorsTemp = array();
             foreach ($errors as $foundError) {
                 $errorsTemp[] = $foundError['message'] . ' (' . $foundError['source'] . ')';
                 $source = $foundError['source'];
                 if (in_array($source, $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES']) === false) {
                     $GLOBALS['PHP_CODESNIFFER_SNIFF_CODES'][] = $source;
                 }
                 if ($foundError['fixable'] === true && in_array($source, $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES']) === false) {
                     $GLOBALS['PHP_CODESNIFFER_FIXABLE_CODES'][] = $source;
                 }
             }
             $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp);
         }
         if (isset($expectedErrors[$line]) === true) {
             $allProblems[$line]['expected_errors'] = $expectedErrors[$line];
         } else {
             $allProblems[$line]['expected_errors'] = 0;
         }
         unset($expectedErrors[$line]);
     }
     foreach ($expectedErrors as $line => $numErrors) {
         if (isset($allProblems[$line]) === false) {
             $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
         }
         $allProblems[$line]['expected_errors'] = $numErrors;
     }
     foreach ($foundWarnings as $line => $lineWarnings) {
         foreach ($lineWarnings as $column => $warnings) {
             if (isset($allProblems[$line]) === false) {
                 $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
             }
             $foundWarningsTemp = array();
             foreach ($allProblems[$line]['found_warnings'] as $foundWarning) {
                 $foundWarningsTemp[] = $foundWarning;
             }
             $warningsTemp = array();
             foreach ($warnings as $warning) {
                 $warningsTemp[] = $warning['message'] . ' (' . $warning['source'] . ')';
             }
             $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp);
         }
         if (isset($expectedWarnings[$line]) === true) {
             $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line];
         } else {
             $allProblems[$line]['expected_warnings'] = 0;
         }
         unset($expectedWarnings[$line]);
     }
     foreach ($expectedWarnings as $line => $numWarnings) {
         if (isset($allProblems[$line]) === false) {
             $allProblems[$line] = array('expected_errors' => 0, 'expected_warnings' => 0, 'found_errors' => array(), 'found_warnings' => array());
         }
         $allProblems[$line]['expected_warnings'] = $numWarnings;
     }
     // Order the messages by line number.
     ksort($allProblems);
     foreach ($allProblems as $line => $problems) {
         $numErrors = count($problems['found_errors']);
         $numWarnings = count($problems['found_warnings']);
         $expectedErrors = $problems['expected_errors'];
         $expectedWarnings = $problems['expected_warnings'];
         $errors = '';
         $foundString = '';
         try {
             $this->assertSame($expectedErrors, $numErrors);
         } catch (PHPUnit_Framework_ExpectationFailedException $exception) {
             // Silence it, we dump the errors below.
         }
         try {
             $this->assertSame($expectedWarnings, $numWarnings);
         } catch (PHPUnit_Framework_ExpectationFailedException $exception) {
             // Silence it, we dump the errors below.
         }
         if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) {
             $lineMessage = sprintf('[LINE %s]', $line);
             $expectedMessage = 'Expected ';
             $foundMessage = 'in ' . basename($testFile) . ' but found ';
             if ($expectedErrors !== $numErrors) {
                 $expectedMessage .= sprintf('%s error(s)', $expectedErrors);
                 $foundMessage .= sprintf('%s error(s)', $numErrors);
                 if ($numErrors !== 0) {
                     $foundString .= 'error(s)';
                     $errors .= implode(PHP_EOL . ' -> ', $problems['found_errors']);
                 }
                 if ($expectedWarnings !== $numWarnings) {
                     $expectedMessage .= ' and ';
                     $foundMessage .= ' and ';
                     if ($numWarnings !== 0) {
                         if ($foundString !== '') {
                             $foundString .= ' and ';
                         }
                     }
                 }
             }
             if ($expectedWarnings !== $numWarnings) {
                 $expectedMessage .= sprintf('%s warning(s)', $expectedWarnings);
                 $foundMessage .= sprintf('%s warning(s)', $numWarnings);
                 if ($numWarnings !== 0) {
                     $foundString .= 'warning(s)';
                     if (empty($errors) === false) {
                         $errors .= PHP_EOL . ' -> ';
                     }
                     $errors .= implode(PHP_EOL . ' -> ', $problems['found_warnings']);
                 }
             }
             $fullMessage = sprintf('%s %s %s.', $lineMessage, $expectedMessage, $foundMessage);
             if ($errors !== '') {
                 $fullMessage .= sprintf(' The %s found were:' . PHP_EOL . ' -> %s', $foundString, $errors);
             }
             $failureMessages[] = $fullMessage;
         }
     }
     return $failureMessages;
 }
 protected function fileHasExpectedErrors(\PHP_CodeSniffer_File $sniffedFile, array $expectedProblems)
 {
     $this->problemsMatchExpected($expectedProblems, $sniffedFile->getErrors(), 'error');
 }
Example #9
0
 /**
  * Get simple result of processed file
  *
  * @param \PHP_CodeSniffer_File $phpcsFile
  * @return array
  */
 protected function getFileResult($phpcsFile)
 {
     $report = array();
     if ($phpcsFile && ($phpcsFile->getErrorCount() || $phpcsFile->getWarningCount())) {
         $report = array('errors' => $phpcsFile->getErrors(), 'warnings' => $phpcsFile->getWarnings());
     }
     return $report;
 }
 /**
  * Generate a list of test failures for a given sniffed file.
  *
  * @param PHP_CodeSniffer_File $file The file being tested.
  *
  * @return array
  * @throws PHP_CodeSniffer_Exception
  */
 public function generateFailureMessages(\PHP_CodeSniffer_File $file)
 {
     $testFile = $file->getFilename();
     $foundErrors = $file->getErrors();
     $foundWarnings = $file->getWarnings();
     $expectedErrors = $this->getErrorList(basename($testFile));
     $expectedWarnings = $this->getWarningList(basename($testFile));
     if (is_array($expectedErrors) === false) {
         throw new \PHP_CodeSniffer_Exception('getErrorList() must return an array');
     }
     if (is_array($expectedWarnings) === false) {
         throw new \PHP_CodeSniffer_Exception('getWarningList() must return an array');
     }
     $failureMessages = [];
     $failureMessages = array_merge($failureMessages, self::validateFailures($foundErrors, $expectedErrors, basename($testFile), 'error'));
     $failureMessages = array_merge($failureMessages, self::validateFailures($foundWarnings, $expectedWarnings, basename($testFile), 'warning'));
     return $failureMessages;
 }