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.

FlyweightInput   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 74
ccs 13
cts 13
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A named() 0 8 2
A any() 0 8 2
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Noodle\Transition\Input;
6
7
/**
8
 * FlyweightInput will reuse inputs it has previously created with the same name
9
 * rather than create new ones. This results in some memory and performance savings
10
 * in applications that would otherwise have many repeated "new Input('...')", and
11
 * it allows for easier comparison of two Inputs, if necessary.
12
 *
13
 * @see https://en.wikipedia.org/wiki/Flyweight_pattern
14
 */
15
final class FlyweightInput implements CreatesWildcard, CreatesWithName, Input
16
{
17
    /**
18
     * The name of the input
19
     *
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * Instances of inputs created by the FlyweightInput factory method
26
     *
27
     * @var Input[]
28
     */
29
    private static $instances = [];
30
31
    /**
32
     * The "wildcard" input, indicating "any" input when doing transition events.
33
     *
34
     * @var Input
35
     */
36
    private static $wildcard;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param string $name
42
     */
43 6
    private function __construct(string $name)
44
    {
45 6
        $this->name = $name;
46 6
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 18
    public function getName() : string
52
    {
53 18
        return $this->name;
54
    }
55
56
    /**
57
     * Creates and returns an input with the given name, or, if such an input
58
     * has already been created, returns it from the instance cache.
59
     *
60
     * @param string $name
61
     *
62
     * @return Input
63
     */
64 19
    public static function named(string $name) : Input
65
    {
66 19
        if (!isset(self::$instances[$name])) {
67 5
            self::$instances[$name] = new self($name);
68
        }
69
70 19
        return self::$instances[$name];
71
    }
72
73
    /**
74
     * Creates and returns an input with a long, random, name in hex to be
75
     * designated the "any" / "wildcard" state, or, if such an input has
76
     * already been created, returns it.
77
     *
78
     * @return Input
79
     */
80 9
    public static function any() : Input
81
    {
82 9
        if (!isset(self::$wildcard)) {
83 1
            self::$wildcard = new self(bin2hex(random_bytes(20)));
84
        }
85
86 9
        return self::$wildcard;
87
    }
88
}
89