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\Formatter;
14 use Psy\Reflection\ReflectionConstant;
15 use Psy\Reflection\ReflectionLanguageConstruct;
17 use Symfony\Component\Console\Formatter\OutputFormatter;
20 * An abstract representation of a function, class or property signature.
22 class SignatureFormatter implements Formatter
25 * Format a signature for the given reflector.
27 * Defers to subclasses to do the actual formatting.
29 * @param \Reflector $reflector
31 * @return string Formatted signature
33 public static function format(\Reflector $reflector)
36 case $reflector instanceof \ReflectionFunction:
37 case $reflector instanceof ReflectionLanguageConstruct:
38 return self::formatFunction($reflector);
40 // this case also covers \ReflectionObject:
41 case $reflector instanceof \ReflectionClass:
42 return self::formatClass($reflector);
44 case $reflector instanceof ReflectionConstant:
45 return self::formatConstant($reflector);
47 case $reflector instanceof \ReflectionMethod:
48 return self::formatMethod($reflector);
50 case $reflector instanceof \ReflectionProperty:
51 return self::formatProperty($reflector);
54 throw new \InvalidArgumentException('Unexpected Reflector class: ' . get_class($reflector));
59 * Print the signature name.
61 * @param \Reflector $reflector
63 * @return string Formatted name
65 public static function formatName(\Reflector $reflector)
67 return $reflector->getName();
71 * Print the method, property or class modifiers.
73 * Technically this should be a trait. Can't wait for 5.4 :)
75 * @param \Reflector $reflector
77 * @return string Formatted modifiers
79 private static function formatModifiers(\Reflector $reflector)
81 return implode(' ', array_map(function ($modifier) {
82 return sprintf('<keyword>%s</keyword>', $modifier);
83 }, \Reflection::getModifierNames($reflector->getModifiers())));
87 * Format a class signature.
89 * @param \ReflectionClass $reflector
91 * @return string Formatted signature
93 private static function formatClass(\ReflectionClass $reflector)
97 if ($modifiers = self::formatModifiers($reflector)) {
98 $chunks[] = $modifiers;
101 if (version_compare(PHP_VERSION, '5.4', '>=') && $reflector->isTrait()) {
104 $chunks[] = $reflector->isInterface() ? 'interface' : 'class';
107 $chunks[] = sprintf('<class>%s</class>', self::formatName($reflector));
109 if ($parent = $reflector->getParentClass()) {
110 $chunks[] = 'extends';
111 $chunks[] = sprintf('<class>%s</class>', $parent->getName());
114 $interfaces = $reflector->getInterfaceNames();
115 if (!empty($interfaces)) {
118 $chunks[] = 'implements';
119 $chunks[] = implode(', ', array_map(function ($name) {
120 return sprintf('<class>%s</class>', $name);
124 return implode(' ', $chunks);
128 * Format a constant signature.
130 * @param ReflectionConstant $reflector
132 * @return string Formatted signature
134 private static function formatConstant(ReflectionConstant $reflector)
136 $value = $reflector->getValue();
137 $style = self::getTypeStyle($value);
140 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>',
141 self::formatName($reflector),
143 OutputFormatter::escape(Json::encode($value)),
149 * Helper for getting output style for a given value's type.
151 * @param mixed $value
155 private static function getTypeStyle($value)
157 if (is_int($value) || is_float($value)) {
159 } elseif (is_string($value)) {
161 } elseif (is_bool($value) || is_null($value)) {
169 * Format a property signature.
171 * @param \ReflectionProperty $reflector
173 * @return string Formatted signature
175 private static function formatProperty(\ReflectionProperty $reflector)
178 '%s <strong>$%s</strong>',
179 self::formatModifiers($reflector),
180 $reflector->getName()
185 * Format a function signature.
187 * @param \ReflectionFunction $reflector
189 * @return string Formatted signature
191 private static function formatFunction(\ReflectionFunctionAbstract $reflector)
194 '<keyword>function</keyword> %s<function>%s</function>(%s)',
195 $reflector->returnsReference() ? '&' : '',
196 self::formatName($reflector),
197 implode(', ', self::formatFunctionParams($reflector))
202 * Format a method signature.
204 * @param \ReflectionMethod $reflector
206 * @return string Formatted signature
208 private static function formatMethod(\ReflectionMethod $reflector)
212 self::formatModifiers($reflector),
213 self::formatFunction($reflector)
218 * Print the function params.
220 * @param \ReflectionFunctionAbstract $reflector
224 private static function formatFunctionParams(\ReflectionFunctionAbstract $reflector)
227 foreach ($reflector->getParameters() as $param) {
230 if ($param->isArray()) {
231 $hint = '<keyword>array</keyword> ';
232 } elseif ($class = $param->getClass()) {
233 $hint = sprintf('<class>%s</class> ', $class->getName());
235 } catch (\Exception $e) {
236 // sometimes we just don't know...
237 // bad class names, or autoloaded classes that haven't been loaded yet, or whathaveyou.
238 // come to think of it, the only time I've seen this is with the intl extension.
240 // Hax: we'll try to extract it :P
241 $chunks = explode('$' . $param->getName(), (string) $param);
242 $chunks = explode(' ', trim($chunks[0]));
243 $guess = end($chunks);
245 $hint = sprintf('<urgent>%s</urgent> ', $guess);
248 if ($param->isOptional()) {
249 if (!$param->isDefaultValueAvailable()) {
251 $typeStyle = 'urgent';
253 $value = $param->getDefaultValue();
254 $typeStyle = self::getTypeStyle($value);
255 $value = is_array($value) ? 'array()' : is_null($value) ? 'null' : var_export($value, true);
257 $default = sprintf(' = <%s>%s</%s>', $typeStyle, OutputFormatter::escape($value), $typeStyle);
263 '%s%s<strong>$%s</strong>%s',
264 $param->isPassedByReference() ? '&' : '',