comparison vendor/psy/psysh/src/Command/DocCommand.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children c2387f117808
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php
2
3 /*
4 * This file is part of Psy Shell.
5 *
6 * (c) 2012-2018 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Psy\Command;
13
14 use Psy\Formatter\DocblockFormatter;
15 use Psy\Formatter\SignatureFormatter;
16 use Psy\Input\CodeArgument;
17 use Psy\Reflection\ReflectionLanguageConstruct;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20
21 /**
22 * Read the documentation for an object, class, constant, method or property.
23 */
24 class DocCommand extends ReflectingCommand
25 {
26 /**
27 * {@inheritdoc}
28 */
29 protected function configure()
30 {
31 $this
32 ->setName('doc')
33 ->setAliases(['rtfm', 'man'])
34 ->setDefinition([
35 new CodeArgument('target', CodeArgument::REQUIRED, 'Function, class, instance, constant, method or property to document.'),
36 ])
37 ->setDescription('Read the documentation for an object, class, constant, method or property.')
38 ->setHelp(
39 <<<HELP
40 Read the documentation for an object, class, constant, method or property.
41
42 It's awesome for well-documented code, not quite as awesome for poorly documented code.
43
44 e.g.
45 <return>>>> doc preg_replace</return>
46 <return>>>> doc Psy\Shell</return>
47 <return>>>> doc Psy\Shell::debug</return>
48 <return>>>> \$s = new Psy\Shell</return>
49 <return>>>> doc \$s->run</return>
50 HELP
51 );
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 protected function execute(InputInterface $input, OutputInterface $output)
58 {
59 $value = $input->getArgument('target');
60 if (ReflectionLanguageConstruct::isLanguageConstruct($value)) {
61 $reflector = new ReflectionLanguageConstruct($value);
62 $doc = $this->getManualDocById($value);
63 } else {
64 list($target, $reflector) = $this->getTargetAndReflector($value);
65 $doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
66 }
67
68 $db = $this->getApplication()->getManualDb();
69
70 $output->page(function ($output) use ($reflector, $doc, $db) {
71 $output->writeln(SignatureFormatter::format($reflector));
72 $output->writeln('');
73
74 if (empty($doc) && !$db) {
75 $output->writeln('<warning>PHP manual not found</warning>');
76 $output->writeln(' To document core PHP functionality, download the PHP reference manual:');
77 $output->writeln(' https://github.com/bobthecow/psysh/wiki/PHP-manual');
78 } else {
79 $output->writeln($doc);
80 }
81 });
82
83 // Set some magic local variables
84 $this->setCommandScopeVariables($reflector);
85 }
86
87 private function getManualDoc($reflector)
88 {
89 switch (get_class($reflector)) {
90 case 'ReflectionClass':
91 case 'ReflectionObject':
92 case 'ReflectionFunction':
93 $id = $reflector->name;
94 break;
95
96 case 'ReflectionMethod':
97 $id = $reflector->class . '::' . $reflector->name;
98 break;
99
100 case 'ReflectionProperty':
101 $id = $reflector->class . '::$' . $reflector->name;
102 break;
103
104 default:
105 return false;
106 }
107
108 return $this->getManualDocById($id);
109 }
110
111 private function getManualDocById($id)
112 {
113 if ($db = $this->getApplication()->getManualDb()) {
114 return $db
115 ->query(sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))
116 ->fetchColumn(0);
117 }
118 }
119 }