Skip to content

Commit

Permalink
Fixed bugs: directory separator, suffix infinity loop, analyze for se…
Browse files Browse the repository at this point in the history
…veral paths

Added more pretty setters
  • Loading branch information
Filipponik committed May 26, 2023
1 parent 688c491 commit 2e7064c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
32 changes: 29 additions & 3 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/DirectoryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2e7064c

Please sign in to comment.