Skip to content

Commit f95091f

Browse files
committed
fix: suppress warnings when loading files and clean up reflection access in tests
1 parent 408d8f7 commit f95091f

16 files changed

Lines changed: 844 additions & 51 deletions

‎src/StaticPHP/Command/BaseCommand.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function initialize(InputInterface $input, OutputInterface $output): void
5454
}
5555

5656
set_error_handler(static function ($error_no, $error_msg, $error_file, $error_line) {
57+
// Respect the @ suppression operator (error_reporting() returns 0 when @ is used)
58+
if (error_reporting() === 0) {
59+
return true;
60+
}
5761
$tips = [
5862
E_WARNING => ['PHP Warning: ', 'warning'],
5963
E_NOTICE => ['PHP Notice: ', 'notice'],

‎src/StaticPHP/Config/ArtifactConfig.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function loadFromDir(string $dir, string $registry_name): array
3838
*/
3939
public static function loadFromFile(string $file, string $registry_name): string
4040
{
41-
$content = file_get_contents($file);
41+
$content = @file_get_contents($file);
4242
if ($content === false) {
4343
throw new WrongUsageException("Failed to read artifact config file: {$file}");
4444
}

‎src/StaticPHP/Config/PackageConfig.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function loadFromDir(string $dir, string $registry_name): array
4646
*/
4747
public static function loadFromFile(string $file, string $registry_name): string
4848
{
49-
$content = file_get_contents($file);
49+
$content = @file_get_contents($file);
5050
if ($content === false) {
5151
throw new WrongUsageException("Failed to read package config file: {$file}");
5252
}

‎src/StaticPHP/Registry/PackageLoader.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ public static function getBeforeStageCallbacks(string $package_name, string $sta
370370
// match condition
371371
$installer = ApplicationContext::get(PackageInstaller::class);
372372
$stages = self::$before_stages[$package_name][$stage] ?? [];
373-
foreach ($stages as [$callback, $only_when_package_resolved, $conditionals]) {
373+
foreach ($stages as $entry) {
374+
$callback = $entry[0];
375+
$only_when_package_resolved = $entry[1] ?? null;
376+
$conditionals = $entry[2] ?? [];
374377
if ($only_when_package_resolved !== null && !$installer->isPackageResolved($only_when_package_resolved)) {
375378
continue;
376379
}
@@ -389,7 +392,10 @@ public static function getAfterStageCallbacks(string $package_name, string $stag
389392
$installer = ApplicationContext::get(PackageInstaller::class);
390393
$stages = self::$after_stages[$package_name][$stage] ?? [];
391394
$result = [];
392-
foreach ($stages as [$callback, $only_when_package_resolved, $conditionals]) {
395+
foreach ($stages as $entry) {
396+
$callback = $entry[0];
397+
$only_when_package_resolved = $entry[1] ?? null;
398+
$conditionals = $entry[2] ?? [];
393399
if ($only_when_package_resolved !== null && !$installer->isPackageResolved($only_when_package_resolved)) {
394400
continue;
395401
}
@@ -433,7 +439,9 @@ public static function checkLoadedStageEvents(): void
433439
}
434440
$pkg = self::getPackage($package_name);
435441
foreach ($stages as $stage_name => $before_events) {
436-
foreach ($before_events as [$event_callable, $only_when_package_resolved, $conditionals]) {
442+
foreach ($before_events as $entry) {
443+
$event_callable = $entry[0];
444+
$only_when_package_resolved = $entry[1] ?? null;
437445
// check only_when_package_resolved package exists
438446
if ($only_when_package_resolved !== null && !self::hasPackage($only_when_package_resolved)) {
439447
throw new RegistryException("{$event_name} event in package [{$package_name}] for stage [{$stage_name}] has unknown only_when_package_resolved package [{$only_when_package_resolved}].");

‎tests/GlobalsFunctionsTest.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ protected function setUp(): void
2121
protected function tearDown(): void
2222
{
2323
$GLOBALS['spc_log_filters'] = null;
24+
// Restore logger level to avoid polluting other tests with DEBUG noise
25+
logger()->setLevel(LogLevel::ERROR);
2426
}
2527

2628
public function testAddLogFilterDeduplicates(): void

‎tests/StaticPHP/Artifact/ArtifactDownloaderTest.php‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ protected function setUp(): void
2323
// Reset ArtifactConfig and ArtifactLoader static state
2424
$reflection = new \ReflectionClass(ArtifactConfig::class);
2525
$property = $reflection->getProperty('artifact_configs');
26-
$property->setAccessible(true);
2726
$property->setValue(null, []);
2827

2928
$loaderReflection = new \ReflectionClass(ArtifactLoader::class);
3029
$loaderProperty = $loaderReflection->getProperty('artifacts');
31-
$loaderProperty->setAccessible(true);
3230
$loaderProperty->setValue(null, null);
3331
}
3432

@@ -38,12 +36,10 @@ protected function tearDown(): void
3836

3937
$reflection = new \ReflectionClass(ArtifactConfig::class);
4038
$property = $reflection->getProperty('artifact_configs');
41-
$property->setAccessible(true);
4239
$property->setValue(null, []);
4340

4441
$loaderReflection = new \ReflectionClass(ArtifactLoader::class);
4542
$loaderProperty = $loaderReflection->getProperty('artifacts');
46-
$loaderProperty->setAccessible(true);
4743
$loaderProperty->setValue(null, null);
4844
}
4945

@@ -343,7 +339,6 @@ private function injectArtifactConfig(string $name, array $config): void
343339
{
344340
$reflection = new \ReflectionClass(ArtifactConfig::class);
345341
$property = $reflection->getProperty('artifact_configs');
346-
$property->setAccessible(true);
347342
$configs = $property->getValue(null) ?? [];
348343
$configs[$name] = $config;
349344
$property->setValue(null, $configs);

‎tests/StaticPHP/Artifact/ArtifactExtractorTest.php‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ protected function setUp(): void
3131

3232
$reflection = new \ReflectionClass(ArtifactConfig::class);
3333
$property = $reflection->getProperty('artifact_configs');
34-
$property->setAccessible(true);
3534
$property->setValue(null, []);
3635

3736
$loaderReflection = new \ReflectionClass(ArtifactLoader::class);
3837
$loaderProperty = $loaderReflection->getProperty('artifacts');
39-
$loaderProperty->setAccessible(true);
4038
$loaderProperty->setValue(null, null);
4139

4240
ApplicationContext::reset();
@@ -51,12 +49,10 @@ protected function tearDown(): void
5149

5250
$reflection = new \ReflectionClass(ArtifactConfig::class);
5351
$property = $reflection->getProperty('artifact_configs');
54-
$property->setAccessible(true);
5552
$property->setValue(null, []);
5653

5754
$loaderReflection = new \ReflectionClass(ArtifactLoader::class);
5855
$loaderProperty = $loaderReflection->getProperty('artifacts');
59-
$loaderProperty->setAccessible(true);
6056
$loaderProperty->setValue(null, null);
6157

6258
ApplicationContext::reset();
@@ -157,7 +153,6 @@ public function testExtractReturnsAlreadyExtractedForSecondCall(): void
157153
// Pre-populate the extracted map for 'my-pkg' via reflection
158154
$reflection = new \ReflectionClass(ArtifactExtractor::class);
159155
$extractedProperty = $reflection->getProperty('extracted');
160-
$extractedProperty->setAccessible(true);
161156
$extractedProperty->setValue($extractor, ['my-pkg' => true]);
162157

163158
$result = $extractor->extract($artifact, false);
@@ -181,7 +176,6 @@ public function testExtractWithStringNameLooksUpFromArtifactLoader(): void
181176
// Pre-populate the extracted map so we don't need actual downloads
182177
$reflection = new \ReflectionClass(ArtifactExtractor::class);
183178
$extractedProperty = $reflection->getProperty('extracted');
184-
$extractedProperty->setAccessible(true);
185179
$extractedProperty->setValue($extractor, ['my-pkg' => true]);
186180

187181
$result = $extractor->extract('my-pkg', false);
@@ -204,7 +198,6 @@ private function injectArtifactConfig(string $name, array $config): void
204198
{
205199
$reflection = new \ReflectionClass(ArtifactConfig::class);
206200
$property = $reflection->getProperty('artifact_configs');
207-
$property->setAccessible(true);
208201
$configs = $property->getValue(null) ?? [];
209202
$configs[$name] = $config;
210203
$property->setValue(null, $configs);

‎tests/StaticPHP/Artifact/ArtifactTest.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ protected function setUp(): void
2929
// Reset ArtifactConfig static state
3030
$reflection = new \ReflectionClass(ArtifactConfig::class);
3131
$property = $reflection->getProperty('artifact_configs');
32-
$property->setAccessible(true);
3332
$property->setValue(null, []);
3433

3534
// Reset DI container
@@ -45,7 +44,6 @@ protected function tearDown(): void
4544

4645
$reflection = new \ReflectionClass(ArtifactConfig::class);
4746
$property = $reflection->getProperty('artifact_configs');
48-
$property->setAccessible(true);
4947
$property->setValue(null, []);
5048

5149
ApplicationContext::reset();
@@ -715,7 +713,6 @@ private function injectArtifactConfig(string $name, array $config): void
715713
{
716714
$reflection = new \ReflectionClass(ArtifactConfig::class);
717715
$property = $reflection->getProperty('artifact_configs');
718-
$property->setAccessible(true);
719716
$configs = $property->getValue(null) ?? [];
720717
$configs[$name] = $config;
721718
$property->setValue(null, $configs);

0 commit comments

Comments
 (0)