GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f51b4f...d5d186 )
by Cees-Jan
10:52
created

Types   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C types() 0 27 7
A has() 0 6 1
A get() 0 10 2
A ensureTypes() 0 9 4
A reset() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\ResourceTestUtilities;
4
5
use Exception;
6
use Generator;
7
use RecursiveDirectoryIterator;
8
use RecursiveIteratorIterator;
9
use ReflectionClass;
10
11
class Types
12
{
13
    protected static $types = [];
14
15
    /**
16
     * @var bool
17
     */
18
    protected static $doneScanning = false;
19
20
    /**
21
     * @return Generator<Type>
0 ignored issues
show
Documentation introduced by
The doc-type Generator<Type> could not be parsed: Expected "|" or "end of type", but got "<" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
22
     */
23
    public static function types(): Generator
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
24
    {
25
        if (self::$doneScanning && count(self::$types) > 0) {
26
            yield from self::$types;
27
            return;
28
        }
29
30
        $path = __DIR__ . DIRECTORY_SEPARATOR . 'Type' . DIRECTORY_SEPARATOR;
31
        $directory = new RecursiveDirectoryIterator($path);
32
        $directory = new RecursiveIteratorIterator($directory);
33
        foreach ($directory as $node) {
34
            $nodePath = $node->getPath() . DIRECTORY_SEPARATOR . $node->getFilename();
35
            if (!is_file($nodePath)) {
36
                continue;
37
            }
38
39
            $fileName = str_replace('/', '\\', $nodePath);
40
            $class = __NAMESPACE__ . '\\Type\\' . substr(substr($fileName, strlen($path)), 0, -4);
41
            if (class_exists($class) && (new ReflectionClass($class))->implementsInterface(Type::class)) {
42
                $type = new $class;
43
                self::$types[$type->scalar()] = $type;
44
                yield $type;
45
            }
46
        }
47
48
        self::$doneScanning = true;
49
    }
50
51
    /**
52
     * @param string $type
53
     * @return bool
54
     */
55
    public static function has(string $type): bool
56
    {
57
        self::ensureTypes();
58
59
        return isset(self::$types[$type]);
60
    }
61
62
    /**
63
     * @param string $type
64
     * @return Type
65
     * @throws Exception
66
     */
67
    public static function get(string $type): Type
68
    {
69
        self::ensureTypes();
70
71
        if (isset(self::$types[$type])) {
72
            return self::$types[$type];
73
        }
74
75
        throw new Exception('Type "' . $type . '" not found, use has to check"');
76
    }
77
78
    /**
79
     * A wee bit hacky, but this ensures that when ever `has` or `get` is called before `types`
80
     * all types are detected and available for `has` and `get`.
81
     */
82
    protected function ensureTypes()
83
    {
84
        if (self::$doneScanning && count(self::$types) > 0) {
85
            return;
86
        }
87
88
        foreach (self::types() as $t) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
89
        }
90
    }
91
92
    public static function reset()
93
    {
94
        self::$types = [];
95
        self::$doneScanning = false;
96
    }
97
}
98