Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
    public function getName(): string
100
    {
101
        return $this->keyword;
102
    }
103
 
104
    /**
105
     * None of these return references.
106
     */
107
    public function returnsReference(): bool
108
    {
109
        return false;
110
    }
111
 
112
    /**
113
     * Get language construct params.
114
     *
115
     * @return array
116
     */
117
    public function getParameters(): array
118
    {
119
        $params = [];
120
        foreach (self::$languageConstructs[$this->keyword] as $parameter => $opts) {
121
            $params[] = new ReflectionLanguageConstructParameter($this->keyword, $parameter, $opts);
122
        }
123
 
124
        return $params;
125
    }
126
 
127
    /**
128
     * Gets the file name from a language construct.
129
     *
130
     * (Hint: it always returns false)
131
     *
132
     * @todo remove \ReturnTypeWillChange attribute after dropping support for PHP 7.x (when we can use union types)
133
     *
134
     * @return string|false (false)
135
     */
136
    #[\ReturnTypeWillChange]
137
    public function getFileName()
138
    {
139
        return false;
140
    }
141
 
142
    /**
143
     * To string.
144
     */
145
    public function __toString(): string
146
    {
147
        return $this->getName();
148
    }
149
 
150
    /**
151
     * Check whether keyword is a (known) language construct.
152
     *
153
     * @param string $keyword
154
     */
155
    public static function isLanguageConstruct(string $keyword): bool
156
    {
157
        return \array_key_exists($keyword, self::$languageConstructs);
158
    }
159
}