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
abstract class AbstractDefaultParametersMatcher extends AbstractContextAwareMatcher
15
{
16
    /**
17
     * @param \ReflectionParameter[] $reflectionParameters
18
     *
19
     * @return array
20
     */
21
    public function getDefaultParameterCompletion(array $reflectionParameters): array
22
    {
23
        $parametersProcessed = [];
24
 
25
        foreach ($reflectionParameters as $parameter) {
26
            if (!$parameter->isDefaultValueAvailable()) {
27
                return [];
28
            }
29
 
30
            $defaultValue = $this->valueToShortString($parameter->getDefaultValue());
31
 
32
            $parametersProcessed[] = \sprintf('$%s = %s', $parameter->getName(), $defaultValue);
33
        }
34
 
35
        if (empty($parametersProcessed)) {
36
            return [];
37
        }
38
 
39
        return [\implode(', ', $parametersProcessed).')'];
40
    }
41
 
42
    /**
43
     * Takes in the default value of a parameter and turns it into a
44
     *  string representation that fits inline.
45
     * This is not 100% true to the original (newlines are inlined, for example).
46
     *
47
     * @param mixed $value
48
     */
49
    private function valueToShortString($value): string
50
    {
51
        if (!\is_array($value)) {
52
            return \json_encode($value);
53
        }
54
 
55
        $chunks = [];
56
        $chunksSequential = [];
57
 
58
        $allSequential = true;
59
 
60
        foreach ($value as $key => $item) {
61
            $allSequential = $allSequential && \is_numeric($key) && $key === \count($chunksSequential);
62
 
63
            $keyString = $this->valueToShortString($key);
64
            $itemString = $this->valueToShortString($item);
65
 
66
            $chunks[] = "{$keyString} => {$itemString}";
67
            $chunksSequential[] = $itemString;
68
        }
69
 
70
        $chunksToImplode = $allSequential ? $chunksSequential : $chunks;
71
 
72
        return '['.\implode(', ', $chunksToImplode).']';
73
    }
74
}