Passed
Push — master ( 178b22...96b0ca )
by Mariano
04:20
created

FactoryClass::asString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Mcustiel\Phiremock\Codeception\Util;
4
5
use Codeception\Exception\ConfigurationException;
6
use Mcustiel\Phiremock\Client\Factory;
7
8
class FactoryClass
9
{
10
    /** @var class-string<Factory> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Factory> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Factory>.
Loading history...
11
    private $factory;
12
13
    /**
14
     * @param string|class-string<Factory> $classNameConfig
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
15
     * @throws ConfigurationException
16
     */
17
    public function __construct(string $classNameConfig)
18
    {
19
        $this->factory = $this->getClassNameFromConfig($classNameConfig);
20
    }
21
22
    public function getInstance(): Factory
23
    {
24
        /** @var class-string<Factory> $factory */
25
        $factory = $this->factory;
26
        return $factory::createDefault();
27
    }
28
29
    /**
30
     * @return string|class-string<Factory>
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
31
     */
32
    public function asString(): string
33
    {
34
        return $this->factory;
35
    }
36
37
    /**
38
     * @throws ConfigurationException
39
     * @return string|class-string<Factory>
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
40
     */
41
    private function getClassNameFromConfig(string $className): string
42
    {
43
        if ($className !== 'default') {
44
            if (!is_a($className, Factory::class, true)) {
45
                throw new ConfigurationException(
46
                    sprintf('%s does not extend %s', $className, Factory::class)
47
                );
48
            }
49
            return $className;
50
        }
51
        return Factory::class;
52
    }
53
}
54