WP_CLI\Dispatcher

CommandFactory::get_doc_comment()private staticWP-CLI 1.0

Gets the document comment. Caters for PHP directive opcache.save comments being disabled.

Метод класса: CommandFactory{}

Хуков нет.

Возвращает

Строку|false|null. Doc comment string if any, false if none (same as Reflection*::getDocComment()), null if error.

Использование

$result = CommandFactory::get_doc_comment( $reflection );
$reflection(ReflectionMethod|ReflectionClass|ReflectionFunction) (обязательный)
Reflection instance.

Код CommandFactory::get_doc_comment() WP-CLI 2.8.0-alpha

private static function get_doc_comment( $reflection ) {
	$contents    = null;
	$doc_comment = $reflection->getDocComment();

	if ( false !== $doc_comment || ! ( ini_get( 'opcache.enable_cli' ) && ! ini_get( 'opcache.save_comments' ) ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.NewIniDirectives
		// Either have doc comment, or no doc comment and save comments enabled - standard situation.
		if ( ! getenv( 'WP_CLI_TEST_GET_DOC_COMMENT' ) ) {
			return $doc_comment;
		}
	}

	$filename = $reflection->getFileName();

	if ( isset( self::$file_contents[ $filename ] ) ) {
		$contents = self::$file_contents[ $filename ];
	} elseif ( is_readable( $filename ) ) {
		$contents = file_get_contents( $filename );
		if ( is_string( $contents ) && '' !== $contents ) {
			$contents                         = explode( "\n", $contents );
			self::$file_contents[ $filename ] = $contents;
		}
	}

	if ( ! empty( $contents ) ) {
		return self::extract_last_doc_comment( implode( "\n", array_slice( $contents, 0, $reflection->getStartLine() ) ) );
	}

	WP_CLI::debug( "Could not read contents for filename '{$filename}'.", 'commandfactory' );
	return null;
}