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\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
     *
200 lars 51
     * @return false
148 lars 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
    protected function getInput(array $tokens): string
64
    {
65
        $var = '';
66
        $firstToken = \array_pop($tokens);
67
        if (self::tokenIs($firstToken, self::T_STRING)) {
68
            $var = $firstToken[1];
69
        }
70
 
71
        return $var;
72
    }
73
 
74
    /**
75
     * Get current namespace and class (if any) from readline input.
76
     *
77
     * @param array $tokens Tokenized readline input (see token_get_all)
78
     */
79
    protected function getNamespaceAndClass(array $tokens): string
80
    {
81
        $class = '';
82
        while (self::hasToken(
83
            [self::T_NS_SEPARATOR, self::T_STRING, self::T_NAME_QUALIFIED],
84
            $token = \array_pop($tokens)
85
        )) {
86
            if (self::needCompleteClass($token)) {
87
                continue;
88
            }
89
 
90
            $class = $token[1].$class;
91
        }
92
 
93
        return $class;
94
    }
95
 
96
    /**
97
     * Provide tab completion matches for readline input.
98
     *
99
     * @param array $tokens information substracted with get_token_all
100
     * @param array $info   readline_info object
101
     *
102
     * @return array The matches resulting from the query
103
     */
104
    abstract public function getMatches(array $tokens, array $info = []): array;
105
 
106
    /**
107
     * Check whether $word starts with $prefix.
108
     *
109
     * @param string $prefix
110
     * @param string $word
111
     */
112
    public static function startsWith(string $prefix, string $word): bool
113
    {
114
        return \preg_match(\sprintf('#^%s#', $prefix), $word);
115
    }
116
 
117
    /**
118
     * Check whether $token matches a given syntax pattern.
119
     *
120
     * @param mixed  $token  A PHP token (see token_get_all)
121
     * @param string $syntax A syntax pattern (default: variable pattern)
122
     */
123
    public static function hasSyntax($token, string $syntax = self::VAR_SYNTAX): bool
124
    {
125
        if (!\is_array($token)) {
126
            return false;
127
        }
128
 
129
        $regexp = \sprintf('#%s#', $syntax);
130
 
131
        return (bool) \preg_match($regexp, $token[1]);
132
    }
133
 
134
    /**
135
     * Check whether $token type is $which.
136
     *
137
     * @param mixed  $token A PHP token (see token_get_all)
138
     * @param string $which A PHP token type
139
     */
140
    public static function tokenIs($token, string $which): bool
141
    {
142
        if (!\is_array($token)) {
143
            return false;
144
        }
145
 
146
        return \token_name($token[0]) === $which;
147
    }
148
 
149
    /**
150
     * Check whether $token is an operator.
151
     *
152
     * @param mixed $token A PHP token (see token_get_all)
153
     */
154
    public static function isOperator($token): bool
155
    {
156
        if (!\is_string($token)) {
157
            return false;
158
        }
159
 
160
        return \strpos(self::MISC_OPERATORS, $token) !== false;
161
    }
162
 
163
    public static function needCompleteClass($token): bool
164
    {
165
        return \in_array($token[1], ['doc', 'ls', 'show']);
166
    }
167
 
168
    /**
169
     * Check whether $token type is present in $coll.
170
     *
171
     * @param array $coll  A list of token types
172
     * @param mixed $token A PHP token (see token_get_all)
173
     */
174
    public static function hasToken(array $coll, $token): bool
175
    {
176
        if (!\is_array($token)) {
177
            return false;
178
        }
179
 
180
        return \in_array(\token_name($token[0]), $coll);
181
    }
182
}