Skip to content

Commit 661a36b

Browse files
committed
fix an error for parse method comments
1 parent 115a071 commit 661a36b

File tree

2 files changed

+19
-81
lines changed

2 files changed

+19
-81
lines changed

src/AbstractHandler.php

+15-79
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,21 @@
2323
use Inhere\Console\Util\FormatUtil;
2424
use Inhere\Console\Util\Helper;
2525
use InvalidArgumentException;
26-
use ReflectionClass;
27-
use ReflectionMethod;
26+
use ReflectionException;
2827
use RuntimeException;
2928
use Swoole\Coroutine;
3029
use Swoole\Event;
3130
use Throwable;
3231
use Toolkit\PFlag\FlagsParser;
3332
use Toolkit\PFlag\SFlags;
33+
use Toolkit\Stdlib\Helper\PhpHelper;
3434
use Toolkit\Stdlib\Obj\ConfigObject;
3535
use Toolkit\Stdlib\Util\PhpDoc;
36-
use function array_keys;
3736
use function array_merge;
3837
use function cli_set_process_title;
3938
use function error_get_last;
4039
use function function_exists;
4140
use function implode;
42-
use function is_array;
4341
use function is_string;
4442
use function preg_replace;
4543
use function sprintf;
@@ -151,7 +149,7 @@ public static function aliases(): array
151149
/**
152150
* Command constructor.
153151
*
154-
* @param Input $input
152+
* @param Input $input
155153
* @param Output $output
156154
*/
157155
// TODO public function __construct(Input $input = null, Output $output = null, InputDefinition $definition = null)
@@ -411,7 +409,7 @@ protected function beforeExecute(): bool
411409
/**
412410
* Do execute command
413411
*
414-
* @param Input $input
412+
* @param Input $input
415413
* @param Output $output
416414
*
417415
* @return int|mixed
@@ -498,12 +496,12 @@ public function initParams(array $params): ConfigObject
498496
* @param string $method
499497
* @param FlagsParser $fs
500498
*
501-
* @throws \ReflectionException
499+
* @throws ReflectionException
502500
*/
503501
public function loadRulesByDocblock(string $method, FlagsParser $fs): void
504502
{
505503
$this->debugf('not config flags, load flag rules by docblock, method: %s', $method);
506-
$rftMth = new ReflectionMethod($this, $method);
504+
$rftMth = PhpHelper::reflectMethod($this, $method);
507505

508506
// parse doc for get flag rules
509507
$dr = DocblockRules::newByDocblock($rftMth->getDocComment());
@@ -524,95 +522,33 @@ public function loadRulesByDocblock(string $method, FlagsParser $fs): void
524522
*/
525523
abstract protected function showHelp(): bool;
526524

527-
/**
528-
* @param InputDefinition $definition
529-
* @param array $aliases
530-
*/
531-
protected function showHelpByDefinition(InputDefinition $definition, array $aliases = []): void
532-
{
533-
$this->log(Console::VERB_DEBUG, 'display help information by input definition');
534-
535-
// if has InputDefinition object. (The comment of the command will not be parsed and used at this time.)
536-
$help = $definition->getSynopsis();
537-
// parse example
538-
$example = $help['example:'];
539-
if (!empty($example)) {
540-
if (is_string($example)) {
541-
$help['example:'] = $this->parseCommentsVars($example);
542-
} elseif (is_array($example)) {
543-
foreach ($example as &$item) {
544-
$item = $this->parseCommentsVars($item);
545-
}
546-
unset($item);
547-
} else {
548-
$help['example:'] = '';
549-
}
550-
}
551-
552-
$binName = $this->getScriptName();
553-
554-
// build usage
555-
if ($this->isAttached()) {
556-
$help['usage:'] = sprintf('%s %s %s', $binName, $this->getCommandName(), $help['usage:']);
557-
} else {
558-
$help['usage:'] = $binName . ' ' . $help['usage:'];
559-
}
560-
561-
// align global options
562-
$help['global options:'] = FormatUtil::alignOptions(GlobalOption::getOptions());
563-
564-
$isAlone = $this->isAlone();
565-
if ($isAlone && empty($help[0])) {
566-
$help[0] = self::getDescription();
567-
}
568-
569-
if (empty($help[0])) {
570-
$help[0] = 'No description message for the command';
571-
}
572-
573-
// output description
574-
$this->write(ucfirst($help[0]) . PHP_EOL);
575-
576-
if ($aliases) {
577-
$this->output->writef('<comment>Alias:</comment> %s', implode(',', $aliases));
578-
}
579-
580-
unset($help[0]);
581-
$this->output->mList($help, ['sepChar' => ' ']);
582-
}
583-
584525
/**
585526
* Display command/action help by parse method annotations
586527
*
587528
* @param string $method
588529
* @param string $action action of an group
589-
* @param array $aliases
530+
* @param array $aliases
590531
*
591532
* @return int
533+
* @throws ReflectionException
592534
*/
593535
protected function showHelpByAnnotations(string $method, string $action = '', array $aliases = []): int
594536
{
595-
$ref = new ReflectionClass($this);
596537
$name = $this->input->getCommand();
597538

598-
if (!$ref->hasMethod($method)) {
599-
$subCmd = $this->input->getSubCommand();
600-
$this->write("The command '<info>$subCmd</info>' dont exist in the group: " . static::getName());
601-
return 0;
602-
}
603-
604539
// subcommand: is a console controller subcommand
605-
if ($action && !$ref->getMethod($method)->isPublic()) {
540+
$rftMth = PhpHelper::reflectMethod($this, $method);
541+
if ($action && !$rftMth->isPublic()) {
606542
$this->write("The command [<info>$name</info>] don't allow access in the class.");
607543
return 0;
608544
}
609545

610-
$allowedTags = array_keys(self::$annotationTags);
546+
$allowedTags = DocblockRules::getAllowedTags();
611547
$this->logf(Console::VERB_DEBUG, "render help for the command: %s", $this->input->getCommandId());
612548

613549
$help = [];
614-
$doc = $ref->getMethod($method)->getDocComment();
615-
$tags = PhpDoc::getTags($this->parseCommentsVars((string)$doc), [
550+
$doc = $this->parseCommentsVars((string)$rftMth->getDocComment());
551+
$tags = PhpDoc::getTags($doc, [
616552
'allow' => $allowedTags,
617553
]);
618554

@@ -631,7 +567,7 @@ protected function showHelpByAnnotations(string $method, string $action = '', ar
631567
}
632568

633569
// is an command object
634-
$isCommand = $ref->isSubclassOf(CommandInterface::class);
570+
$isCommand = $this->isCommand();
635571
foreach ($allowedTags as $tag) {
636572
if (empty($tags[$tag]) || !is_string($tags[$tag])) {
637573
// for alone command
@@ -790,7 +726,7 @@ public static function addAnnotationTag(string $name): void
790726

791727
/**
792728
* @param array $annotationTags
793-
* @param bool $replace
729+
* @param bool $replace
794730
*/
795731
public static function setAnnotationTags(array $annotationTags, $replace = false): void
796732
{

src/Annotate/DocblockRules.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ public function getTagValue(string $tag): string
191191
}
192192

193193
/**
194+
* @param bool $onlyName
195+
*
194196
* @return array
195197
*/
196-
public static function getAllowedTags(): array
198+
public static function getAllowedTags(bool $onlyName = true): array
197199
{
198-
return array_keys(self::$allowedTags);
200+
return $onlyName ? array_keys(self::$allowedTags) : self::$allowedTags;
199201
}
200202

201203
/**

0 commit comments

Comments
 (0)