Skip to content

Commit

Permalink
fixing issues around accessing typed property before initialisation, …
Browse files Browse the repository at this point in the history
…setting up property as Closure and then creating Closure from callable in the setter. Using short function syntax for the no-op function placeholder
  • Loading branch information
joseph committed Jul 8, 2021
1 parent db72d22 commit 82f936a
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions src/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@

class Linter
{
private mixed $processCallback;
private array $files = [];
private array $cache = [];
private array $paths;
private array $excludes;
private array $extensions;
private int $processLimit = 5;
private bool $warning;

public function __construct(array|string $paths, array $excludes = [], array $extensions = ['php'], $warning = false)
{
$this->paths = (array)$paths;
$this->excludes = $excludes;
$this->warning = $warning;
private ?\Closure $processCallback = null;
private array $files = [];
private array $cache = [];
private array $paths;
private array $excludes;
private array $extensions;
private int $processLimit = 5;
private bool $warning;

public function __construct(
array|string $paths,
array $excludes = [],
array $extensions = ['php'],
$warning = false
) {
$this->paths = (array)$paths;
$this->excludes = $excludes;
$this->warning = $warning;
$this->extensions = \array_map(function ($extension) {
return \sprintf('*.%s', \ltrim($extension, '.'));
}, $extensions);
},
$extensions);
}

public function lint(array $files = [], bool $cache = true): array
Expand All @@ -34,23 +39,23 @@ public function lint(array $files = [], bool $cache = true): array
$files = $this->getFiles();
}

$processCallback = is_callable($this->processCallback) ? $this->processCallback : function () {
};
$processCallback = $this->processCallback ?? fn() => null;

$errors = [];
$running = [];
$errors = [];
$running = [];
$newCache = [];

while (!empty($files) || !empty($running)) {
for ($i = count($running); !empty($files) && $i < $this->processLimit; ++$i) {
$file = array_shift($files);
$filename = $file->getRealPath();
$file = array_shift($files);
$filename = $file->getRealPath();
$relativePathname = $file->getRelativePathname();
if (!isset($this->cache[$relativePathname]) || $this->cache[$relativePathname] !== md5_file($filename)) {
$lint = $this->createLintProcess($filename);
if (!isset($this->cache[$relativePathname]) ||
$this->cache[$relativePathname] !== md5_file($filename)) {
$lint = $this->createLintProcess($filename);
$running[$filename] = [
'process' => $lint,
'file' => $file,
'process' => $lint,
'file' => $file,
'relativePath' => $relativePathname,
];
$lint->start();
Expand All @@ -71,10 +76,14 @@ public function lint(array $files = [], bool $cache = true): array

if ($lint->hasSyntaxError()) {
$processCallback('error', $item['file']);
$errors[$filename] = array_merge(['file' => $filename, 'file_name' => $item['relativePath']], $lint->getSyntaxError());
$errors[$filename] =
array_merge(['file' => $filename, 'file_name' => $item['relativePath']],
$lint->getSyntaxError());
} elseif ($this->warning && $lint->hasSyntaxIssue()) {
$processCallback('warning', $item['file']);
$errors[$filename] = array_merge(['file' => $filename, 'file_name' => $item['relativePath']], $lint->getSyntaxIssue());
$errors[$filename] =
array_merge(['file' => $filename, 'file_name' => $item['relativePath']],
$lint->getSyntaxIssue());
} else {
$newCache[$item['relativePath']] = md5_file($filename);
$processCallback('ok', $item['file']);
Expand Down Expand Up @@ -115,12 +124,12 @@ protected function getFilesFromDir(string $dir): array
{
$finder = new Finder();
$finder->files()
->ignoreUnreadableDirs()
->ignoreVCS(true)
->filter(function (SplFileInfo $file) {
return $file->isReadable();
})
->in(realpath($dir));
->ignoreUnreadableDirs()
->ignoreVCS(true)
->filter(function (SplFileInfo $file) {
return $file->isReadable();
})
->in(realpath($dir));

array_map([$finder, 'name'], $this->extensions);
array_map([$finder, 'notPath'], $this->excludes);
Expand All @@ -147,7 +156,7 @@ public function setFiles(array $files): static

public function setProcessCallback(callable $processCallback): static
{
$this->processCallback = $processCallback;
$this->processCallback = \Closure::fromCallable($processCallback);

return $this;
}
Expand All @@ -165,7 +174,8 @@ protected function createLintProcess(string $filename): Lint
PHP_SAPI == 'cli' ? PHP_BINARY : PHP_BINDIR . '/php',
'-d error_reporting=E_ALL',
'-d display_errors=On',
'-l', $filename,
'-l',
$filename,
];

return new Lint($command);
Expand Down

0 comments on commit 82f936a

Please sign in to comment.