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\TabCompletion\Matcher;
13
 
14
/**
15
 * Abstract tab completion Matcher.
16
 *
17
 * @author Marc Garcia <markcial@gmail.com>
18
 */
19
abstract class AbstractMatcher
20
{
21
    /** Syntax types */
22
    const CONSTANT_SYNTAX = '^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$';
23
    const VAR_SYNTAX = '^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$';
24
    const MISC_OPERATORS = '+-*/^|&';
25
    /** Token values */
26
    const T_OPEN_TAG = 'T_OPEN_TAG';
27
    const T_VARIABLE = 'T_VARIABLE';
28
    const T_OBJECT_OPERATOR = 'T_OBJECT_OPERATOR';
29
    const T_DOUBLE_COLON = 'T_DOUBLE_COLON';
30
    const T_NEW = 'T_NEW';
31
    const T_CLONE = 'T_CLONE';
32
    const T_NS_SEPARATOR = 'T_NS_SEPARATOR';
33
    const T_STRING = 'T_STRING';
34
    const T_NAME_QUALIFIED = 'T_NAME_QUALIFIED';
35
    const T_WHITESPACE = 'T_WHITESPACE';
36
    const T_AND_EQUAL = 'T_AND_EQUAL';
37
    const T_BOOLEAN_AND = 'T_BOOLEAN_AND';
38
    const T_BOOLEAN_OR = 'T_BOOLEAN_OR';
39
 
40
    const T_ENCAPSED_AND_WHITESPACE = 'T_ENCAPSED_AND_WHITESPACE';
41
    const T_REQUIRE = 'T_REQUIRE';
42
    const T_REQUIRE_ONCE = 'T_REQUIRE_ONCE';
43
    const T_INCLUDE = 'T_INCLUDE';
44
    const T_INCLUDE_ONCE = 'T_INCLUDE_ONCE';
45
 
46
    /**
47
     * Check whether this matcher can provide completions for $tokens.
48
     *
49
     * @param array $tokens Tokenized readline input
50
     *
51
     * @return bool
52
     */
53
    public function hasMatched(array $tokens): bool
54
    {
55
        return false;
56
    }
57
 
58
    /**
59
     * Get current readline input word.
60
     *
61
     * @param array $tokens Tokenized readline input (see token_get_all)
62
     *
63
     * @return string
64
     */
65
    protected function getInput(array $tokens): string
66
    {
67
        $var = '';
68
        $firstToken = \array_pop($tokens);
69
        if (self::tokenIs($firstToken, self::T_STRING)) {
70
            $var = $firstToken[1];
71
        }
72
 
73
        return $var;
74
    }
75
 
76
    /**
77
     * Get current namespace and class (if any) from readline input.
78
     *
79
     * @param array $tokens Tokenized readline input (see token_get_all)
80
     *
81
     * @return string
82
     */
83
    protected function getNamespaceAndClass(array $tokens): string
84
    {
85
        $class = '';
86
        while (self::hasToken(
87
            [self::T_NS_SEPARATOR, self::T_STRING, self::T_NAME_QUALIFIED],
88
            $token = \array_pop($tokens)
89
        )) {
90
            if (self::needCompleteClass($token)) {
91
                continue;
92
            }
93
 
94
            $class = $token[1].$class;
95
        }
96
 
97
        return $class;
98
    }
99
 
100
    /**
101
     * Provide tab completion matches for readline input.
102
     *
103
     * @param array $tokens information substracted with get_token_all
104
     * @param array $info   readline_info object
105
     *
106
     * @return array The matches resulting from the query
107
     */
108
    abstract public function getMatches(array $tokens, array $info = []): array;
109
 
110
    /**
111
     * Check whether $word starts with $prefix.
112
     *
113
     * @param string $prefix
114
     * @param string $word
115
     *
116
     * @return bool
117
     */
118
    public static function startsWith(string $prefix, string $word): bool
119
    {
120
        return \preg_match(\sprintf('#^%s#', $prefix), $word);
121
    }
122
 
123
    /**
124
     * Check whether $token matches a given syntax pattern.
125
     *
126
     * @param mixed  $token  A PHP token (see token_get_all)
127
     * @param string $syntax A syntax pattern (default: variable pattern)
128
     *
129
     * @return bool
130
     */
131
    public static function hasSyntax($token, string $syntax = self::VAR_SYNTAX): bool
132
    {
133
        if (!\is_array($token)) {
134
            return false;
135
        }
136
 
137
        $regexp = \sprintf('#%s#', $syntax);
138
 
139
        return (bool) \preg_match($regexp, $token[1]);
140
    }
141
 
142
    /**
143
     * Check whether $token type is $which.
144
     *
145
     * @param mixed  $token A PHP token (see token_get_all)
146
     * @param string $which A PHP token type
147
     *
148
     * @return bool
149
     */
150
    public static function tokenIs($token, string $which): bool
151
    {
152
        if (!\is_array($token)) {
153
            return false;
154
        }
155
 
156
        return \token_name($token[0]) === $which;
157
    }
158
 
159
    /**
160
     * Check whether $token is an operator.
161
     *
162
     * @param mixed $token A PHP token (see token_get_all)
163
     *
164
     * @return bool
165
     */
166
    public static function isOperator($token): bool
167
    {
168
        if (!\is_string($token)) {
169
            return false;
170
        }
171
 
172
        return \strpos(self::MISC_OPERATORS, $token) !== false;
173
    }
174
 
175
    public static function needCompleteClass($token): bool
176
    {
177
        return \in_array($token[1], ['doc', 'ls', 'show']);
178
    }
179
 
180
    /**
181
     * Check whether $token type is present in $coll.
182
     *
183
     * @param array $coll  A list of token types
184
     * @param mixed $token A PHP token (see token_get_all)
185
     *
186
     * @return bool
187
     */
188
    public static function hasToken(array $coll, $token): bool
189
    {
190
        if (!\is_array($token)) {
191
            return false;
192
        }
193
 
194
        return \in_array(\token_name($token[0]), $coll);
195
    }
196
}