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

ConfigResolverAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 27
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 3 1
A resolveConfig() 0 3 1
A getConfig() 0 7 2
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