Passed
Push — master ( 477224...3259b8 )
by Aleksei
06:12 queued 18s
created

BindScope::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Boot\Attribute;
6
7
/**
8
 * Attribute to bind a method's result to a specific container scope.
9
 *
10
 * This attribute allows defining a specific scope for the binding created
11
 * by method attributes such as BindMethod or SingletonMethod.
12
 *
13
 * Example usage:
14
 * ```php
15
 * class MyBootloader extends Bootloader
16
 * {
17
 *     // Bind to the 'http' scope
18
 *     #[BindMethod]
19
 *     #[BindScope('http')]
20
 *     public function createHttpClient(): HttpClientInterface
21
 *     {
22
 *         return new HttpClient();
23
 *     }
24
 *
25
 *     // Bind to the 'console' scope using an enum
26
 *     #[SingletonMethod]
27
 *     #[BindScope(ScopeEnum::Console)]
28
 *     public function createConsoleOutput(): OutputInterface
29
 *     {
30
 *         return new ConsoleOutput();
31
 *     }
32
 * }
33
 * ```
34
 *
35
 * When using a scope, the binding will only be available when the container
36
 * is running within that scope.
37
 */
38
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
39
final class BindScope
40
{
41
    /**
42
     * The scope name to bind to.
43
     */
44
    public readonly string $scope;
45
46
    /**
47
     * @param string|\BackedEnum $scope The scope name or enum value to bind to.
48
     *                                  If an enum is provided, its value will be used as the scope name.
49
     */
50
    public function __construct(string|\BackedEnum $scope)
51
    {
52
        $this->scope = \is_object($scope) ? (string) $scope->value : $scope;
0 ignored issues
show
Bug introduced by
Accessing value on the interface BackedEnum suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The property scope is declared read-only in Spiral\Boot\Attribute\BindScope.
Loading history...
53
    }
54
}
55