1 | <?php |
||
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) |
|
53 | |||
54 | /** |
||
55 | * @param string $name |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | 3 | public function remove($name) |
|
68 | |||
69 | /** |
||
70 | * @param callable $handler |
||
71 | * @psalm-param callable(ShortcodeInterface):string $handler |
||
72 | * |
||
73 | * @return $this |
||
74 | */ |
||
75 | 4 | public function setDefault($handler) |
|
83 | |||
84 | /** @return string[] */ |
||
85 | 2 | public function getNames() |
|
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) |
|
100 | |||
101 | /** |
||
102 | * @param string $name |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | 66 | public function has($name) |
|
110 | |||
111 | /** |
||
112 | * @param callable $handler |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | 65 | private function guardHandler($handler) |
|
122 | } |
||
123 |
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.