Skip to content

Commit

Permalink
Add toggle switch for Counter::getFolderInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanAZ committed Aug 17, 2023
1 parent 36e0174 commit fa33259
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/NhanAZ/libBedrock/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,43 @@ class Counter {
* @param string $extension The file extension to filter by.
* @return array{"totalFiles": int, "totalLines": int, "totalWords": int, "totalChars": int} An array of file information.
*/
public static function getFolderInfo(string $dir, string $extension) : array {
public static function getFolderInfo(string $dir, string $extension, bool $totalFilesT = true, bool $totalLinesT = true, bool $totalWordsT = true, bool $totalCharsT = true): array {
$files = new \RecursiveIteratorIterator(
iterator: new \RecursiveDirectoryIterator($dir),
mode: \RecursiveIteratorIterator::CHILD_FIRST
);
$txtFiles = [];
foreach ($files as $file) {
/** @var SplFileInfo $file */
if ($file->isFile() && $file->getExtension() == $extension) {
$txtFiles[] = $file->getRealPath();
if ($totalFilesT) {
foreach ($files as $file) {
/** @var SplFileInfo $file */
if ($file->isFile() && $file->getExtension() == $extension) {
$txtFiles[] = $file->getRealPath();
}
}
}
$totalLines = 0;
$totalWords = 0;
$totalChars = 0;
foreach ($txtFiles as $txtFile) {
$content = file_get_contents($txtFile);
$value = file($txtFile);
if (is_array($value)) {
$totalLines += count($value);
}
if ($content !== false) {
$totalWords += str_word_count($content);
$totalChars += strlen($content);
if ($totalLinesT || $totalWords || $totalCharsT) {

Check failure on line 41 in src/NhanAZ/libBedrock/Counter.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Right side of || is always false.

Check failure on line 41 in src/NhanAZ/libBedrock/Counter.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.2)

Right side of || is always false.
foreach ($txtFiles as $txtFile) {
if ($totalLinesT) {
$value = file($txtFile);
if (is_array($value)) {
$totalLines += count($value);
}
}

if ($totalWordsT || $totalChars) {
$content = file_get_contents($txtFile);
if ($content !== false) {
if ($totalWordsT) {
$totalWords += str_word_count($content);
}
if ($totalCharsT) {
$totalChars += strlen($content);
}
}
}
}
}
/* TODO: Add more info */
Expand Down

0 comments on commit fa33259

Please sign in to comment.