Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 Justin Hileman
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Psy\Reflection;
13
 
14
/**
15
 * A fake ReflectionFunction but for language constructs.
16
 */
17
class ReflectionLanguageConstruct extends \ReflectionFunctionAbstract
18
{
19
    public $keyword;
20
 
21
    /**
22
     * Language construct parameter definitions.
23
     */
24
    private static $languageConstructs = [
25
        'isset' => [
26
            'var' => [],
27
            '...' => [
28
                'isOptional'   => true,
29
                'defaultValue' => null,
30
            ],
31
        ],
32
 
33
        'unset' => [
34
            'var' => [],
35
            '...' => [
36
                'isOptional'   => true,
37
                'defaultValue' => null,
38
            ],
39
        ],
40
 
41
        'empty' => [
42
            'var' => [],
43
        ],
44
 
45
        'echo' => [
46
            'arg1' => [],
47
            '...'  => [
48
                'isOptional'   => true,
49
                'defaultValue' => null,
50
            ],
51
        ],
52
 
53
        'print' => [
54
            'arg' => [],
55
        ],
56
 
57
        'die' => [
58
            'status' => [
59
                'isOptional'   => true,
60
                'defaultValue' => 0,
61
            ],
62
        ],
63
 
64
        'exit' => [
65
            'status' => [
66
                'isOptional'   => true,
67
                'defaultValue' => 0,
68
            ],
69
        ],
70
    ];
71
 
72
    /**
73
     * Construct a ReflectionLanguageConstruct object.
74
     *
75
     * @param string $keyword
76
     */
77
    public function __construct(string $keyword)
78
    {
79
        if (!self::isLanguageConstruct($keyword)) {
80
            throw new \InvalidArgumentException('Unknown language construct: '.$keyword);
81
        }
82
 
83
        $this->keyword = $keyword;
84
    }
85
 
86
    /**
87
     * This can't (and shouldn't) do anything :).
88
     *
89
     * @throws \RuntimeException
90
     */
91
    public static function export($name)
92
    {
93
        throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
94
    }
95
 
96
    /**
97
     * Get language construct name.
98
     *
99
     * @return string
100
     */
101
    public function getName(): string
102
    {
103
        return $this->keyword;
104
    }
105
 
106
    /**
107
     * None of these return references.
108
     *
109
     * @return bool
110
     */
111
    public function returnsReference(): bool
112
    {
113
        return false;
114
    }
115
 
116
    /**
117
     * Get language construct params.
118
     *
119
     * @return array
120
     */
121
    public function getParameters(): array
122
    {
123
        $params = [];
124
        foreach (self::$languageConstructs[$this->keyword] as $parameter => $opts) {
125
            $params[] = new ReflectionLanguageConstructParameter($this->keyword, $parameter, $opts);
126
        }
127
 
128
        return $params;
129
    }
130
 
131
    /**
132
     * Gets the file name from a language construct.
133
     *
134
     * (Hint: it always returns false)
135
     *
136
     * @todo remove \ReturnTypeWillChange attribute after dropping support for PHP 7.x (when we can use union types)
137
     *
138
     * @return string|false (false)
139
     */
140
    #[\ReturnTypeWillChange]
141
    public function getFileName()
142
    {
143
        return false;
144
    }
145
 
146
    /**
147
     * To string.
148
     *
149
     * @return string
150
     */
151
    public function __toString(): string
152
    {
153
        return $this->getName();
154
    }
155
 
156
    /**
157
     * Check whether keyword is a (known) language construct.
158
     *
159
     * @param string $keyword
160
     *
161
     * @return bool
162
     */
163
    public static function isLanguageConstruct(string $keyword): bool
164
    {
165
        return \array_key_exists($keyword, self::$languageConstructs);
166
    }
167
}