protected static function analyze(array $sniffs, $files) { $sniffs = array_map(static function ($sniff) { $sniff = __DIR__ . '/../../../../src/' . $sniff . '.php'; static::assertFileExists($sniff, 'Sniff does not exist'); return $sniff; }, $sniffs); $files = array_map(static function ($file) { static::assertFileExists($file, 'Source file does not exists'); return $file; }, (array) $files); $codeSniffer = new PHP_CodeSniffer(); $codeSniffer->registerSniffs($sniffs, []); $codeSniffer->populateTokenListeners(); $report = []; foreach ($files as $file) { $phpcsFile = $codeSniffer->processFile($file); $report[$file] = []; $report[$file]['numWarnings'] = $phpcsFile->getWarningCount(); $report[$file]['warnings'] = $phpcsFile->getWarnings(); $report[$file]['numErrors'] = $phpcsFile->getErrorCount(); $report[$file]['errors'] = $phpcsFile->getErrors(); } return $report; }
/** * Rebuilds the list of listeners to ensure their state is cleared. * * @return void */ public function refreshTokenListeners() { $this->phpcs->populateTokenListeners(); $this->_listeners = $this->phpcs->getTokenSniffs(); }
/** * Test suppressing a whole file. * * @return void */ public function testSuppressFile() { $phpcs = new PHP_CodeSniffer(); $phpcs->setTokenListeners('Squiz', array('Generic_Sniffs_Commenting_TodoSniff')); $phpcs->populateTokenListeners(); // Process without suppression. $content = '<?php ' . PHP_EOL . '//TODO: write some code'; $phpcs->processFile('noSuppressionTest.php', $content); $files = $phpcs->getFiles(); $file = $files[0]; $warnings = $file->getWarnings(); $numWarnings = $file->getWarningCount(); $this->assertEquals(1, $numWarnings); $this->assertEquals(1, count($warnings)); // Process with suppression. $content = '<?php ' . PHP_EOL . '// @codingStandardsIgnoreFile' . PHP_EOL . '//TODO: write some code'; $phpcs->processFile('suppressionTest.php', $content); $files = $phpcs->getFiles(); $file = $files[1]; $warnings = $file->getWarnings(); $numWarnings = $file->getWarningCount(); $this->assertEquals(0, $numWarnings); $this->assertEquals(0, count($warnings)); }
/** * Test suppressing a whole file. * * @return void */ public function testSuppressFile() { $phpcs = new PHP_CodeSniffer(); $phpcs->setTokenListeners('Squiz', array('Squiz_Sniffs_Commenting_FileCommentSniff')); $phpcs->populateTokenListeners(); // Process without suppression. $content = '<?php ' . PHP_EOL . '$var = FALSE;'; $phpcs->processFile('noSuppressionTest.php', $content); $files = $phpcs->getFiles(); $file = $files[0]; $errors = $file->getErrors(); $numErrors = $file->getErrorCount(); $this->assertEquals(1, $numErrors); $this->assertEquals(1, count($errors)); $this->assertEquals(1, count($files)); // Process with suppression. $content = '<?php ' . PHP_EOL . '// @codingStandardsIgnoreFile' . PHP_EOL . '$var = FALSE;'; $phpcs->processFile('suppressionTest.php', $content); // The file shouldn't even be added to the $files array. $files = $phpcs->getFiles(); $this->assertEquals(1, count($files)); }
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; }