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
 * A class attribute tab completion Matcher.
16
 *
17
 * Given a namespace and class, this matcher provides completion for constants
18
 * and static properties.
19
 *
20
 * @author Marc Garcia <markcial@gmail.com>
21
 */
22
class ClassAttributesMatcher extends AbstractMatcher
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getMatches(array $tokens, array $info = []): array
28
    {
29
        $input = $this->getInput($tokens);
30
 
31
        $firstToken = \array_pop($tokens);
32
        if (self::tokenIs($firstToken, self::T_STRING)) {
33
            // second token is the nekudotayim operator
34
            \array_pop($tokens);
35
        }
36
 
37
        $class = $this->getNamespaceAndClass($tokens);
38
 
39
        try {
40
            $reflection = new \ReflectionClass($class);
41
        } catch (\ReflectionException $re) {
42
            return [];
43
        }
44
 
45
        $vars = \array_merge(
46
            \array_map(
47
                function ($var) {
48
                    return '$'.$var;
49
                },
50
                \array_keys($reflection->getStaticProperties())
51
            ),
52
            \array_keys($reflection->getConstants())
53
        );
54
 
55
        return \array_map(
56
            function ($name) use ($class) {
57
                $chunks = \explode('\\', $class);
58
                $className = \array_pop($chunks);
59
 
60
                return $className.'::'.$name;
61
            },
62
            \array_filter(
63
                $vars,
64
                function ($var) use ($input) {
65
                    return AbstractMatcher::startsWith($input, $var);
66
                }
67
            )
68
        );
69
    }
70
 
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function hasMatched(array $tokens): bool
75
    {
76
        $token = \array_pop($tokens);
77
        $prevToken = \array_pop($tokens);
78
 
79
        switch (true) {
80
            case self::tokenIs($prevToken, self::T_DOUBLE_COLON) && self::tokenIs($token, self::T_STRING):
81
            case self::tokenIs($token, self::T_DOUBLE_COLON):
82
                return true;
83
        }
84
 
85
        return false;
86
    }
87
}