Overtrue\PHPLint\Linter::lint PHP Method

lint() public method

Check the files.
public lint ( array $files = [], boolean $cache = true ) : array
$files array
$cache boolean
return array
    public function lint($files = [], $cache = true)
    {
        if (empty($files)) {
            $files = $this->getFiles();
        }
        $processCallback = is_callable($this->processCallback) ? $this->processCallback : function () {
        };
        $errors = [];
        $running = [];
        $newCache = [];
        while (!empty($files) || !empty($running)) {
            for ($i = count($running); !empty($files) && $i < $this->procLimit; ++$i) {
                $file = array_shift($files);
                $filename = $file->getRealpath();
                if (!isset($this->cache[$filename]) || $this->cache[$filename] !== md5_file($filename)) {
                    $running[$filename] = new Lint(PHP_BINARY . ' -d error_reporting=E_ALL -d display_errors=On -l ' . escapeshellarg($filename));
                    $running[$filename]->start();
                } else {
                    $newCache[$filename] = $this->cache[$filename];
                }
            }
            foreach ($running as $filename => $lintProcess) {
                if ($lintProcess->isRunning()) {
                    continue;
                }
                unset($running[$filename]);
                if ($lintProcess->hasSyntaxError()) {
                    $processCallback('error', $filename);
                    $errors[$filename] = array_merge(['file' => $filename], $lintProcess->getSyntaxError());
                } else {
                    $newCache[$filename] = md5_file($filename);
                    $processCallback('ok', $filename);
                }
            }
            $cache && Cache::put($newCache);
        }
        return $errors;
    }

Usage Example

Example #1
0
 /**
  * Execute lint and return errors.
  *
  * @param Linter          $linter
  * @param OutputInterface $output
  * @param int             $fileCount
  * @param bool            $cache
  */
 protected function executeLint($linter, $output, $fileCount, $cache = true)
 {
     $maxColumns = floor($this->getScreenColumns() / 2);
     $verbosity = $output->getVerbosity();
     $linter->setProcessCallback(function ($status, $filename) use($output, $verbosity, $fileCount, $maxColumns) {
         static $i = 0;
         if ($i && $i % $maxColumns === 0) {
             $percent = floor($i / $fileCount * 100);
             $output->writeln(str_pad(" {$i} / {$fileCount} ({$percent}%)", 18, ' ', STR_PAD_LEFT));
         }
         ++$i;
         if ($verbosity >= OutputInterface::VERBOSITY_VERBOSE) {
             $output->writeln('Linting: ' . $filename . "\t" . ($status === 'ok' ? '<info>OK</info>' : '<error>Error</error>'));
         } else {
             $output->write($status === 'ok' ? '<info>.</info>' : '<error>E</error>');
         }
     });
     return $linter->lint([], $cache);
 }