forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotation.php
More file actions
85 lines (73 loc) · 2.09 KB
/
Copy pathAnnotation.php
File metadata and controls
85 lines (73 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-06-30
* Time: 17:29
*/
namespace Inhere\Console\Utils;
/**
* Class Annotation
* @package Inhere\Console\Utils
*/
final class Annotation
{
/*
* 以下三个方法来自 yii2 console/Controller.php
*/
/**
* Parses the comment block into tags.
*
* @param string $comment The comment block text
* @return array The parsed tags
*/
public static function tagList($comment)
{
$comment = "@description \n" . str_replace("\r", '',
trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))));
$parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
$tags = [];
foreach ($parts as $part) {
if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) {
$name = $matches[1];
if (!isset($tags[$name])) {
$tags[$name] = trim($matches[2]);
} elseif (\is_array($tags[$name])) {
$tags[$name][] = trim($matches[2]);
} else {
$tags[$name] = [$tags[$name], trim($matches[2])];
}
}
}
return $tags;
}
/**
* Returns the first line of docBlock.
*
* @param $comment
* @return string
*/
public static function firstLine($comment): string
{
$docLines = preg_split('~\R~u', $comment);
if (isset($docLines[1])) {
return trim($docLines[1], "\t *");
}
return '';
}
/**
* Returns full description from the doc-block.
* If have multi line text, will return multi line.
*
* @param $comment
* @return string
*/
public static function description($comment): string
{
$comment = str_replace("\r", '', trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))));
if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
$comment = trim(substr($comment, 0, $matches[0][1]));
}
return $comment;
}
}