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\Command;
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;
22 * Read the documentation for an object, class, constant, method or property.
24 class DocCommand extends ReflectingCommand
29 protected function configure()
33 ->setAliases(['rtfm', 'man'])
35 new CodeArgument('target', CodeArgument::REQUIRED, 'Function, class, instance, constant, method or property to document.'),
37 ->setDescription('Read the documentation for an object, class, constant, method or property.')
40 Read the documentation for an object, class, constant, method or property.
42 It's awesome for well-documented code, not quite as awesome for poorly documented code.
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>
57 protected function execute(InputInterface $input, OutputInterface $output)
59 $value = $input->getArgument('target');
60 if (ReflectionLanguageConstruct::isLanguageConstruct($value)) {
61 $reflector = new ReflectionLanguageConstruct($value);
62 $doc = $this->getManualDocById($value);
64 list($target, $reflector) = $this->getTargetAndReflector($value);
65 $doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
68 $db = $this->getApplication()->getManualDb();
70 $output->page(function ($output) use ($reflector, $doc, $db) {
71 $output->writeln(SignatureFormatter::format($reflector));
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');
79 $output->writeln($doc);
83 // Set some magic local variables
84 $this->setCommandScopeVariables($reflector);
87 private function getManualDoc($reflector)
89 switch (\get_class($reflector)) {
90 case 'ReflectionClass':
91 case 'ReflectionObject':
92 case 'ReflectionFunction':
93 $id = $reflector->name;
96 case 'ReflectionMethod':
97 $id = $reflector->class . '::' . $reflector->name;
100 case 'ReflectionProperty':
101 $id = $reflector->class . '::$' . $reflector->name;
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
109 $id = $reflector->class . '::' . $reflector->name;
112 case 'Psy\Reflection\ReflectionConstant_':
113 $id = $reflector->name;
120 return $this->getManualDocById($id);
123 private function getManualDocById($id)
125 if ($db = $this->getApplication()->getManualDb()) {
127 ->query(\sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)))