From 2e7064c76e32f05cc207e54962e89735079904a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=82=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Fri, 26 May 2023 12:42:12 +0300 Subject: [PATCH] Fixed bugs: directory separator, suffix infinity loop, analyze for several paths Added more pretty setters --- src/Analyzer.php | 32 +++++++++++++++++++++++++++++--- src/DirectoryAnalyzer.php | 4 +++- src/Helper.php | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Analyzer.php b/src/Analyzer.php index c9cb352..182701d 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -14,12 +14,22 @@ class Analyzer private string $suffix = ''; + protected function analyzeDirectory(string $path): array + { + return DirectoryAnalyzer::getKeysFromDirectory($path, [], $this->suffix); + } + /** - * Analyze some directory + * Analyze some directories */ - public function analyze(string $path): self + public function analyze(string ...$paths): self { - $keys = array_unique(DirectoryAnalyzer::getKeysFromDirectory($path, [], $this->suffix)); + $keys = array_unique(array_reduce( + $paths, + fn (array $carry, string $path) => [$carry, ...$this->analyzeDirectory($path)], + [] + )); + sort($keys); $this->createCorrectKeysArray($keys); @@ -79,13 +89,29 @@ public function getIncorrectKeys(): array return $this->incorrectKeys; } + /** + * @deprecated use suffix() instead + */ public function setSuffix(string $suffix): self + { + return $this->suffix($suffix); + } + + public function suffix(string $suffix): self { $this->suffix = $suffix; return $this; } + /** + * @deprecated use directory() instead + */ public function setDirectoryPath(string $directoryPath): self + { + return $this->directory($directoryPath); + } + + public function directory(string $directoryPath): self { $this->directoryPath = $directoryPath; return $this; diff --git a/src/DirectoryAnalyzer.php b/src/DirectoryAnalyzer.php index 1a9580a..7fca5db 100644 --- a/src/DirectoryAnalyzer.php +++ b/src/DirectoryAnalyzer.php @@ -29,12 +29,14 @@ public static function getKeysFromDirectory(string $path, array $inputArr, strin if ($iterator->isReadable() && $iterator->isFile()) { if (!$suffix || $iterator->getExtension() !== $suffix) { + $iterator->next(); continue; } $file = $iterator->openFile(); $inputArr = array_merge($inputArr, self::getKeysFromFile($file)); } elseif ($iterator->isDir()) { - $inputArr = self::getKeysFromDirectory($path . '/' . $iterator->getFilename(), $inputArr, $suffix); + $nextPath = $path . DIRECTORY_SEPARATOR . $iterator->getFilename(); + $inputArr = self::getKeysFromDirectory($nextPath, $inputArr, $suffix); } $iterator->next(); } while (true); diff --git a/src/Helper.php b/src/Helper.php index b1cd615..35d6226 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -38,7 +38,7 @@ public static function saveToFile(string $directoryName, string $fileName, strin if (!is_dir($directoryName)) { mkdir($directoryName, 0755, true); } - $file = fopen($directoryName . '/' . $fileName, 'w'); + $file = fopen($directoryName . DIRECTORY_SEPARATOR . $fileName, 'w'); fputs($file, $data); fclose($file); }