comparison vendor/psy/psysh/src/Formatter/SignatureFormatter.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
54 54
55 case $reflector instanceof ReflectionConstant_: 55 case $reflector instanceof ReflectionConstant_:
56 return self::formatConstant($reflector); 56 return self::formatConstant($reflector);
57 57
58 default: 58 default:
59 throw new \InvalidArgumentException('Unexpected Reflector class: ' . get_class($reflector)); 59 throw new \InvalidArgumentException('Unexpected Reflector class: ' . \get_class($reflector));
60 } 60 }
61 } 61 }
62 62
63 /** 63 /**
64 * Print the signature name. 64 * Print the signature name.
82 private static function formatModifiers(\Reflector $reflector) 82 private static function formatModifiers(\Reflector $reflector)
83 { 83 {
84 if ($reflector instanceof \ReflectionClass && $reflector->isTrait()) { 84 if ($reflector instanceof \ReflectionClass && $reflector->isTrait()) {
85 // For some reason, PHP 5.x returns `abstract public` modifiers for 85 // For some reason, PHP 5.x returns `abstract public` modifiers for
86 // traits. Let's just ignore that business entirely. 86 // traits. Let's just ignore that business entirely.
87 if (version_compare(PHP_VERSION, '7.0.0', '<')) { 87 if (\version_compare(PHP_VERSION, '7.0.0', '<')) {
88 return []; 88 return [];
89 } 89 }
90 } 90 }
91 91
92 return implode(' ', array_map(function ($modifier) { 92 return \implode(' ', \array_map(function ($modifier) {
93 return sprintf('<keyword>%s</keyword>', $modifier); 93 return \sprintf('<keyword>%s</keyword>', $modifier);
94 }, \Reflection::getModifierNames($reflector->getModifiers()))); 94 }, \Reflection::getModifierNames($reflector->getModifiers())));
95 } 95 }
96 96
97 /** 97 /**
98 * Format a class signature. 98 * Format a class signature.
113 $chunks[] = 'trait'; 113 $chunks[] = 'trait';
114 } else { 114 } else {
115 $chunks[] = $reflector->isInterface() ? 'interface' : 'class'; 115 $chunks[] = $reflector->isInterface() ? 'interface' : 'class';
116 } 116 }
117 117
118 $chunks[] = sprintf('<class>%s</class>', self::formatName($reflector)); 118 $chunks[] = \sprintf('<class>%s</class>', self::formatName($reflector));
119 119
120 if ($parent = $reflector->getParentClass()) { 120 if ($parent = $reflector->getParentClass()) {
121 $chunks[] = 'extends'; 121 $chunks[] = 'extends';
122 $chunks[] = sprintf('<class>%s</class>', $parent->getName()); 122 $chunks[] = \sprintf('<class>%s</class>', $parent->getName());
123 } 123 }
124 124
125 $interfaces = $reflector->getInterfaceNames(); 125 $interfaces = $reflector->getInterfaceNames();
126 if (!empty($interfaces)) { 126 if (!empty($interfaces)) {
127 sort($interfaces); 127 \sort($interfaces);
128 128
129 $chunks[] = 'implements'; 129 $chunks[] = 'implements';
130 $chunks[] = implode(', ', array_map(function ($name) { 130 $chunks[] = \implode(', ', \array_map(function ($name) {
131 return sprintf('<class>%s</class>', $name); 131 return \sprintf('<class>%s</class>', $name);
132 }, $interfaces)); 132 }, $interfaces));
133 } 133 }
134 134
135 return implode(' ', $chunks); 135 return \implode(' ', $chunks);
136 } 136 }
137 137
138 /** 138 /**
139 * Format a constant signature. 139 * Format a constant signature.
140 * 140 *
145 private static function formatClassConstant($reflector) 145 private static function formatClassConstant($reflector)
146 { 146 {
147 $value = $reflector->getValue(); 147 $value = $reflector->getValue();
148 $style = self::getTypeStyle($value); 148 $style = self::getTypeStyle($value);
149 149
150 return sprintf( 150 return \sprintf(
151 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>', 151 '<keyword>const</keyword> <const>%s</const> = <%s>%s</%s>',
152 self::formatName($reflector), 152 self::formatName($reflector),
153 $style, 153 $style,
154 OutputFormatter::escape(Json::encode($value)), 154 OutputFormatter::escape(Json::encode($value)),
155 $style 155 $style
166 private static function formatConstant($reflector) 166 private static function formatConstant($reflector)
167 { 167 {
168 $value = $reflector->getValue(); 168 $value = $reflector->getValue();
169 $style = self::getTypeStyle($value); 169 $style = self::getTypeStyle($value);
170 170
171 return sprintf( 171 return \sprintf(
172 '<keyword>define</keyword>(<string>%s</string>, <%s>%s</%s>)', 172 '<keyword>define</keyword>(<string>%s</string>, <%s>%s</%s>)',
173 OutputFormatter::escape(Json::encode($reflector->getName())), 173 OutputFormatter::escape(Json::encode($reflector->getName())),
174 $style, 174 $style,
175 OutputFormatter::escape(Json::encode($value)), 175 OutputFormatter::escape(Json::encode($value)),
176 $style 176 $style
184 * 184 *
185 * @return string 185 * @return string
186 */ 186 */
187 private static function getTypeStyle($value) 187 private static function getTypeStyle($value)
188 { 188 {
189 if (is_int($value) || is_float($value)) { 189 if (\is_int($value) || \is_float($value)) {
190 return 'number'; 190 return 'number';
191 } elseif (is_string($value)) { 191 } elseif (\is_string($value)) {
192 return 'string'; 192 return 'string';
193 } elseif (is_bool($value) || is_null($value)) { 193 } elseif (\is_bool($value) || \is_null($value)) {
194 return 'bool'; 194 return 'bool';
195 } else { 195 } else {
196 return 'strong'; // @codeCoverageIgnore 196 return 'strong'; // @codeCoverageIgnore
197 } 197 }
198 } 198 }
204 * 204 *
205 * @return string Formatted signature 205 * @return string Formatted signature
206 */ 206 */
207 private static function formatProperty(\ReflectionProperty $reflector) 207 private static function formatProperty(\ReflectionProperty $reflector)
208 { 208 {
209 return sprintf( 209 return \sprintf(
210 '%s <strong>$%s</strong>', 210 '%s <strong>$%s</strong>',
211 self::formatModifiers($reflector), 211 self::formatModifiers($reflector),
212 $reflector->getName() 212 $reflector->getName()
213 ); 213 );
214 } 214 }
220 * 220 *
221 * @return string Formatted signature 221 * @return string Formatted signature
222 */ 222 */
223 private static function formatFunction(\ReflectionFunctionAbstract $reflector) 223 private static function formatFunction(\ReflectionFunctionAbstract $reflector)
224 { 224 {
225 return sprintf( 225 return \sprintf(
226 '<keyword>function</keyword> %s<function>%s</function>(%s)', 226 '<keyword>function</keyword> %s<function>%s</function>(%s)',
227 $reflector->returnsReference() ? '&' : '', 227 $reflector->returnsReference() ? '&' : '',
228 self::formatName($reflector), 228 self::formatName($reflector),
229 implode(', ', self::formatFunctionParams($reflector)) 229 \implode(', ', self::formatFunctionParams($reflector))
230 ); 230 );
231 } 231 }
232 232
233 /** 233 /**
234 * Format a method signature. 234 * Format a method signature.
237 * 237 *
238 * @return string Formatted signature 238 * @return string Formatted signature
239 */ 239 */
240 private static function formatMethod(\ReflectionMethod $reflector) 240 private static function formatMethod(\ReflectionMethod $reflector)
241 { 241 {
242 return sprintf( 242 return \sprintf(
243 '%s %s', 243 '%s %s',
244 self::formatModifiers($reflector), 244 self::formatModifiers($reflector),
245 self::formatFunction($reflector) 245 self::formatFunction($reflector)
246 ); 246 );
247 } 247 }
260 $hint = ''; 260 $hint = '';
261 try { 261 try {
262 if ($param->isArray()) { 262 if ($param->isArray()) {
263 $hint = '<keyword>array</keyword> '; 263 $hint = '<keyword>array</keyword> ';
264 } elseif ($class = $param->getClass()) { 264 } elseif ($class = $param->getClass()) {
265 $hint = sprintf('<class>%s</class> ', $class->getName()); 265 $hint = \sprintf('<class>%s</class> ', $class->getName());
266 } 266 }
267 } catch (\Exception $e) { 267 } catch (\Exception $e) {
268 // sometimes we just don't know... 268 // sometimes we just don't know...
269 // 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.
270 // 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.
271 271
272 // Hax: we'll try to extract it :P 272 // Hax: we'll try to extract it :P
273 273
274 // @codeCoverageIgnoreStart 274 // @codeCoverageIgnoreStart
275 $chunks = explode('$' . $param->getName(), (string) $param); 275 $chunks = \explode('$' . $param->getName(), (string) $param);
276 $chunks = explode(' ', trim($chunks[0])); 276 $chunks = \explode(' ', \trim($chunks[0]));
277 $guess = end($chunks); 277 $guess = \end($chunks);
278 278
279 $hint = sprintf('<urgent>%s</urgent> ', $guess); 279 $hint = \sprintf('<urgent>%s</urgent> ', $guess);
280 // @codeCoverageIgnoreEnd 280 // @codeCoverageIgnoreEnd
281 } 281 }
282 282
283 if ($param->isOptional()) { 283 if ($param->isOptional()) {
284 if (!$param->isDefaultValueAvailable()) { 284 if (!$param->isDefaultValueAvailable()) {
285 $value = 'unknown'; 285 $value = 'unknown';
286 $typeStyle = 'urgent'; 286 $typeStyle = 'urgent';
287 } else { 287 } else {
288 $value = $param->getDefaultValue(); 288 $value = $param->getDefaultValue();
289 $typeStyle = self::getTypeStyle($value); 289 $typeStyle = self::getTypeStyle($value);
290 $value = is_array($value) ? 'array()' : is_null($value) ? 'null' : var_export($value, true); 290 $value = \is_array($value) ? 'array()' : \is_null($value) ? 'null' : \var_export($value, true);
291 } 291 }
292 $default = sprintf(' = <%s>%s</%s>', $typeStyle, OutputFormatter::escape($value), $typeStyle); 292 $default = \sprintf(' = <%s>%s</%s>', $typeStyle, OutputFormatter::escape($value), $typeStyle);
293 } else { 293 } else {
294 $default = ''; 294 $default = '';
295 } 295 }
296 296
297 $params[] = sprintf( 297 $params[] = \sprintf(
298 '%s%s<strong>$%s</strong>%s', 298 '%s%s<strong>$%s</strong>%s',
299 $param->isPassedByReference() ? '&' : '', 299 $param->isPassedByReference() ? '&' : '',
300 $hint, 300 $hint,
301 $param->getName(), 301 $param->getName(),
302 $default 302 $default