/** * Prints a report showing the sniffs contained in a standard. * * @param string $standard The standard to validate. * * @return void */ public function explainStandard($standard) { $phpcs = new PHP_CodeSniffer(); $phpcs->setTokenListeners($standard); $sniffs = $phpcs->getSniffs(); $sniffs = array_keys($sniffs); sort($sniffs); ob_start(); $lastStandard = ''; $lastCount = ''; $sniffCount = count($sniffs); $sniffs[] = '___'; echo PHP_EOL . "The {$standard} standard contains {$sniffCount} sniffs" . PHP_EOL; ob_start(); foreach ($sniffs as $sniff) { $parts = explode('_', str_replace('\\', '_', $sniff)); if ($lastStandard === '') { $lastStandard = $parts[0]; } if ($parts[0] !== $lastStandard) { $sniffList = ob_get_contents(); ob_end_clean(); echo PHP_EOL . $lastStandard . ' (' . $lastCount . ' sniffs)' . PHP_EOL; echo str_repeat('-', strlen($lastStandard . $lastCount) + 10); echo PHP_EOL; echo $sniffList; $lastStandard = $parts[0]; $lastCount = 0; ob_start(); } echo ' ' . $parts[0] . '.' . $parts[2] . '.' . substr($parts[3], 0, -5) . PHP_EOL; $lastCount++; } //end foreach ob_end_clean(); }
/** * 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)); }