comparison vendor/psy/psysh/src/Formatter/SignatureFormatter.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 5fb285c0d0e3
children 129ea1e6d783
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Psy\Formatter; 12 namespace Psy\Formatter;
13 13
14 use Psy\Reflection\ReflectionConstant; 14 use Psy\Reflection\ReflectionClassConstant;
15 use Psy\Reflection\ReflectionConstant_;
15 use Psy\Reflection\ReflectionLanguageConstruct; 16 use Psy\Reflection\ReflectionLanguageConstruct;
16 use Psy\Util\Json; 17 use Psy\Util\Json;
17 use Symfony\Component\Console\Formatter\OutputFormatter; 18 use Symfony\Component\Console\Formatter\OutputFormatter;
18 19
19 /** 20 /**
39 40
40 // this case also covers \ReflectionObject: 41 // this case also covers \ReflectionObject:
41 case $reflector instanceof \ReflectionClass: 42 case $reflector instanceof \ReflectionClass:
42 return self::formatClass($reflector); 43 return self::formatClass($reflector);
43 44
44 case $reflector instanceof ReflectionConstant: 45 case $reflector instanceof ReflectionClassConstant:
45 return self::formatConstant($reflector); 46 case $reflector instanceof \ReflectionClassConstant:
47 return self::formatClassConstant($reflector);
46 48
47 case $reflector instanceof \ReflectionMethod: 49 case $reflector instanceof \ReflectionMethod:
48 return self::formatMethod($reflector); 50 return self::formatMethod($reflector);
49 51
50 case $reflector instanceof \ReflectionProperty: 52 case $reflector instanceof \ReflectionProperty:
51 return self::formatProperty($reflector); 53 return self::formatProperty($reflector);
52 54
55 case $reflector instanceof ReflectionConstant_:
56 return self::formatConstant($reflector);
57
53 default: 58 default:
54 throw new \InvalidArgumentException('Unexpected Reflector class: ' . get_class($reflector)); 59 throw new \InvalidArgumentException('Unexpected Reflector class: ' . get_class($reflector));
55 } 60 }
56 } 61 }
57 62
68 } 73 }
69 74
70 /** 75 /**
71 * Print the method, property or class modifiers. 76 * Print the method, property or class modifiers.
72 * 77 *
73 * Technically this should be a trait. Can't wait for 5.4 :)
74 *
75 * @param \Reflector $reflector 78 * @param \Reflector $reflector
76 * 79 *
77 * @return string Formatted modifiers 80 * @return string Formatted modifiers
78 */ 81 */
79 private static function formatModifiers(\Reflector $reflector) 82 private static function formatModifiers(\Reflector $reflector)
80 { 83 {
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', '<')) {
88 return [];
89 }
90 }
91
81 return implode(' ', array_map(function ($modifier) { 92 return implode(' ', array_map(function ($modifier) {
82 return sprintf('<keyword>%s</keyword>', $modifier); 93 return sprintf('<keyword>%s</keyword>', $modifier);
83 }, \Reflection::getModifierNames($reflector->getModifiers()))); 94 }, \Reflection::getModifierNames($reflector->getModifiers())));
84 } 95 }
85 96
125 } 136 }
126 137
127 /** 138 /**
128 * Format a constant signature. 139 * Format a constant signature.
129 * 140 *
130 * @param ReflectionConstant $reflector 141 * @param ReflectionClassConstant|\ReflectionClassConstant $reflector
131 * 142 *
132 * @return string Formatted signature 143 * @return string Formatted signature
133 */ 144 */
134 private static function formatConstant(ReflectionConstant $reflector) 145 private static function formatClassConstant($reflector)
135 { 146 {
136 $value = $reflector->getValue(); 147 $value = $reflector->getValue();
137 $style = self::getTypeStyle($value); 148 $style = self::getTypeStyle($value);
138 149
139 return sprintf( 150 return sprintf(
140 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>', 151 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>',
141 self::formatName($reflector), 152 self::formatName($reflector),
153 $style,
154 OutputFormatter::escape(Json::encode($value)),
155 $style
156 );
157 }
158
159 /**
160 * Format a constant signature.
161 *
162 * @param ReflectionConstant_ $reflector
163 *
164 * @return string Formatted signature
165 */
166 private static function formatConstant($reflector)
167 {
168 $value = $reflector->getValue();
169 $style = self::getTypeStyle($value);
170
171 return sprintf(
172 '<keyword>define</keyword>(<string>%s</string>, <%s>%s</%s>)',
173 OutputFormatter::escape(Json::encode($reflector->getName())),
142 $style, 174 $style,
143 OutputFormatter::escape(Json::encode($value)), 175 OutputFormatter::escape(Json::encode($value)),
144 $style 176 $style
145 ); 177 );
146 } 178 }
159 } elseif (is_string($value)) { 191 } elseif (is_string($value)) {
160 return 'string'; 192 return 'string';
161 } elseif (is_bool($value) || is_null($value)) { 193 } elseif (is_bool($value) || is_null($value)) {
162 return 'bool'; 194 return 'bool';
163 } else { 195 } else {
164 return 'strong'; 196 return 'strong'; // @codeCoverageIgnore
165 } 197 }
166 } 198 }
167 199
168 /** 200 /**
169 * Format a property signature. 201 * Format a property signature.
236 // sometimes we just don't know... 268 // sometimes we just don't know...
237 // bad class names, or autoloaded classes that haven't been loaded yet, or whathaveyou. 269 // 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. 270 // come to think of it, the only time I've seen this is with the intl extension.
239 271
240 // Hax: we'll try to extract it :P 272 // Hax: we'll try to extract it :P
273
274 // @codeCoverageIgnoreStart
241 $chunks = explode('$' . $param->getName(), (string) $param); 275 $chunks = explode('$' . $param->getName(), (string) $param);
242 $chunks = explode(' ', trim($chunks[0])); 276 $chunks = explode(' ', trim($chunks[0]));
243 $guess = end($chunks); 277 $guess = end($chunks);
244 278
245 $hint = sprintf('<urgent>%s</urgent> ', $guess); 279 $hint = sprintf('<urgent>%s</urgent> ', $guess);
280 // @codeCoverageIgnoreEnd
246 } 281 }
247 282
248 if ($param->isOptional()) { 283 if ($param->isOptional()) {
249 if (!$param->isDefaultValueAvailable()) { 284 if (!$param->isDefaultValueAvailable()) {
250 $value = 'unknown'; 285 $value = 'unknown';