Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
40 / 40 |
|
91.11% |
41 / 45 |
|
74.07% |
20 / 27 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Injected | |
100.00% |
40 / 40 |
|
91.11% |
41 / 45 |
|
74.07% |
20 / 27 |
|
100.00% |
6 / 6 |
30.43 | |
100.00% |
1 / 1 |
| value | |
100.00% |
8 / 8 |
|
86.67% |
13 / 15 |
|
85.71% |
6 / 7 |
|
100.00% |
1 / 1 |
7.14 | |||
| getValue | |
100.00% |
13 / 13 |
|
90.91% |
10 / 11 |
|
71.43% |
5 / 7 |
|
100.00% |
1 / 1 |
5.58 | |||
| getEntry | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| getObject | |
100.00% |
7 / 7 |
|
87.50% |
7 / 8 |
|
33.33% |
2 / 6 |
|
100.00% |
1 / 1 |
5.67 | |||
| getEnvVar | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getFromCallback | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Wire; |
| 6 | |
| 7 | use Celemas\Wire\Exception\WireException; |
| 8 | |
| 9 | final readonly class Injected |
| 10 | { |
| 11 | public static function value( |
| 12 | Inject $inject, |
| 13 | CreatorInterface $creator, |
| 14 | array $predefinedTypes, |
| 15 | ?callable $injectCallback, |
| 16 | ): mixed { |
| 17 | return match ($inject->type) { |
| 18 | Type::Literal => $inject->value, |
| 19 | Type::Create => self::getObject($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 20 | Type::Entry => self::getEntry($creator, (string) $inject->value, $predefinedTypes), |
| 21 | Type::Env => self::getEnvVar($inject->value), |
| 22 | Type::Callback => self::getFromCallback($inject, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | private static function getValue( |
| 28 | CreatorInterface $creator, |
| 29 | mixed $value, |
| 30 | array $predefinedTypes, |
| 31 | ?callable $injectCallback, |
| 32 | ): mixed { |
| 33 | if (is_string($value)) { |
| 34 | if (isset($predefinedTypes[$value])) { |
| 35 | return $predefinedTypes[$value]; |
| 36 | } |
| 37 | |
| 38 | $container = $creator->container(); |
| 39 | |
| 40 | if ($container?->has($value)) { |
| 41 | return $container->get($value); |
| 42 | } |
| 43 | |
| 44 | if (class_exists($value)) { |
| 45 | return $creator->create( |
| 46 | $value, |
| 47 | predefinedTypes: $predefinedTypes, |
| 48 | injectCallback: $injectCallback, |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return $value; |
| 54 | } |
| 55 | |
| 56 | private static function getEntry( |
| 57 | CreatorInterface $creator, |
| 58 | string $value, |
| 59 | array $predefinedTypes, |
| 60 | ): mixed { |
| 61 | if (isset($predefinedTypes[$value])) { |
| 62 | return $predefinedTypes[$value]; |
| 63 | } |
| 64 | |
| 65 | $container = $creator->container(); |
| 66 | |
| 67 | if (is_null($container)) { |
| 68 | throw new WireException('No container available to resolve injected id "' . $value . '"!'); |
| 69 | } |
| 70 | |
| 71 | return $container->get($value); |
| 72 | } |
| 73 | |
| 74 | private static function getObject( |
| 75 | CreatorInterface $creator, |
| 76 | mixed $value, |
| 77 | array $predefinedTypes, |
| 78 | ?callable $injectCallback, |
| 79 | ): mixed { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 81 | throw new WireException('No valid class string "' . (string) $value . '"!'); |
| 82 | } |
| 83 | |
| 84 | return $creator->create( |
| 85 | $value, |
| 86 | predefinedTypes: $predefinedTypes, |
| 87 | injectCallback: $injectCallback, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | private static function getEnvVar(mixed $value): bool|string |
| 92 | { |
| 93 | if (!is_string($value)) { |
| 94 | throw new WireException('Environment variable must be a string!'); |
| 95 | } |
| 96 | |
| 97 | return getenv($value); |
| 98 | } |
| 99 | |
| 100 | private static function getFromCallback(Inject $inject, ?callable $injectCallback): mixed |
| 101 | { |
| 102 | if (is_callable($injectCallback)) { |
| 103 | return $injectCallback($inject); |
| 104 | } |
| 105 | |
| 106 | throw new WireException('Inject callback not available'); |
| 107 | } |
| 108 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 57 | CreatorInterface $creator, |
| 58 | string $value, |
| 59 | array $predefinedTypes, |
| 60 | ): mixed { |
| 61 | if (isset($predefinedTypes[$value])) { |
| 62 | return $predefinedTypes[$value]; |
| 65 | $container = $creator->container(); |
| 66 | |
| 67 | if (is_null($container)) { |
| 68 | throw new WireException('No container available to resolve injected id "' . $value . '"!'); |
| 71 | return $container->get($value); |
| 72 | } |
| 91 | private static function getEnvVar(mixed $value): bool|string |
| 92 | { |
| 93 | if (!is_string($value)) { |
| 94 | throw new WireException('Environment variable must be a string!'); |
| 97 | return getenv($value); |
| 98 | } |
| 100 | private static function getFromCallback(Inject $inject, ?callable $injectCallback): mixed |
| 101 | { |
| 102 | if (is_callable($injectCallback)) { |
| 103 | return $injectCallback($inject); |
| 106 | throw new WireException('Inject callback not available'); |
| 107 | } |
| 75 | CreatorInterface $creator, |
| 76 | mixed $value, |
| 77 | array $predefinedTypes, |
| 78 | ?callable $injectCallback, |
| 79 | ): mixed { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 80 | if (!is_string($value) || !class_exists($value)) { |
| 81 | throw new WireException('No valid class string "' . (string) $value . '"!'); |
| 84 | return $creator->create( |
| 85 | $value, |
| 86 | predefinedTypes: $predefinedTypes, |
| 87 | injectCallback: $injectCallback, |
| 88 | ); |
| 89 | } |
| 28 | CreatorInterface $creator, |
| 29 | mixed $value, |
| 30 | array $predefinedTypes, |
| 31 | ?callable $injectCallback, |
| 32 | ): mixed { |
| 33 | if (is_string($value)) { |
| 34 | if (isset($predefinedTypes[$value])) { |
| 35 | return $predefinedTypes[$value]; |
| 38 | $container = $creator->container(); |
| 39 | |
| 40 | if ($container?->has($value)) { |
| 41 | return $container->get($value); |
| 44 | if (class_exists($value)) { |
| 44 | if (class_exists($value)) { |
| 44 | if (class_exists($value)) { |
| 44 | if (class_exists($value)) { |
| 45 | return $creator->create( |
| 46 | $value, |
| 53 | return $value; |
| 54 | } |
| 12 | Inject $inject, |
| 13 | CreatorInterface $creator, |
| 14 | array $predefinedTypes, |
| 15 | ?callable $injectCallback, |
| 16 | ): mixed { |
| 17 | return match ($inject->type) { |
| 18 | Type::Literal => $inject->value, |
| 19 | Type::Create => self::getObject($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 20 | Type::Entry => self::getEntry($creator, (string) $inject->value, $predefinedTypes), |
| 21 | Type::Env => self::getEnvVar($inject->value), |
| 22 | Type::Callback => self::getFromCallback($inject, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 18 | Type::Literal => $inject->value, |
| 19 | Type::Create => self::getObject($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 20 | Type::Entry => self::getEntry($creator, (string) $inject->value, $predefinedTypes), |
| 21 | Type::Env => self::getEnvVar($inject->value), |
| 22 | Type::Callback => self::getFromCallback($inject, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 23 | null => self::getValue($creator, $inject->value, $predefinedTypes, $injectCallback), |
| 24 | }; |
| 25 | } |