AnonymousClassesTest.php$2 ➔ createDomainClass()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AnonymousClassesTest.php$2 ➔ greet() 0 3 1
A AnonymousClassesTest.php$2 ➔ getConfigValues() 0 3 1
A AnonymousClassesTest.php$2 ➔ __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Integration\Framework\GlobalServices;
6
7
use Gacela\Framework\AbstractConfig;
8
use Gacela\Framework\AbstractFacade;
9
use Gacela\Framework\AbstractFactory;
10
use Gacela\Framework\AbstractProvider;
11
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal;
12
use Gacela\Framework\Container\Container;
13
use Gacela\Framework\Gacela;
14
use PHPUnit\Framework\TestCase;
15
16
use function sprintf;
17
18
final class AnonymousClassesTest extends TestCase
19
{
20
    protected function setUp(): void
21
    {
22
        Gacela::bootstrap(__DIR__);
23
    }
24
25
    public function test_class_resolver_using_anonymous_classes(): void
26
    {
27
        $this->registerConfig();
28
        $this->registerFactory();
29
        $this->registerAbstractProvider();
30
31
        $facade = new class() extends AbstractFacade {
32
            public function getSomething(): array
33
            {
34
                return $this->getFactory()
35
                    ->createDomainClass()
0 ignored issues
show
Bug introduced by
The method createDomainClass() does not exist on Gacela\Framework\AbstractFactory. It seems like you code against a sub-type of Gacela\Framework\AbstractFactory such as GacelaTest\Benchmark\FileCache\ModuleC\FactoryC or GacelaTest\Benchmark\FileCache\ModuleE\FactoryE or GacelaTest\Benchmark\Fil...\ModuleG\ModuleGFactory or GacelaTest\Benchmark\FileCache\ModuleA\FactoryA or GacelaTest\Feature\Frame...ileCache\Module\Factory or anonymous//tests/Integra...nymousClassesTest.php$3 or GacelaTest\Benchmark\FileCache\ModuleB\FactoryB or GacelaTest\Benchmark\FileCache\ModuleD\FactoryD or GacelaTest\Benchmark\Mod...le\ModuleExampleFactory or GacelaTest\Benchmark\FileCache\ModuleF\Factory or anonymous//tests/Benchmark/GacelaGlobalBench.php$2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                    ->/** @scrutinizer ignore-call */ createDomainClass()
Loading history...
36
                    ->getConfigValues();
37
            }
38
39
            public function greet(string $name): string
40
            {
41
                return $this->getFactory()
42
                    ->createDomainClass()
43
                    ->greet($name);
44
            }
45
        };
46
47
        self::assertSame([1, 2, 3, 4, 5], $facade->getSomething());
48
        self::assertSame('Hello, Chema!', $facade->greet('Chema'));
49
    }
50
51
    /**
52
     * Using $this object as context.
53
     */
54
    private function registerConfig(): void
55
    {
56
        AnonymousGlobal::addGlobal(
57
            $this,
58
            new class() extends AbstractConfig {
59
                /**
60
                 * @return int[]
61
                 */
62
                public function getValues(): array
63
                {
64
                    return [1, 2, 3, 4, 5];
65
                }
66
            },
67
        );
68
    }
69
70
    /**
71
     * Using a string (class name) as context.
72
     */
73
    private function registerFactory(): void
74
    {
75
        AnonymousGlobal::addGlobal(
76
            'AnonymousClassesTest',
77
            new class() extends AbstractFactory {
78
                public function createDomainClass(): object
79
                {
80
                    /** @var object $myService */
81
                    $myService = $this->getProvidedDependency('my-greeter');
82
83
                    /** @var int[] $configValues */
84
                    $configValues = $this->getConfig()->getValues();
0 ignored issues
show
Bug introduced by
The method getValues() does not exist on Gacela\Framework\AbstractConfig. It seems like you code against a sub-type of Gacela\Framework\AbstractConfig such as anonymous//tests/Benchmark/GacelaGlobalBench.php$4 or anonymous//tests/Integra...nymousClassesTest.php$1 or GacelaTest\Benchmark\Mod...ple\ModuleExampleConfig. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                    $configValues = $this->getConfig()->/** @scrutinizer ignore-call */ getValues();
Loading history...
85
86
                    return new class($myService, ...$configValues) {
87
                        /** @var int[] */
88
                        private readonly array $configValues;
89
90
                        public function __construct(
91
                            private readonly object $myService,
92
                            int ...$configValues,
93
                        ) {
94
                            $this->configValues = $configValues;
0 ignored issues
show
Bug introduced by
The property configValues is declared read-only in anonymous//tests/Integra...nymousClassesTest.php$2.
Loading history...
95
                        }
96
97
                        public function getConfigValues(): array
98
                        {
99
                            return $this->configValues;
100
                        }
101
102
                        public function greet(string $name): string
103
                        {
104
                            return $this->myService->greet($name);
105
                        }
106
                    };
107
                }
108
            },
109
        );
110
    }
111
112
    private function registerAbstractProvider(): void
113
    {
114
        AnonymousGlobal::addGlobal(
115
            $this,
116
            new class() extends AbstractProvider {
117
                public function provideModuleDependencies(Container $container): void
118
                {
119
                    $container->set('my-greeter', new class() {
120
                        public function greet(string $name): string
121
                        {
122
                            return sprintf('Hello, %s!', $name);
123
                        }
124
                    });
125
                }
126
            },
127
        );
128
    }
129
}
130