3d7b4214b65bbd13c8bcb8419b79b940addcc3e0
[yaffs-website] / vendor / psy / psysh / src / Psy / TabCompletion / Matcher / AbstractDefaultParametersMatcher.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 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 abstract class AbstractDefaultParametersMatcher extends AbstractContextAwareMatcher
15 {
16     /**
17      * @param \ReflectionParameter[] $reflectionParameters
18      *
19      * @return array
20      */
21     public function getDefaultParameterCompletion(array $reflectionParameters)
22     {
23         $parametersProcessed = array();
24
25         foreach ($reflectionParameters as $parameter) {
26             if (!$parameter->isDefaultValueAvailable()) {
27                 return array();
28             }
29
30             $defaultValue = $this->valueToShortString($parameter->getDefaultValue());
31
32             $parametersProcessed[] = "\${$parameter->getName()} = $defaultValue";
33         }
34
35         if (empty($parametersProcessed)) {
36             return array();
37         }
38
39         return array(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      * @return string
50      */
51     private function valueToShortString($value)
52     {
53         if (!is_array($value)) {
54             return json_encode($value);
55         }
56
57         $chunks = array();
58         $chunksSequential = array();
59
60         $allSequential = true;
61
62         foreach ($value as $key => $item) {
63             $allSequential = $allSequential && is_numeric($key) && $key === count($chunksSequential);
64
65             $keyString  = $this->valueToShortString($key);
66             $itemString = $this->valueToShortString($item);
67
68             $chunks[] = "{$keyString} => {$itemString}";
69             $chunksSequential[] = $itemString;
70         }
71
72         $chunksToImplode = $allSequential ? $chunksSequential : $chunks;
73
74         return '[' . implode(', ', $chunksToImplode) . ']';
75     }
76 }