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

FactoryResolverAwareTrait::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
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\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