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.

HandlerContainer::add()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
namespace Thunder\Shortcode\HandlerContainer;
3
4
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
5
6
/**
7
 * @author Tomasz Kowalczyk <[email protected]>
8
 */
9
final class HandlerContainer implements HandlerContainerInterface
10
{
11
    /** @psalm-var array<string,callable(ShortcodeInterface):string> */
12
    private $handlers = array();
13
    /** @psalm-var (callable(ShortcodeInterface):string)|null */
14
    private $default = null;
15
16
    /**
17
     * @param string $name
18
     * @param callable $handler
19
     * @psalm-param callable(ShortcodeInterface):string $handler
20
     *
21
     * @return $this
22
     */
23 62
    public function add($name, $handler)
24
    {
25 62
        $this->guardHandler($handler);
26
27 61
        if (empty($name) || $this->has($name)) {
28 1
            $msg = 'Invalid name or duplicate shortcode handler for %s!';
29 1
            throw new \RuntimeException(sprintf($msg, $name));
30
        }
31
32 61
        $this->handlers[$name] = $handler;
33
34 61
        return $this;
35
    }
36
37
    /**
38
     * @param string $alias
39
     * @param string $name
40
     *
41
     * @return $this
42
     */
43 30
    public function addAlias($alias, $name)
44
    {
45 30
        if (false === $this->has($name)) {
46 1
            $msg = 'Failed to add an alias %s to non existent handler %s!';
47 1
            throw new \RuntimeException(sprintf($msg, $alias, $name));
48
        }
49
50
        /** @psalm-suppress PossiblyNullArgument */
51 29
        return $this->add($alias, $this->get($name));
0 ignored issues
show
Bug introduced by
It seems like $this->get($name) targeting Thunder\Shortcode\Handle...HandlerContainer::get() can also be of type null; however, Thunder\Shortcode\Handle...HandlerContainer::add() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
    }
53
54
    /**
55
     * @param string $name
56
     *
57
     * @return void
58
     */
59 3
    public function remove($name)
60
    {
61 3
        if (false === $this->has($name)) {
62 1
            $msg = 'Failed to remove non existent handler %s!';
63 1
            throw new \RuntimeException(sprintf($msg, $name));
64
        }
65
66 2
        unset($this->handlers[$name]);
67 2
    }
68
69
    /**
70
     * @param callable $handler
71
     * @psalm-param callable(ShortcodeInterface):string $handler
72
     *
73
     * @return $this
74
     */
75 4
    public function setDefault($handler)
76
    {
77 4
        $this->guardHandler($handler);
78
79 4
        $this->default = $handler;
80
81 4
        return $this;
82
    }
83
84
    /** @return string[] */
85 2
    public function getNames()
86
    {
87 2
        return array_keys($this->handlers);
88
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return callable|null
94
     * @psalm-return (callable(ShortcodeInterface):string)|null
95
     */
96 61
    public function get($name)
97
    {
98 61
        return $this->has($name) ? $this->handlers[$name] : $this->default;
99
    }
100
101
    /**
102
     * @param string $name
103
     *
104
     * @return bool
105
     */
106 66
    public function has($name)
107
    {
108 66
        return array_key_exists($name, $this->handlers);
109
    }
110
111
    /**
112
     * @param callable $handler
113
     *
114
     * @return void
115
     */
116 65
    private function guardHandler($handler)
117
    {
118 65
        if (!is_callable($handler)) {
119 1
            throw new \RuntimeException('Shortcode handler must be callable!');
120
        }
121 64
    }
122
}
123