Passed
Push — allow-using-static-facade ( abf22d )
by Chema
04:22
created

FactoryResolverAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactory() 0 7 2
A __callStatic() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
8
use RuntimeException;
9
10
/**
11
 * @method static AbstractFactory factory()
12
 */
13
trait FactoryResolverAwareTrait
14
{
15
    private ?AbstractFactory $factory = null;
16
17
    public static function __callStatic(string $name, array $arguments): AbstractFactory
18
    {
19
        if ($name === 'factory') {
20
            return (new static())->getFactory();
21
        }
22
23
        throw new RuntimeException('Unknown method: ' . $name);
24
    }
25
26
    protected function getFactory(): AbstractFactory
27
    {
28
        if ($this->factory === null) {
29
            $this->factory = (new FactoryResolver())->resolve($this);
30
        }
31
32
        return $this->factory;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->factory could return the type null which is incompatible with the type-hinted return Gacela\Framework\AbstractFactory. Consider adding an additional type-check to rule them out.
Loading history...
33
    }
34
}
35