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\ReflectionClassConstant;
15 use Psy\Reflection\ReflectionConstant_;
16 use Psy\Reflection\ReflectionLanguageConstruct;
18 use Symfony\Component\Console\Formatter\OutputFormatter;
21 * An abstract representation of a function, class or property signature.
23 class SignatureFormatter implements Formatter
26 * Format a signature for the given reflector.
28 * Defers to subclasses to do the actual formatting.
30 * @param \Reflector $reflector
32 * @return string Formatted signature
34 public static function format(\Reflector $reflector)
37 case $reflector instanceof \ReflectionFunction:
38 case $reflector instanceof ReflectionLanguageConstruct:
39 return self::formatFunction($reflector);
41 // this case also covers \ReflectionObject:
42 case $reflector instanceof \ReflectionClass:
43 return self::formatClass($reflector);
45 case $reflector instanceof ReflectionClassConstant:
46 case $reflector instanceof \ReflectionClassConstant:
47 return self::formatClassConstant($reflector);
49 case $reflector instanceof \ReflectionMethod:
50 return self::formatMethod($reflector);
52 case $reflector instanceof \ReflectionProperty:
53 return self::formatProperty($reflector);
55 case $reflector instanceof ReflectionConstant_:
56 return self::formatConstant($reflector);
59 throw new \InvalidArgumentException('Unexpected Reflector class: ' . \get_class($reflector));
64 * Print the signature name.
66 * @param \Reflector $reflector
68 * @return string Formatted name
70 public static function formatName(\Reflector $reflector)
72 return $reflector->getName();
76 * Print the method, property or class modifiers.
78 * @param \Reflector $reflector
80 * @return string Formatted modifiers
82 private static function formatModifiers(\Reflector $reflector)
84 if ($reflector instanceof \ReflectionClass && $reflector->isTrait()) {
85 // For some reason, PHP 5.x returns `abstract public` modifiers for
86 // traits. Let's just ignore that business entirely.
87 if (\version_compare(PHP_VERSION, '7.0.0', '<')) {
92 return \implode(' ', \array_map(function ($modifier) {
93 return \sprintf('<keyword>%s</keyword>', $modifier);
94 }, \Reflection::getModifierNames($reflector->getModifiers())));
98 * Format a class signature.
100 * @param \ReflectionClass $reflector
102 * @return string Formatted signature
104 private static function formatClass(\ReflectionClass $reflector)
108 if ($modifiers = self::formatModifiers($reflector)) {
109 $chunks[] = $modifiers;
112 if ($reflector->isTrait()) {
115 $chunks[] = $reflector->isInterface() ? 'interface' : 'class';
118 $chunks[] = \sprintf('<class>%s</class>', self::formatName($reflector));
120 if ($parent = $reflector->getParentClass()) {
121 $chunks[] = 'extends';
122 $chunks[] = \sprintf('<class>%s</class>', $parent->getName());
125 $interfaces = $reflector->getInterfaceNames();
126 if (!empty($interfaces)) {
129 $chunks[] = 'implements';
130 $chunks[] = \implode(', ', \array_map(function ($name) {
131 return \sprintf('<class>%s</class>', $name);
135 return \implode(' ', $chunks);
139 * Format a constant signature.
141 * @param ReflectionClassConstant|\ReflectionClassConstant $reflector
143 * @return string Formatted signature
145 private static function formatClassConstant($reflector)
147 $value = $reflector->getValue();
148 $style = self::getTypeStyle($value);
151 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>',
152 self::formatName($reflector),
154 OutputFormatter::escape(Json::encode($value)),
160 * Format a constant signature.
162 * @param ReflectionConstant_ $reflector
164 * @return string Formatted signature
166 private static function formatConstant($reflector)
168 $value = $reflector->getValue();
169 $style = self::getTypeStyle($value);
172 '<keyword>define</keyword>(<string>%s</string>, <%s>%s</%s>)',
173 OutputFormatter::escape(Json::encode($reflector->getName())),
175 OutputFormatter::escape(Json::encode($value)),
181 * Helper for getting output style for a given value's type.
183 * @param mixed $value
187 private static function getTypeStyle($value)
189 if (\is_int($value) || \is_float($value)) {
191 } elseif (\is_string($value)) {
193 } elseif (\is_bool($value) || \is_null($value)) {
196 return 'strong'; // @codeCoverageIgnore
201 * Format a property signature.
203 * @param \ReflectionProperty $reflector
205 * @return string Formatted signature
207 private static function formatProperty(\ReflectionProperty $reflector)
210 '%s <strong>$%s</strong>',
211 self::formatModifiers($reflector),
212 $reflector->getName()
217 * Format a function signature.
219 * @param \ReflectionFunction $reflector
221 * @return string Formatted signature
223 private static function formatFunction(\ReflectionFunctionAbstract $reflector)
226 '<keyword>function</keyword> %s<function>%s</function>(%s)',
227 $reflector->returnsReference() ? '&' : '',
228 self::formatName($reflector),
229 \implode(', ', self::formatFunctionParams($reflector))
234 * Format a method signature.
236 * @param \ReflectionMethod $reflector
238 * @return string Formatted signature
240 private static function formatMethod(\ReflectionMethod $reflector)
244 self::formatModifiers($reflector),
245 self::formatFunction($reflector)
250 * Print the function params.
252 * @param \ReflectionFunctionAbstract $reflector
256 private static function formatFunctionParams(\ReflectionFunctionAbstract $reflector)
259 foreach ($reflector->getParameters() as $param) {
262 if ($param->isArray()) {
263 $hint = '<keyword>array</keyword> ';
264 } elseif ($class = $param->getClass()) {
265 $hint = \sprintf('<class>%s</class> ', $class->getName());
267 } catch (\Exception $e) {
268 // sometimes we just don't know...
269 // bad class names, or autoloaded classes that haven't been loaded yet, or whathaveyou.
270 // come to think of it, the only time I've seen this is with the intl extension.
272 // Hax: we'll try to extract it :P
274 // @codeCoverageIgnoreStart
275 $chunks = \explode('$' . $param->getName(), (string) $param);
276 $chunks = \explode(' ', \trim($chunks[0]));
277 $guess = \end($chunks);
279 $hint = \sprintf('<urgent>%s</urgent> ', $guess);
280 // @codeCoverageIgnoreEnd
283 if ($param->isOptional()) {
284 if (!$param->isDefaultValueAvailable()) {
286 $typeStyle = 'urgent';
288 $value = $param->getDefaultValue();
289 $typeStyle = self::getTypeStyle($value);
290 $value = \is_array($value) ? 'array()' : \is_null($value) ? 'null' : \var_export($value, true);
292 $default = \sprintf(' = <%s>%s</%s>', $typeStyle, OutputFormatter::escape($value), $typeStyle);
297 $params[] = \sprintf(
298 '%s%s<strong>$%s</strong>%s',
299 $param->isPassedByReference() ? '&' : '',