Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/martinssipenko/phplint
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/martinssipenko/phplint:
  Added check for file count with given patterns
  Fixed app name and version, added initial cache mechanism
  Improved file finding
  Update LintCommand.php
  Update README.md

Conflicts:
	Phplint/Linter.php
  • Loading branch information
Martins Sipenko committed Sep 26, 2014
2 parents 7ebff84 + f8e52f3 commit 01fa0cd
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 53 deletions.
86 changes: 48 additions & 38 deletions Phplint/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$startTime = microtime(true);

$output->writeln("phplint {$this->getApplication()->getVersion()}");
$output->writeln($this->getApplication()->getLongVersion());
$output->writeln('');

$phpBinary = PHP_BINARY;
Expand All @@ -70,54 +70,64 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$linter = new Linter($path, $exclude, $extensions);
if ($procLimit) {
$linter->setProcessLimit($procLimit);
}

$files = $linter->getFiles();
$fileCount = count($files);

$progress = new ProgressBar($output, $fileCount);
$progress->setBarWidth(50);
$progress->setMessage('', 'overview');
$progress->setFormat(" %overview%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%");
$progress->start();

$linter->setProcessCallback(function ($status, $filename) use ($progress) {
/*
$overview = $progress->getMessage('overview');
if ($status == 'ok') {
$overview .= '.';
} elseif ($status == 'error') {
$overview .= 'F';
// exit(1);
if ($fileCount > 0) {
if ($procLimit) {
$linter->setProcessLimit($procLimit);
}

$progress->setMessage($overview, 'overview');
*/
$progress->advance();
});
if (file_exists(__DIR__.'/../../phplint.cache')) {
$linter->setCache(json_decode(file_get_contents(__DIR__.'/../../phplint.cache'), true));
}

$result = $linter->lint($files);

$progress->finish();
$output->writeln('');

$testTime = microtime(true) - $startTime;
$progress = new ProgressBar($output, $fileCount);
$progress->setBarWidth(50);
$progress->setMessage('', 'filename');
$progress->setFormat(" %filename%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%");
$progress->start();

$code = 0;
$errCount = count($result);
$out = "<info>Checked {$fileCount} files in ".round($testTime, 1)." seconds</info>";
if ($errCount > 0) {
$out .= "<info> and found syntax errors in </info><error>{$errCount}</error><info> files.</info>";
$out .= "\n" . json_encode($result, JSON_PRETTY_PRINT);
$code = 1;
} else {
$out .= '<info> a no syntax error were deteced.';
$linter->setProcessCallback(function ($status, $filename) use ($progress) {
$progress->setMessage($filename, 'filename');
$progress->advance();

// if ($status == 'ok') {
// $overview .= '.';
// } elseif ($status == 'error') {
// $overview .= 'F';
// // exit(1);
// }
});

$result = $linter->lint($files);

$progress->finish();
$output->writeln('');
$output->writeln('');

$testTime = microtime(true) - $startTime;

$code = 0;
$errCount = count($result);
$out = "<info>Checked {$fileCount} files in ".round($testTime, 1)." seconds</info>";
if ($errCount > 0) {
$out .= "<info> and found syntax errors in </info><error>{$errCount}</error><info> files.</info>";
$out .= "\n" . json_encode($result, JSON_PRETTY_PRINT);
$code = 1;
} else {
$out .= '<info> a no syntax error were deteced.';
}
$output->writeln($out);

return $code;
}
$output->writeln($out.PHP_EOL);

return $code;
$output->writeln('<info>Could not find files to lint</info>');

return 0;
}
}
5 changes: 3 additions & 2 deletions Phplint/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

class Application extends BaseApplication
{
const NAME = 'phplint';
const VERSION = '0.0.1';

public function getVersion()
public function __construct()
{
return self::VERSION;
parent::__construct(self::NAME, self::VERSION);
}

/**
Expand Down
46 changes: 34 additions & 12 deletions Phplint/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Linter
/** @var callable */
private $processCallback;

private $files = false;

private $path;
private $excludes;
private $extensions;
Expand All @@ -27,15 +29,19 @@ public function lint($files)
{
$processCallback = is_callable($this->processCallback) ? $this->processCallback : function() {};

$running = array();
$errors = array();
$errors = array();
$running = array();
$newCache = array();

while ($files || $running) {
for ($i = count($running); $files && $i < $this->procLimit; $i++) {
$file = array_shift($files);
$fileName = $file->getRealpath();

$running[$fileName] = new Lint(PHP_BINARY.' -l '.$fileName);
$running[$fileName]->start();
if (!isset($this->cache[$fileName]) || $this->cache[$fileName] !== md5_file($fileName)) {
$running[$fileName] = new Lint(PHP_BINARY.' -l '.$fileName);
$running[$fileName]->start();
}
}

foreach ($running as $fileName => $lintProcess) {
Expand All @@ -45,29 +51,45 @@ public function lint($files)
$processCallback('error', $fileName);
$errors[$fileName] = $lintProcess->getSyntaxError();
} else {
$newCache[$fileName] = md5_file($fileName);
$processCallback('ok', $file);
}
}
}

file_put_contents(__DIR__.'/../phplint.cache', json_encode($newCache));
}

return $errors;
}

public function setCache($cache = array())
{
if (is_array($cache)) {
$this->cache = $cache;
} else {
$this->cache = array();
}
}

public function getFiles()
{
$files = new Finder();
$files->files()->ignoreUnreadableDirs()->in(realpath($this->path));
if (!$this->files) {
$this->files = new Finder();
$this->files->files()->ignoreUnreadableDirs()->in(realpath($this->path));

foreach ($this->excludes as $exclude) {
$files->notPath($exclude);
}
foreach ($this->excludes as $exclude) {
$this->files->notPath($exclude);
}

foreach ($this->extensions as $extension) {
$this->files->name('*.'.$extension);
}

foreach ($this->extensions as $extension) {
$files->name('*.'.$extension);
$this->files = iterator_to_array($this->files);
}

return iterator_to_array($files);
return $this->files;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bin/phplint
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (PHP_SAPI !== 'cli' && PHP_MINOR_VERSION <= 3) {

$loaded = false;

foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/vendor/autoload.php') as $file) {
foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php') as $file) {
if (file_exists($file)) {
require $file;
$loaded = true;
Expand Down

0 comments on commit 01fa0cd

Please sign in to comment.