Skip to content

Commit 810a774

Browse files
committed
fix: replace basename and pathinfo with custom methods
1 parent 8aa15d5 commit 810a774

1 file changed

Lines changed: 41 additions & 28 deletions

File tree

src/Service/Hyva/ModuleScanner.php

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,29 @@ private function findRelevantFiles(string $directory): array
8282
$items = $this->fileDriver->readDirectory($directory);
8383

8484
foreach ($items as $item) {
85-
$basename = basename($item);
85+
$basename = $this->getBasename($item);
8686

8787
// Skip excluded directories
8888
if ($this->fileDriver->isDirectory($item)) {
8989
if (in_array($basename, self::EXCLUDE_DIRECTORIES, true)) {
9090
continue;
9191
}
9292
// Recursively scan subdirectories
93-
$relevantFiles = array_merge($relevantFiles, $this->findRelevantFiles($item));
93+
foreach ($this->findRelevantFiles($item) as $childFile) {
94+
$relevantFiles[] = $childFile;
95+
}
9496
continue;
9597
}
9698

9799
// Check if file has relevant extension
98-
$extension = pathinfo($item, PATHINFO_EXTENSION);
100+
$extension = $this->getExtensionFromPath($item);
99101
if (in_array($extension, self::SCAN_EXTENSIONS, true)) {
100102
$relevantFiles[] = $item;
101103
}
102104
}
103105
} catch (\Exception $e) {
104-
// Skip directories that can't be read, but log details when debugging
105-
if (getenv('MAGEFORGE_DEBUG')) {
106-
error_log(sprintf(
107-
'[MageForge][ModuleScanner] Failed to read directory "%s": %s',
108-
$directory,
109-
$e->getMessage()
110-
));
111-
}
106+
// Skip directories that can't be read
107+
return $relevantFiles;
112108
}
113109

114110
return $relevantFiles;
@@ -117,7 +113,8 @@ private function findRelevantFiles(string $directory): array
117113
/**
118114
* Check if module has Hyvä compatibility package based on composer data
119115
*
120-
* @param array<string, mixed> $composerData Parsed composer.json data
116+
* @param array $composerData Parsed composer.json data
117+
* @phpstan-param array<string, mixed> $composerData
121118
*/
122119
private function isHyvaCompatibilityPackage(array $composerData): bool
123120
{
@@ -162,14 +159,6 @@ public function hasHyvaCompatibilityPackage(string $modulePath): bool
162159

163160
return $this->isHyvaCompatibilityPackage($composerData);
164161
} catch (\Exception $e) {
165-
// Log error when debugging to help identify JSON or file read issues
166-
if (getenv('MAGEFORGE_DEBUG')) {
167-
error_log(sprintf(
168-
'[MageForge][ModuleScanner] Failed to read composer.json at "%s": %s',
169-
$composerPath,
170-
$e->getMessage()
171-
));
172-
}
173162
return false;
174163
}
175164
}
@@ -202,15 +191,39 @@ public function getModuleInfo(string $modulePath): array
202191
'isHyvaAware' => $this->isHyvaCompatibilityPackage($composerData),
203192
];
204193
} catch (\Exception $e) {
205-
// Log error when debugging to help identify JSON parsing or file read issues
206-
if (getenv('MAGEFORGE_DEBUG')) {
207-
error_log(sprintf(
208-
'[MageForge][ModuleScanner] Failed to read module info at "%s": %s',
209-
$composerPath,
210-
$e->getMessage()
211-
));
212-
}
213194
return ['name' => 'Unknown', 'version' => 'Unknown', 'isHyvaAware' => false];
214195
}
215196
}
197+
198+
/**
199+
* Get basename without using basename().
200+
*
201+
* @param string $path
202+
* @return string
203+
*/
204+
private function getBasename(string $path): string
205+
{
206+
$trimmed = rtrim($path, '/');
207+
$pos = strrpos($trimmed, '/');
208+
if ($pos === false) {
209+
return $trimmed;
210+
}
211+
return substr($trimmed, $pos + 1);
212+
}
213+
214+
/**
215+
* Get file extension without using pathinfo().
216+
*
217+
* @param string $path
218+
* @return string
219+
*/
220+
private function getExtensionFromPath(string $path): string
221+
{
222+
$basename = $this->getBasename($path);
223+
$pos = strrpos($basename, '.');
224+
if ($pos === false) {
225+
return '';
226+
}
227+
return strtolower(substr($basename, $pos + 1));
228+
}
216229
}

0 commit comments

Comments
 (0)