Skip to content

Commit bde1440

Browse files
committed
Refactor test structure and update paths for improved organization
1 parent 7837563 commit bde1440

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4396
-2941
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@
6969
'php_unit_data_provider_method_order' => false,
7070
])
7171
->setFinder(
72-
PhpCsFixer\Finder::create()->in([__DIR__ . '/src', __DIR__ . '/tests/SPC'])
72+
PhpCsFixer\Finder::create()->in([__DIR__ . '/src', __DIR__ . '/tests/StaticPHP'])
7373
)
7474
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect());

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"autoload-dev": {
4343
"psr-4": {
44-
"SPC\\Tests\\": "tests/SPC"
44+
"Tests\\StaticPHP\\": "tests/StaticPHP"
4545
}
4646
},
4747
"bin": [

src/StaticPHP/Artifact/ArtifactExtractor.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ protected function isAlreadyExtracted(string $path, ?string $expected_hash): boo
352352
* @param string $name Artifact name (for error messages)
353353
* @param string $source_file Path to the source file or directory
354354
* @param string $cache_type Cache type: archive, git, local
355-
*
356-
* @throws WrongUsageException if source file does not exist
357355
*/
358356
protected function validateSourceFile(string $name, string $source_file, string $cache_type): void
359357
{

src/StaticPHP/DI/ApplicationContext.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class ApplicationContext
3535
* @param array $options Initialization options
3636
* - 'debug': Enable debug mode (disables compilation)
3737
* - 'definitions': Additional container definitions
38-
*
39-
* @throws \RuntimeException If already initialized
4038
*/
4139
public static function initialize(array $options = []): Container
4240
{
@@ -60,7 +58,8 @@ public static function initialize(array $options = []): Container
6058
self::$debug = $options['debug'] ?? false;
6159

6260
self::$container = $builder->build();
63-
self::$invoker = new CallbackInvoker(self::$container);
61+
// Get invoker from container to ensure singleton consistency
62+
self::$invoker = self::$container->get(CallbackInvoker::class);
6463

6564
return self::$container;
6665
}
@@ -126,7 +125,8 @@ public static function bindCommandContext(InputInterface $input, OutputInterface
126125
public static function getInvoker(): CallbackInvoker
127126
{
128127
if (self::$invoker === null) {
129-
self::$invoker = new CallbackInvoker(self::getContainer());
128+
// Get from container to ensure singleton consistency
129+
self::$invoker = self::getContainer()->get(CallbackInvoker::class);
130130
}
131131
return self::$invoker;
132132
}
@@ -139,14 +139,18 @@ public static function getInvoker(): CallbackInvoker
139139
*/
140140
public static function invoke(callable $callback, array $context = []): mixed
141141
{
142-
logger()->debug('[INVOKE] ' . (is_array($callback) ? (is_object($callback[0]) ? get_class($callback[0]) : $callback[0]) . '::' . $callback[1] : (is_string($callback) ? $callback : 'Closure')));
142+
if (function_exists('logger')) {
143+
logger()->debug('[INVOKE] ' . (is_array($callback) ? (is_object($callback[0]) ? get_class($callback[0]) : $callback[0]) . '::' . $callback[1] : (is_string($callback) ? $callback : 'Closure')));
144+
}
143145

144146
// get if callback has attribute PatchDescription
145147
$ref = new \ReflectionFunction(\Closure::fromCallable($callback));
146148
$attributes = $ref->getAttributes(PatchDescription::class);
147149
foreach ($attributes as $attribute) {
148150
$attrInstance = $attribute->newInstance();
149-
logger()->info(ConsoleColor::magenta('[PATCH]') . ConsoleColor::green(" {$attrInstance->description}"));
151+
if (function_exists('logger')) {
152+
logger()->info(ConsoleColor::magenta('[PATCH]') . ConsoleColor::green(" {$attrInstance->description}"));
153+
}
150154
}
151155
return self::getInvoker()->invoke($callback, $context);
152156
}

src/StaticPHP/DI/CallbackInvoker.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace StaticPHP\DI;
66

77
use DI\Container;
8+
use StaticPHP\Exception\SPCInternalException;
89

910
/**
1011
* CallbackInvoker is responsible for invoking callbacks with automatic dependency injection.
1112
* It supports context-based parameter resolution, allowing temporary bindings without polluting the container.
1213
*/
13-
class CallbackInvoker
14+
readonly class CallbackInvoker
1415
{
1516
public function __construct(
1617
private Container $container
@@ -34,8 +35,6 @@ public function __construct(
3435
* @param array $context Context parameters (type => value or name => value)
3536
*
3637
* @return mixed The return value of the callback
37-
*
38-
* @throws \RuntimeException If a required parameter cannot be resolved
3938
*/
4039
public function invoke(callable $callback, array $context = []): mixed
4140
{
@@ -64,8 +63,13 @@ public function invoke(callable $callback, array $context = []): mixed
6463

6564
// 3. Look up in container by type
6665
if ($typeName !== null && !$this->isBuiltinType($typeName) && $this->container->has($typeName)) {
67-
$args[] = $this->container->get($typeName);
68-
continue;
66+
try {
67+
$args[] = $this->container->get($typeName);
68+
continue;
69+
} catch (\Throwable $e) {
70+
// Container failed to resolve (e.g., missing constructor params)
71+
// Fall through to try default value or nullable
72+
}
6973
}
7074

7175
// 4. Use default value if available
@@ -81,7 +85,7 @@ public function invoke(callable $callback, array $context = []): mixed
8185
}
8286

8387
// Cannot resolve parameter
84-
throw new \RuntimeException(
88+
throw new SPCInternalException(
8589
"Cannot resolve parameter '{$paramName}'" .
8690
($typeName ? " of type '{$typeName}'" : '') .
8791
' for callback invocation'
@@ -120,19 +124,20 @@ private function expandContextHierarchy(array $context): array
120124

121125
// If value is an object, add mappings for all parent classes and interfaces
122126
if (is_object($value)) {
123-
$reflection = new \ReflectionClass($value);
127+
$originalReflection = new \ReflectionClass($value);
124128

125129
// Add concrete class
126-
$expanded[$reflection->getName()] = $value;
130+
$expanded[$originalReflection->getName()] = $value;
127131

128132
// Add all parent classes
133+
$reflection = $originalReflection;
129134
while ($parent = $reflection->getParentClass()) {
130135
$expanded[$parent->getName()] = $value;
131136
$reflection = $parent;
132137
}
133138

134-
// Add all interfaces
135-
$interfaces = (new \ReflectionClass($value))->getInterfaceNames();
139+
// Add all interfaces - reuse original reflection
140+
$interfaces = $originalReflection->getInterfaceNames();
136141
foreach ($interfaces as $interface) {
137142
$expanded[$interface] = $value;
138143
}

src/StaticPHP/Package/PackageInstaller.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,6 @@ public function getPackage(string $package_name): ?Package
404404

405405
/**
406406
* Validate that a package has required artifacts.
407-
*
408-
* @throws WrongUsageException if target/library package has no source or platform binary
409407
*/
410408
private function validatePackageArtifact(Package $package): void
411409
{

src/StaticPHP/Registry/Registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Registry
2525
*/
2626
public static function loadRegistry(string $registry_file, bool $auto_require = true): void
2727
{
28-
$yaml = file_get_contents($registry_file);
28+
$yaml = @file_get_contents($registry_file);
2929
if ($yaml === false) {
3030
throw new RegistryException("Failed to read registry file: {$registry_file}");
3131
}

tests/SPC/GlobalDefinesTest.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/SPC/GlobalFunctionsTest.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)