4 * This file is part of Psy Shell.
6 * (c) 2012-2018 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 if ($reflector instanceof \ReflectionClass && $reflector->isTrait()) {
82 // For some reason, PHP 5.x returns `abstract public` modifiers for
83 // traits. Let's just ignore that business entirely.
84 if (version_compare(PHP_VERSION, '7.0.0', '<')) {
89 return implode(' ', array_map(function ($modifier) {
90 return sprintf('<keyword>%s</keyword>', $modifier);
91 }, \Reflection::getModifierNames($reflector->getModifiers())));
95 * Format a class signature.
97 * @param \ReflectionClass $reflector
99 * @return string Formatted signature
101 private static function formatClass(\ReflectionClass $reflector)
105 if ($modifiers = self::formatModifiers($reflector)) {
106 $chunks[] = $modifiers;
109 if ($reflector->isTrait()) {
112 $chunks[] = $reflector->isInterface() ? 'interface' : 'class';
115 $chunks[] = sprintf('<class>%s</class>', self::formatName($reflector));
117 if ($parent = $reflector->getParentClass()) {
118 $chunks[] = 'extends';
119 $chunks[] = sprintf('<class>%s</class>', $parent->getName());
122 $interfaces = $reflector->getInterfaceNames();
123 if (!empty($interfaces)) {
126 $chunks[] = 'implements';
127 $chunks[] = implode(', ', array_map(function ($name) {
128 return sprintf('<class>%s</class>', $name);
132 return implode(' ', $chunks);
136 * Format a constant signature.
138 * @param ReflectionConstant $reflector
140 * @return string Formatted signature
142 private static function formatConstant(ReflectionConstant $reflector)
144 $value = $reflector->getValue();
145 $style = self::getTypeStyle($value);
148 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>',
149 self::formatName($reflector),
151 OutputFormatter::escape(Json::encode($value)),
157 * Helper for getting output style for a given value's type.
159 * @param mixed $value
163 private static function getTypeStyle($value)
165 if (is_int($value) || is_float($value)) {
167 } elseif (is_string($value)) {
169 } elseif (is_bool($value) || is_null($value)) {
172 return 'strong'; // @codeCoverageIgnore
177 * Format a property signature.
179 * @param \ReflectionProperty $reflector
181 * @return string Formatted signature
183 private static function formatProperty(\ReflectionProperty $reflector)
186 '%s <strong>$%s</strong>',
187 self::formatModifiers($reflector),
188 $reflector->getName()
193 * Format a function signature.
195 * @param \ReflectionFunction $reflector
197 * @return string Formatted signature
199 private static function formatFunction(\ReflectionFunctionAbstract $reflector)
202 '<keyword>function</keyword> %s<function>%s</function>(%s)',
203 $reflector->returnsReference() ? '&' : '',
204 self::formatName($reflector),
205 implode(', ', self::formatFunctionParams($reflector))
210 * Format a method signature.
212 * @param \ReflectionMethod $reflector
214 * @return string Formatted signature
216 private static function formatMethod(\ReflectionMethod $reflector)
220 self::formatModifiers($reflector),
221 self::formatFunction($reflector)
226 * Print the function params.
228 * @param \ReflectionFunctionAbstract $reflector
232 private static function formatFunctionParams(\ReflectionFunctionAbstract $reflector)
235 foreach ($reflector->getParameters() as $param) {
238 if ($param->isArray()) {
239 $hint = '<keyword>array</keyword> ';
240 } elseif ($class = $param->getClass()) {
241 $hint = sprintf('<class>%s</class> ', $class->getName());
243 } catch (\Exception $e) {
244 // sometimes we just don't know...
245 // bad class names, or autoloaded classes that haven't been loaded yet, or whathaveyou.
246 // come to think of it, the only time I've seen this is with the intl extension.
248 // Hax: we'll try to extract it :P
250 // @codeCoverageIgnoreStart
251 $chunks = explode('$' . $param->getName(), (string) $param);
252 $chunks = explode(' ', trim($chunks[0]));
253 $guess = end($chunks);
255 $hint = sprintf('<urgent>%s</urgent> ', $guess);
256 // @codeCoverageIgnoreEnd
259 if ($param->isOptional()) {
260 if (!$param->isDefaultValueAvailable()) {
262 $typeStyle = 'urgent';
264 $value = $param->getDefaultValue();
265 $typeStyle = self::getTypeStyle($value);
266 $value = is_array($value) ? 'array()' : is_null($value) ? 'null' : var_export($value, true);
268 $default = sprintf(' = <%s>%s</%s>', $typeStyle, OutputFormatter::escape($value), $typeStyle);
274 '%s%s<strong>$%s</strong>%s',
275 $param->isPassedByReference() ? '&' : '',