From 82f936a7d20746db7404f8cb5778748ceb5069f6 Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 8 Jul 2021 10:02:45 +0000 Subject: [PATCH 1/2] fixing issues around accessing typed property before initialisation, setting up property as Closure and then creating Closure from callable in the setter. Using short function syntax for the no-op function placeholder --- src/Linter.php | 80 ++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/Linter.php b/src/Linter.php index 5e2114be..a3543fe6 100644 --- a/src/Linter.php +++ b/src/Linter.php @@ -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 @@ -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(); @@ -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']); @@ -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); @@ -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; } @@ -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); From 581eef5eaa0463a9ccab0910798738f3bf7b7aef Mon Sep 17 00:00:00 2001 From: joseph Date: Thu, 8 Jul 2021 10:05:11 +0000 Subject: [PATCH 2/2] removing reformat --- src/Linter.php | 73 +++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/src/Linter.php b/src/Linter.php index a3543fe6..b1b0fbad 100644 --- a/src/Linter.php +++ b/src/Linter.php @@ -10,27 +10,22 @@ class Linter { 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; + 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 @@ -41,21 +36,20 @@ public function lint(array $files = [], bool $cache = true): array $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(); @@ -76,14 +70,10 @@ 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']); @@ -124,12 +114,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); @@ -174,8 +164,7 @@ 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);