4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\TabCompletion\Matcher;
14 abstract class AbstractDefaultParametersMatcher extends AbstractContextAwareMatcher
17 * @param \ReflectionParameter[] $reflectionParameters
21 public function getDefaultParameterCompletion(array $reflectionParameters)
23 $parametersProcessed = array();
25 foreach ($reflectionParameters as $parameter) {
26 if (!$parameter->isDefaultValueAvailable()) {
30 $defaultValue = $this->valueToShortString($parameter->getDefaultValue());
32 $parametersProcessed[] = "\${$parameter->getName()} = $defaultValue";
35 if (empty($parametersProcessed)) {
39 return array(implode(', ', $parametersProcessed) . ')');
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).
51 private function valueToShortString($value)
53 if (!is_array($value)) {
54 return json_encode($value);
58 $chunksSequential = array();
60 $allSequential = true;
62 foreach ($value as $key => $item) {
63 $allSequential = $allSequential && is_numeric($key) && $key === count($chunksSequential);
65 $keyString = $this->valueToShortString($key);
66 $itemString = $this->valueToShortString($item);
68 $chunks[] = "{$keyString} => {$itemString}";
69 $chunksSequential[] = $itemString;
72 $chunksToImplode = $allSequential ? $chunksSequential : $chunks;
74 return '[' . implode(', ', $chunksToImplode) . ']';