Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / psy / psysh / src / Command / DocCommand.php
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             case 'ReflectionClassConstant':
105             case 'Psy\Reflection\ReflectionClassConstant':
106                 // @todo this is going to collide with ReflectionMethod ids
107                 // someday... start running the query by id + type if the DB
108                 // supports it.
109                 $id = $reflector->class . '::' . $reflector->name;
110                 break;
111
112             case 'Psy\Reflection\ReflectionConstant_':
113                 $id = $reflector->name;
114                 break;
115
116             default:
117                 return false;
118         }
119
120         return $this->getManualDocById($id);
121     }
122
123     private function getManualDocById($id)
124     {
125         if ($db = $this->getApplication()->getManualDb()) {
126             return $db
127                 ->query(\sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))
128                 ->fetchColumn(0);
129         }
130     }
131 }