Completed
Push — master ( 84a7d6...51cd60 )
by Nicolai
08:39 queued 06:45
created

BaseClassResolver::getBaseClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Generator\Test;
5
6
use InvalidArgumentException;
7
use function class_exists;
8
9
/**
10
 * Class BaseClassResolver
11
 *
12
 * @package SmartWeb\ModuleTesting\Generator\Test
13
 */
14
class BaseClassResolver implements BaseClassResolverInterface
15
{
16
    
17
    /**
18
     * Name used to group global base classes
19
     */
20
    const DEFAULT_MODULE = '_default';
21
    
22
    /**
23
     * @var string[][][]
24
     */
25
    private static $baseClasses = [];
26
    
27
    /**
28
     * @var string
29
     */
30
    protected $module;
31
    
32
    /**
33
     * @var string
34
     */
35
    protected $type;
36
    
37
    /**
38
     * @var string
39
     */
40
    protected $suite;
41
    
42
    /**
43
     * BaseClassResolver constructor.
44
     *
45
     * @param CodeceptionTestType $type
46
     * @param string              $suite
47
     * @param string|null         $module
48
     */
49
    public function __construct(CodeceptionTestType $type, string $suite, string $module = null)
50
    {
51
        $this->type = $type->key();
52
        $this->suite = $suite;
53
        $this->module = $module ?? self::DEFAULT_MODULE;
54
    }
55
    
56
    /**
57
     * @inheritDoc
58
     */
59
    public function registerBaseClass($baseClass)
60
    {
61
        $this->validateClass($baseClass);
62
        
63
        static::$baseClasses[$this->type][$this->suite][$this->module] = $baseClass;
0 ignored issues
show
Bug introduced by
Since $baseClasses is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $baseClasses to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
64
    }
65
    
66
    /**
67
     * @inheritDoc
68
     */
69
    public function getBaseClass() : string
70
    {
71
        if (isset(static::$baseClasses[$this->type][$this->suite][$this->module])) {
0 ignored issues
show
Bug introduced by
Since $baseClasses is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $baseClasses to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
72
            return static::$baseClasses[$this->type][$this->suite][$this->module];
0 ignored issues
show
Bug introduced by
Since $baseClasses is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $baseClasses to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
73
        }
74
        
75
        throw new InvalidArgumentException("No base class is set for type {$this->type}, suite {$this->suite}, module {$this->module}");
76
    }
77
    
78
    /**
79
     * @inheritDoc
80
     */
81
    public static function registerBaseClassForModule(CodeceptionTestType $type, string $suite, string $module, $baseClass)
82
    {
83
        (new static($type, $suite, $module))->registerBaseClass($baseClass);
84
    }
85
    
86
    /**
87
     * @inheritDoc
88
     */
89
    public static function getBaseClassForModule(CodeceptionTestType $type, string $suite, string $module) : string
90
    {
91
        return (new static($type, $suite, $module))->getBaseClass();
92
    }
93
    
94
    /**
95
     * @param string $class
96
     *
97
     * @throws BaseClassDoesNotExist
98
     */
99
    protected function validateClass(string $class)
100
    {
101
        if (!class_exists($class)) {
102
            throw new BaseClassDoesNotExist($class);
103
        }
104
    }
105
    
106
}
107