Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / vendor / psy / psysh / src / TabCompletion / Matcher / AbstractMatcher.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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_WHITESPACE = 'T_WHITESPACE';
35     const T_AND_EQUAL = 'T_AND_EQUAL';
36     const T_BOOLEAN_AND = 'T_BOOLEAN_AND';
37     const T_BOOLEAN_OR = 'T_BOOLEAN_OR';
38
39     const T_ENCAPSED_AND_WHITESPACE = 'T_ENCAPSED_AND_WHITESPACE';
40     const T_REQUIRE = 'T_REQUIRE';
41     const T_REQUIRE_ONCE = 'T_REQUIRE_ONCE';
42     const T_INCLUDE = 'T_INCLUDE';
43     const T_INCLUDE_ONCE = 'T_INCLUDE_ONCE';
44
45     /**
46      * Check whether this matcher can provide completions for $tokens.
47      *
48      * @param array $tokens Tokenized readline input
49      *
50      * @return bool
51      */
52     public function hasMatched(array $tokens)
53     {
54         return false;
55     }
56
57     /**
58      * Get current readline input word.
59      *
60      * @param array $tokens Tokenized readline input (see token_get_all)
61      *
62      * @return string
63      */
64     protected function getInput(array $tokens)
65     {
66         $var = '';
67         $firstToken = array_pop($tokens);
68         if (self::tokenIs($firstToken, self::T_STRING)) {
69             $var = $firstToken[1];
70         }
71
72         return $var;
73     }
74
75     /**
76      * Get current namespace and class (if any) from readline input.
77      *
78      * @param array $tokens Tokenized readline input (see token_get_all)
79      *
80      * @return string
81      */
82     protected function getNamespaceAndClass($tokens)
83     {
84         $class = '';
85         while (self::hasToken(
86             [self::T_NS_SEPARATOR, self::T_STRING],
87             $token = array_pop($tokens)
88         )) {
89             if (self::needCompleteClass($token)) {
90                 continue;
91             }
92
93             $class = $token[1] . $class;
94         }
95
96         return $class;
97     }
98
99     /**
100      * Provide tab completion matches for readline input.
101      *
102      * @param array $tokens information substracted with get_token_all
103      * @param array $info   readline_info object
104      *
105      * @return array The matches resulting from the query
106      */
107     abstract public function getMatches(array $tokens, array $info = []);
108
109     /**
110      * Check whether $word starts with $prefix.
111      *
112      * @param string $prefix
113      * @param string $word
114      *
115      * @return bool
116      */
117     public static function startsWith($prefix, $word)
118     {
119         return preg_match(sprintf('#^%s#', $prefix), $word);
120     }
121
122     /**
123      * Check whether $token matches a given syntax pattern.
124      *
125      * @param mixed  $token  A PHP token (see token_get_all)
126      * @param string $syntax A syntax pattern (default: variable pattern)
127      *
128      * @return bool
129      */
130     public static function hasSyntax($token, $syntax = self::VAR_SYNTAX)
131     {
132         if (!is_array($token)) {
133             return false;
134         }
135
136         $regexp = sprintf('#%s#', $syntax);
137
138         return (bool) preg_match($regexp, $token[1]);
139     }
140
141     /**
142      * Check whether $token type is $which.
143      *
144      * @param string $which A PHP token type
145      * @param mixed  $token A PHP token (see token_get_all)
146      *
147      * @return bool
148      */
149     public static function tokenIs($token, $which)
150     {
151         if (!is_array($token)) {
152             return false;
153         }
154
155         return token_name($token[0]) === $which;
156     }
157
158     /**
159      * Check whether $token is an operator.
160      *
161      * @param mixed $token A PHP token (see token_get_all)
162      *
163      * @return bool
164      */
165     public static function isOperator($token)
166     {
167         if (!is_string($token)) {
168             return false;
169         }
170
171         return strpos(self::MISC_OPERATORS, $token) !== false;
172     }
173
174     public static function needCompleteClass($token)
175     {
176         return in_array($token[1], ['doc', 'ls', 'show']);
177     }
178
179     /**
180      * Check whether $token type is present in $coll.
181      *
182      * @param array $coll  A list of token types
183      * @param mixed $token A PHP token (see token_get_all)
184      *
185      * @return bool
186      */
187     public static function hasToken(array $coll, $token)
188     {
189         if (!is_array($token)) {
190             return false;
191         }
192
193         return in_array(token_name($token[0]), $coll);
194     }
195 }