If the extension is one of the defaults, a specific tokenizer
will be used. Otherwise, the PHP tokenizer will be used for
all extensions passed.
public setAllowedFileExtensions ( array $extensions ) : void | ||
$extensions | array | An array of file extensions. |
Результат | void |
/** * Runs PHP_CodeSniffer over files and directories. * * @param array $values An array of values determined from CLI args. * * @return int The number of error and warning messages shown. * @see getCommandLineValues() */ public function process($values = array()) { if (empty($values) === true) { $values = $this->getCommandLineValues(); } else { $values = array_merge($this->getDefaults(), $values); $this->values = $values; } if ($values['generator'] !== '') { $phpcs = new PHP_CodeSniffer($values['verbosity']); if ($values['standard'] === null) { $values['standard'] = $this->validateStandard(null); } foreach ($values['standard'] as $standard) { $phpcs->generateDocs($standard, $values['sniffs'], $values['generator']); } exit(0); } // If no standard is supplied, get the default. $values['standard'] = $this->validateStandard($values['standard']); foreach ($values['standard'] as $standard) { if (PHP_CodeSniffer::isInstalledStandard($standard) === false) { // They didn't select a valid coding standard, so help them // out by letting them know which standards are installed. echo 'ERROR: the "' . $standard . '" coding standard is not installed. '; $this->printInstalledStandards(); exit(2); } } if ($values['explain'] === true) { foreach ($values['standard'] as $standard) { $this->explainStandard($standard); } exit(0); } $phpcs = new PHP_CodeSniffer($values['verbosity'], null, null, null); $phpcs->setCli($this); $phpcs->initStandard($values['standard'], $values['sniffs']); $values = $this->values; $phpcs->setTabWidth($values['tabWidth']); $phpcs->setEncoding($values['encoding']); $phpcs->setInteractive($values['interactive']); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (empty($values['extensions']) === false) { $phpcs->setAllowedFileExtensions($values['extensions']); } // Set ignore patterns if they were specified. if (empty($values['ignored']) === false) { $ignorePatterns = array_merge($phpcs->getIgnorePatterns(), $values['ignored']); $phpcs->setIgnorePatterns($ignorePatterns); } // Set some convenience member vars. if ($values['errorSeverity'] === null) { $this->errorSeverity = PHPCS_DEFAULT_ERROR_SEV; } else { $this->errorSeverity = $values['errorSeverity']; } if ($values['warningSeverity'] === null) { $this->warningSeverity = PHPCS_DEFAULT_WARN_SEV; } else { $this->warningSeverity = $values['warningSeverity']; } if (empty($values['reports']) === true) { $values['reports']['full'] = $values['reportFile']; $this->values['reports'] = $values['reports']; } // Include bootstrap files. foreach ($values['bootstrap'] as $bootstrap) { include $bootstrap; } $phpcs->processFiles($values['files'], $values['local']); if (empty($values['files']) === true || $values['stdin'] !== null) { $fileContents = $values['stdin']; if ($fileContents === null) { // Check if they are passing in the file contents. $handle = fopen('php://stdin', 'r'); stream_set_blocking($handle, true); $fileContents = stream_get_contents($handle); fclose($handle); } if ($fileContents === '') { // No files and no content passed in. echo 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL; $this->printUsage(); exit(2); } else { $phpcs->processFile('STDIN', $fileContents); } } // Interactive runs don't require a final report and it doesn't really // matter what the retun value is because we know it isn't being read // by a script. if ($values['interactive'] === true) { return 0; } return $this->printErrorReport($phpcs, $values['reports'], $values['showSources'], $values['reportFile'], $values['reportWidth']); }
/** * Runs PHP_CodeSniffer over files and directories. * * @param array $values An array of values determined from CLI args. * * @return int The number of error and warning messages shown. * @see getCommandLineValues() */ public function process($values = array()) { if (empty($values) === true) { $values = $this->getCommandLineValues(); } if ($values['generator'] !== '') { $phpcs = new PHP_CodeSniffer($values['verbosity']); $phpcs->generateDocs($values['standard'], $values['files'], $values['generator']); exit(0); } $fileContents = ''; if (empty($values['files']) === true) { // Check if they passing in the file contents. $handle = fopen('php://stdin', 'r'); $fileContents = stream_get_contents($handle); fclose($handle); if ($fileContents === '') { // No files and no content passed in. echo 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL; $this->printUsage(); exit(2); } } $values['standard'] = $this->validateStandard($values['standard']); if (PHP_CodeSniffer::isInstalledStandard($values['standard']) === false) { // They didn't select a valid coding standard, so help them // out by letting them know which standards are installed. echo 'ERROR: the "' . $values['standard'] . '" coding standard is not installed. '; $this->printInstalledStandards(); exit(2); } $phpcs = new PHP_CodeSniffer($values['verbosity'], $values['tabWidth'], $values['encoding'], $values['interactive']); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (empty($values['extensions']) === false) { $phpcs->setAllowedFileExtensions($values['extensions']); } // Set ignore patterns if they were specified. if (empty($values['ignored']) === false) { $phpcs->setIgnorePatterns($values['ignored']); } // Set some convenience member vars. if ($values['errorSeverity'] === null) { $this->errorSeverity = PHPCS_DEFAULT_ERROR_SEV; } else { $this->errorSeverity = $values['errorSeverity']; } if ($values['warningSeverity'] === null) { $this->warningSeverity = PHPCS_DEFAULT_WARN_SEV; } else { $this->warningSeverity = $values['warningSeverity']; } $phpcs->setCli($this); $phpcs->process($values['files'], $values['standard'], $values['sniffs'], $values['local']); if ($fileContents !== '') { $phpcs->processFile('STDIN', $fileContents); } return $this->printErrorReport($phpcs, $values['reports'], $values['showSources'], $values['reportFile'], $values['reportWidth']); }
/** * Executes PHP code sniffer against PhingFile or a FileSet */ public function main() { if (!class_exists('PHP_CodeSniffer')) { include_once 'PHP/CodeSniffer.php'; } if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PhpCodeSnifferTask_FormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } if (!isset($this->file)) { $fileList = array(); $project = $this->getProject(); foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($project); $files = $ds->getIncludedFiles(); $dir = $fs->getDir($this->project)->getAbsolutePath(); foreach ($files as $file) { $fileList[] = $dir . DIRECTORY_SEPARATOR . $file; } } } $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth); $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions); if (is_array($this->ignorePatterns)) { $codeSniffer->setIgnorePatterns($this->ignorePatterns); } foreach ($this->configData as $configData) { $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true); } if ($this->file instanceof PhingFile) { $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories); } else { $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); } $report = $this->printErrorReport($codeSniffer); // generate the documentation if ($this->docGenerator !== '' && $this->docFile !== null) { ob_start(); $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); $output = ob_get_contents(); ob_end_clean(); // write to file $outputFile = $this->docFile->getPath(); $check = file_put_contents($outputFile, $output); if (is_bool($check) && !$check) { throw new BuildException('Error writing doc to ' . $outputFile); } } elseif ($this->docGenerator !== '' && $this->docFile === null) { $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); } if ($this->haltonerror && $report['totals']['errors'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : '')); } if ($this->haltonwarning && $report['totals']['warnings'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : '')); } }
/** * Executes PHP code sniffer against PhingFile or a FileSet */ public function main() { if (!class_exists('PHP_CodeSniffer')) { @(include_once 'PHP/CodeSniffer.php'); if (!class_exists('PHP_CodeSniffer')) { throw new BuildException("This task requires the PHP_CodeSniffer package installed and available on the include path", $this->getLocation()); } } /** * Determine PHP_CodeSniffer version number */ if (!$this->skipVersionCheck) { preg_match('/\\d\\.\\d\\.\\d/', shell_exec('phpcs --version'), $version); if (version_compare($version[0], '1.2.2') < 0) { throw new BuildException('PhpCodeSnifferTask requires PHP_CodeSniffer version >= 1.2.2', $this->getLocation()); } } if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (PHP_CodeSniffer::isInstalledStandard($this->standard) === false) { // They didn't select a valid coding standard, so help them // out by letting them know which standards are installed. $installedStandards = PHP_CodeSniffer::getInstalledStandards(); $numStandards = count($installedStandards); $errMsg = ''; if ($numStandards === 0) { $errMsg = 'No coding standards are installed.'; } else { $lastStandard = array_pop($installedStandards); if ($numStandards === 1) { $errMsg = 'The only coding standard installed is ' . $lastStandard; } else { $standardList = implode(', ', $installedStandards); $standardList .= ' and ' . $lastStandard; $errMsg = 'The installed coding standards are ' . $standardList; } } throw new BuildException('ERROR: the "' . $this->standard . '" coding standard is not installed. ' . $errMsg, $this->getLocation()); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PhpCodeSnifferTask_FormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } if (!isset($this->file)) { $fileList = array(); $project = $this->getProject(); foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($project); $files = $ds->getIncludedFiles(); $dir = $fs->getDir($this->project)->getAbsolutePath(); foreach ($files as $file) { $fileList[] = $dir . DIRECTORY_SEPARATOR . $file; } } } $cwd = getcwd(); // Save command line arguments because it confuses PHPCS (version 1.3.0) $oldArgs = $_SERVER['argv']; $_SERVER['argv'] = array(); $_SERVER['argc'] = 0; $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth, $this->encoding); $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions); if (is_array($this->ignorePatterns)) { $codeSniffer->setIgnorePatterns($this->ignorePatterns); } foreach ($this->configData as $configData) { $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true); } if ($this->file instanceof PhingFile) { $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories); } else { $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); } // Restore command line arguments $_SERVER['argv'] = $oldArgs; $_SERVER['argc'] = count($oldArgs); chdir($cwd); $report = $this->printErrorReport($codeSniffer); // generate the documentation if ($this->docGenerator !== '' && $this->docFile !== null) { ob_start(); $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); $output = ob_get_contents(); ob_end_clean(); // write to file $outputFile = $this->docFile->getPath(); $check = file_put_contents($outputFile, $output); if (is_bool($check) && !$check) { throw new BuildException('Error writing doc to ' . $outputFile); } } elseif ($this->docGenerator !== '' && $this->docFile === null) { $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); } if ($this->haltonerror && $report['totals']['errors'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : '')); } if ($this->haltonwarning && $report['totals']['warnings'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : '')); } }
/** * Executes PHP code sniffer against PhingFile or a FileSet */ public function main() { if (!class_exists('PHP_CodeSniffer')) { include_once 'PHP/CodeSniffer.php'; } /** * Determine PHP_CodeSniffer version number */ if (!$this->skipVersionCheck) { preg_match('/\\d\\.\\d\\.\\d/', shell_exec('phpcs --version'), $version); if (version_compare($version[0], '1.2.2') < 0) { throw new BuildException('PhpCodeSnifferTask requires PHP_CodeSniffer version >= 1.2.2', $this->getLocation()); } } if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PhpCodeSnifferTask_FormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } if (!isset($this->file)) { $fileList = array(); $project = $this->getProject(); foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($project); $files = $ds->getIncludedFiles(); $dir = $fs->getDir($this->project)->getAbsolutePath(); foreach ($files as $file) { $fileList[] = $dir . DIRECTORY_SEPARATOR . $file; } } } $cwd = getcwd(); // Save command line arguments because it confuses PHPCS (version 1.3.0) $oldArgs = $_SERVER['argv']; $_SERVER['argv'] = array(); $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth); $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions); if (is_array($this->ignorePatterns)) { $codeSniffer->setIgnorePatterns($this->ignorePatterns); } foreach ($this->configData as $configData) { $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true); } if ($this->file instanceof PhingFile) { $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories); } else { $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); } // Restore command line arguments $_SERVER['argv'] = $oldArgs; chdir($cwd); $report = $this->printErrorReport($codeSniffer); // generate the documentation if ($this->docGenerator !== '' && $this->docFile !== null) { ob_start(); $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); $output = ob_get_contents(); ob_end_clean(); // write to file $outputFile = $this->docFile->getPath(); $check = file_put_contents($outputFile, $output); if (is_bool($check) && !$check) { throw new BuildException('Error writing doc to ' . $outputFile); } } elseif ($this->docGenerator !== '' && $this->docFile === null) { $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator); } if ($this->haltonerror && $report['totals']['errors'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : '')); } if ($this->haltonwarning && $report['totals']['warnings'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : '')); } }
/** * Runs PHP_CodeSniffer over files are directories. * * @param array $values An array of values determined from CLI args. * * @return int The number of error and warning messages shown. * @see getCommandLineValues() */ public function process($values = array()) { if (empty($values) === true) { $values = $this->getCommandLineValues(); } if ($values['generator'] !== '') { $phpcs = new PHP_CodeSniffer($values['verbosity']); $phpcs->generateDocs($values['standard'], $values['files'], $values['generator']); exit(0); } if (empty($values['files']) === true) { echo 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL; $this->printUsage(); exit(2); } $values['standard'] = $this->validateStandard($values['standard']); if (PHP_CodeSniffer::isInstalledStandard($values['standard']) === false) { // They didn't select a valid coding standard, so help them // out by letting them know which standards are installed. echo 'ERROR: the "' . $values['standard'] . '" coding standard is not installed. '; $this->printInstalledStandards(); exit(2); } $phpcs = new PHP_CodeSniffer($values['verbosity'], $values['tabWidth']); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (empty($values['extensions']) === false) { $phpcs->setAllowedFileExtensions($values['extensions']); } // Set ignore patterns if they were specified. if (empty($values['ignored']) === false) { $phpcs->setIgnorePatterns($values['ignored']); } $phpcs->process($values['files'], $values['standard'], $values['sniffs'], $values['local']); return $this->printErrorReport($phpcs, $values['report'], $values['showWarnings'], $values['showSources'], $values['reportFile']); }
private function process($files, $interactive = true) { $standards = 'Webasyst'; if (PHP_CodeSniffer::isInstalledStandard($standards) === false) { $this->tracef('WARNING: %s standard not found, will used PSR2. Some rules are differ', $standards); $standards = 'PSR2'; } if (is_array($standards) === false) { $standards = array($standards); } $phpcs = new PHP_CodeSniffer(0, 4, 'UTF-8', $interactive); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (true) { $extensions = array('php', 'js', 'css'); $phpcs->setAllowedFileExtensions($extensions); } if (is_array($files) === false) { $files = array($files); } // Reset the members. // Ensure this option is enabled or else line endings will not always // be detected properly for files created on a Mac with the /r line ending. ini_set('auto_detect_line_endings', true); $sniffs = array(); foreach ($standards as $standard) { $installed = $phpcs->getInstalledStandardPath($standard); if ($installed !== null) { $standard = $installed; } else { if (is_dir($standard) === true && is_file(realpath($standard . '/ruleset.xml')) === true) { $standard = realpath($standard . '/ruleset.xml'); } } $sniffs = array_merge($sniffs, $phpcs->processRuleset($standard)); } //end foreach $sniffRestrictions = array(); $phpcs->registerSniffs($sniffs, $sniffRestrictions); $phpcs->populateTokenListeners(); // The SVN pre-commit calls process() to init the sniffs // and ruleset so there may not be any files to process. // But this has to come after that initial setup. //define('PHP_CODESNIFFER_IN_TESTS',true); $_SERVER['argc'] = 0; $errors_count = 0; foreach ($files as $file) { $phpcsFile = $phpcs->processFile($file); // Show progress information. if ($phpcsFile !== null) { $count = $phpcsFile->getErrorCount() + $phpcsFile->getWarningCount(); if (!$interactive && $count) { $report = array('ERROR' => $phpcsFile->getErrors(), 'WARNING' => $phpcsFile->getWarnings()); $this->tracef("\nFILE: %s", str_replace($this->path . '/', '', $file)); $this->trace(str_repeat('-', 80)); foreach ($report as $type => $errors) { foreach ($errors as $line => $line_errors) { foreach ($line_errors as $column => $errors) { foreach ($errors as $error) { $this->tracef('%4d | %s | %s', $line, $type, $error['message']); } } } } $this->trace(str_repeat('-', 80)); } $errors_count += $count; } } return $errors_count; }
/** * Executes PHP code sniffer against PhingFile or a FileSet */ public function main() { if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } require_once 'PHP/CodeSniffer.php'; $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth); $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions); if (is_array($this->ignorePatterns)) { $codeSniffer->setIgnorePatterns($this->ignorePatterns); } foreach ($this->configData as $configData) { $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true); } if ($this->file instanceof PhingFile) { $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories); } else { $fileList = array(); $project = $this->getProject(); foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($project); $files = $ds->getIncludedFiles(); $dir = $fs->getDir($this->project)->getPath(); foreach ($files as $file) { $fileList[] = $dir . DIRECTORY_SEPARATOR . $file; } } $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); } $this->output($codeSniffer); }
/** * @link http://www.webasyst.ru/developers/docs/basics/naming-rules/ */ private function codeStyle($param) { error_reporting(E_ALL | E_STRICT); $files = array(); $ext = array('php', 'js', 'html', 'css'); foreach ($this->files as $file) { if (in_array(pathinfo($file, PATHINFO_EXTENSION), $ext)) { if ($param === 'vendors' && preg_match('@^(lib/)?vendors/@', $file)) { continue; } if (preg_match('@\\.min\\.(js|css)$@', $file)) { continue; } $files[] = $this->path . '/' . $file; } } include_once 'PHP/CodeSniffer.php'; if (!class_exists('PHP_CodeSniffer')) { $this->trace('WARNING: Code style check skipped:'); $this->trace(' PEAR extension CodeSniffer required'); return; } $values = array('verbosity' => 0, 'tabWidth' => 4, 'encoding' => 'UTF-8', 'interactive' => true, 'files' => $files, 'standard' => 'Webasyst', 'sniffs' => array(), 'local' => true, 'reports' => array('full' => null), 'extensions' => array('php', 'html', 'js', 'css')); if (PHP_CodeSniffer::isInstalledStandard($values['standard']) === false) { $this->tracef('WARNING: %s standard not found, will used PSR2. Some rules are differ', $values['standard']); $values['standard'] = 'PSR2'; } $phpcs = new PHP_CodeSniffer($values['verbosity'], $values['tabWidth'], $values['encoding'], $values['interactive']); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (empty($values['extensions']) === false) { $phpcs->setAllowedFileExtensions($values['extensions']); } try { $phpcs->process($values['files'], $values['standard'], $values['sniffs'], $values['local']); } catch (Exception $ex) { $this->tracef('ERROR: %s', $ex->getMessage()); } }
private function checkCodingStandard() { require_once __DIR__ . '/vendor/PHP_CodeSniffer-2.2.0/CodeSniffer.php'; $phpcs = new \PHP_CodeSniffer(); $arguments = array('--standard=CodeIgniter', '-s'); $_SERVER['argv'] = isset($_SERVER['argv']) ? $_SERVER['argv'] + $arguments : $arguments; $phpcs->initStandard('CodeIgniter'); $phpcs->setAllowedFileExtensions(array('PHP')); $folders = array('controllers', 'libraries', 'models', 'helpers'); $loadedFiles = array_merge(array($this->ci->router->class), array_keys($this->loadLibraries()), $this->ci->load->get_models(), array_keys($this->ci->load->get_helpers())); array_walk($loadedFiles, function (&$item) { $item = strtolower($item) . '.php'; }); $reports = array(); foreach ($folders as $folder) { $path = APPPATH . $folder . '/'; $directoryIterator = new \RecursiveDirectoryIterator($path); foreach ($directoryIterator as $fileInfo) { if ($fileInfo->isFile() && in_array(strtolower($fileInfo->getFileName()), $loadedFiles)) { $phpcsFile = $phpcs->processFile($fileInfo->getPathName()); $reportData = $phpcs->reporting->prepareFileReport($phpcsFile); if (count($reportData['messages'])) { foreach ($reportData['messages'] as $line => $_messages) { foreach ($_messages as $message) { foreach ($message as $msg) { $reports[] = array('filename' => str_replace(str_replace('\\', '/', FCPATH), '', str_replace('\\', '/', $reportData['filename'])), 'filepath' => $reportData['filename'], 'severity' => $msg['type'], 'line' => $line, 'message' => $msg['message']); } } } } } } } return $reports; }
/** * Executes PHP code sniffer against PhingFile or a FileSet */ public function main() { if (!isset($this->file) and count($this->filesets) == 0) { throw new BuildException("Missing either a nested fileset or attribute 'file' set"); } if (count($this->formatters) == 0) { // turn legacy format attribute into formatter $fmt = new PhpCodeSnifferTask_FormatterElement(); $fmt->setType($this->format); $fmt->setUseFile(false); $this->formatters[] = $fmt; } if (!isset($this->file)) { $fileList = array(); $project = $this->getProject(); foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($project); $files = $ds->getIncludedFiles(); $dir = $fs->getDir($this->project)->getAbsolutePath(); foreach ($files as $file) { $fileList[] = $dir . DIRECTORY_SEPARATOR . $file; } } } /* save current directory */ $old_cwd = getcwd(); require_once 'PHP/CodeSniffer.php'; $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth); $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions); if (is_array($this->ignorePatterns)) { $codeSniffer->setIgnorePatterns($this->ignorePatterns); } foreach ($this->configData as $configData) { $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true); } if ($this->file instanceof PhingFile) { $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories); } else { $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); } $this->output($codeSniffer); $report = $codeSniffer->prepareErrorReport(true); if ($this->haltonerror && $report['totals']['errors'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : '')); } if ($this->haltonwarning && $report['totals']['warnings'] > 0) { throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : '')); } /* reset current directory */ chdir($old_cwd); }