Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\TabCompletion\Matcher;
|
Chris@13
|
13
|
Chris@13
|
14 abstract class AbstractDefaultParametersMatcher extends AbstractContextAwareMatcher
|
Chris@13
|
15 {
|
Chris@13
|
16 /**
|
Chris@13
|
17 * @param \ReflectionParameter[] $reflectionParameters
|
Chris@13
|
18 *
|
Chris@13
|
19 * @return array
|
Chris@13
|
20 */
|
Chris@13
|
21 public function getDefaultParameterCompletion(array $reflectionParameters)
|
Chris@13
|
22 {
|
Chris@13
|
23 $parametersProcessed = [];
|
Chris@13
|
24
|
Chris@13
|
25 foreach ($reflectionParameters as $parameter) {
|
Chris@13
|
26 if (!$parameter->isDefaultValueAvailable()) {
|
Chris@13
|
27 return [];
|
Chris@13
|
28 }
|
Chris@13
|
29
|
Chris@13
|
30 $defaultValue = $this->valueToShortString($parameter->getDefaultValue());
|
Chris@13
|
31
|
Chris@13
|
32 $parametersProcessed[] = "\${$parameter->getName()} = $defaultValue";
|
Chris@13
|
33 }
|
Chris@13
|
34
|
Chris@13
|
35 if (empty($parametersProcessed)) {
|
Chris@13
|
36 return [];
|
Chris@13
|
37 }
|
Chris@13
|
38
|
Chris@17
|
39 return [\implode(', ', $parametersProcessed) . ')'];
|
Chris@13
|
40 }
|
Chris@13
|
41
|
Chris@13
|
42 /**
|
Chris@13
|
43 * Takes in the default value of a parameter and turns it into a
|
Chris@13
|
44 * string representation that fits inline.
|
Chris@13
|
45 * This is not 100% true to the original (newlines are inlined, for example).
|
Chris@13
|
46 *
|
Chris@13
|
47 * @param mixed $value
|
Chris@13
|
48 *
|
Chris@13
|
49 * @return string
|
Chris@13
|
50 */
|
Chris@13
|
51 private function valueToShortString($value)
|
Chris@13
|
52 {
|
Chris@17
|
53 if (!\is_array($value)) {
|
Chris@17
|
54 return \json_encode($value);
|
Chris@13
|
55 }
|
Chris@13
|
56
|
Chris@13
|
57 $chunks = [];
|
Chris@13
|
58 $chunksSequential = [];
|
Chris@13
|
59
|
Chris@13
|
60 $allSequential = true;
|
Chris@13
|
61
|
Chris@13
|
62 foreach ($value as $key => $item) {
|
Chris@17
|
63 $allSequential = $allSequential && \is_numeric($key) && $key === \count($chunksSequential);
|
Chris@13
|
64
|
Chris@13
|
65 $keyString = $this->valueToShortString($key);
|
Chris@13
|
66 $itemString = $this->valueToShortString($item);
|
Chris@13
|
67
|
Chris@13
|
68 $chunks[] = "{$keyString} => {$itemString}";
|
Chris@13
|
69 $chunksSequential[] = $itemString;
|
Chris@13
|
70 }
|
Chris@13
|
71
|
Chris@13
|
72 $chunksToImplode = $allSequential ? $chunksSequential : $chunks;
|
Chris@13
|
73
|
Chris@17
|
74 return '[' . \implode(', ', $chunksToImplode) . ']';
|
Chris@13
|
75 }
|
Chris@13
|
76 }
|