Passed
Push — allow-using-config-as-static-f... ( e5f7db )
by Chema
03:43
created

ConfigResolverAwareTrait::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\Config\ConfigNotFoundException;
8
use Gacela\Framework\ClassResolver\Config\ConfigResolver;
9
10
trait ConfigResolverAwareTrait
11
{
12
    private ?AbstractConfig $config = null;
13
14
    /**
15
     * Syntax sugar to access the config from static methods.
16
     */
17
    public static function config(): AbstractConfig
18
    {
19
        return (new static())->getConfig();
20
    }
21
22
    public function getConfig(): AbstractConfig
23
    {
24
        if ($this->config === null) {
25
            $this->config = $this->resolveConfig();
26
        }
27
28
        return $this->config;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config could return the type null which is incompatible with the type-hinted return Gacela\Framework\AbstractConfig. Consider adding an additional type-check to rule them out.
Loading history...
29
    }
30
31
    /**
32
     * @throws ConfigNotFoundException
33
     */
34
    private function resolveConfig(): AbstractConfig
35
    {
36
        return (new ConfigResolver())->resolve($this);
37
    }
38
}
39