/** * PHPca's main method. Returns a result object holding * error and warning messages for all the files that have been analyzed. * * @param string $pathToPhpExecutable path to PHP executable for lint check * @param string $fileOrDirectory path to file or directory to check * @return object */ public function run($pathToPhpExecutable, $fileOrDirectory, Configuration $configuration = null) { if ($pathToPhpExecutable == '') { throw new Exception('No path to PHP executable specified'); } if ($fileOrDirectory == '') { throw new Exception('No file or directory to analyze'); } if (!is_null($configuration)) { $this->configuration = $configuration; } // Define our own additionl T_* token constants Constants::init(); // Set up the lint checker and make sure that given path points to a PHP binary $linter = new Linter($pathToPhpExecutable); // Create result object that collects the error and warning messages $this->result = new Result(); // Create a list of all rules to enforce $this->rules = $this->loadRules($this->configuration->getRules()); // List all PHP files in given path $phpFiles = $this->listFiles($fileOrDirectory, $this->configuration->getExtensions()); if (sizeof($phpFiles) == 0) { throw new Exception('No PHP files to analyze'); } $this->numberOfFiles = sizeof($phpFiles); foreach ($phpFiles as $phpFile) { // Remember that we have processed this file, // even if it generates no message at all. $this->result->addFile($phpFile); if ($this->isSkipped($phpFile)) { $this->result->addMessage(new Skipped($phpFile, 'Skipped')); } else { if ($linter->runLintCheck($phpFile)) { $file = Tokenizer::tokenize($phpFile, file_get_contents($phpFile)); $this->result->addNamespaces($phpFile, $file->getNamespaces()); $this->result->addClasses($phpFile, $file->getClasses()); $this->result->addFunctions($phpFile, $file->getFunctions()); $this->enforceRules($phpFile, $file); } else { $this->result->addMessage(new LintError($phpFile, $linter->getErrorMessages())); } } // Notify the progress printer that we have analyzed a file if (is_object($this->progressPrinter)) { $this->progressPrinter->showProgress($phpFile, $this->result, $this); } unset($phpFile); } // Return the result object containing all error and warning messages return $this->result; }
/** * This test assumes that the PHP binary can be found by running "which php". * * @covers spriebsch\PHPca\Linter::getErrorMessages */ public function testGetErrorMessagesReturnsLintErrors() { $php = trim(exec('which php')); $linter = new Linter($php); $linter->runLintCheck(__DIR__ . '/_testdata/lint_fail.php'); $this->assertContains('Parse error', $linter->getErrorMessages()); }