12
12
13
13
namespace phpDocumentor \Reflection \DocBlock \Tags ;
14
14
15
+ use phpDocumentor \Reflection \DocBlock \Description ;
15
16
use phpDocumentor \Reflection \DocBlock \Tag ;
17
+ use Webmozart \Assert \Assert ;
16
18
17
19
/**
18
20
* Reflection class for a {@}example tag in a Docblock.
@@ -22,14 +24,40 @@ final class Example extends BaseTag
22
24
/**
23
25
* @var string Path to a file to use as an example. May also be an absolute URI.
24
26
*/
25
- private $ filePath = '' ;
27
+ private $ filePath ;
26
28
27
29
/**
28
30
* @var bool Whether the file path component represents an URI. This determines how the file portion
29
31
* appears at {@link getContent()}.
30
32
*/
31
33
private $ isURI = false ;
32
34
35
+ /**
36
+ * @var
37
+ */
38
+ private $ startingLine ;
39
+
40
+ /**
41
+ * @var
42
+ */
43
+ private $ lineCount ;
44
+
45
+ public function __construct ($ filePath , $ isURI , $ startingLine , $ lineCount , $ description )
46
+ {
47
+ Assert::notEmpty ($ filePath );
48
+ Assert::integer ($ startingLine );
49
+ Assert::greaterThanEq ($ startingLine , 0 );
50
+
51
+ $ this ->filePath = $ filePath ;
52
+ $ this ->startingLine = $ startingLine ;
53
+ $ this ->lineCount = $ lineCount ;
54
+ if ($ description !== null ) {
55
+ $ this ->description = trim ($ description );
56
+ }
57
+
58
+ $ this ->isURI = $ isURI ;
59
+ }
60
+
33
61
/**
34
62
* {@inheritdoc}
35
63
*/
@@ -43,7 +71,7 @@ public function getContent()
43
71
:$ this ->filePath ;
44
72
}
45
73
46
- $ this -> description = $ filePath . ' ' . parent ::getContent ( );
74
+ return trim ( $ filePath . ' ' . parent ::getDescription () );
47
75
}
48
76
49
77
return $ this ->description ;
@@ -71,16 +99,29 @@ public static function create($body)
71
99
$ lineCount = null ;
72
100
$ description = null ;
73
101
74
- // Starting line / Number of lines / Description
75
- if (preg_match ('/^([1-9]\d*)\s*(?:((?1))\s+)?(.*)$/sux ' , $ matches [3 ], $ matches )) {
76
- $ startingLine = (int )$ matches [1 ];
77
- if (isset ($ matches [2 ]) && $ matches [2 ] !== '' ) {
78
- $ lineCount = (int )$ matches [2 ];
79
- }
102
+ if (array_key_exists (3 , $ matches )) {
80
103
$ description = $ matches [3 ];
104
+
105
+ // Starting line / Number of lines / Description
106
+ if (preg_match ('/^([1-9]\d*)(?:\s+((?1))\s*)?(.*)$/sux ' , $ matches [3 ], $ contentMatches )) {
107
+ $ startingLine = (int )$ contentMatches [1 ];
108
+ if (isset ($ contentMatches [2 ]) && $ contentMatches [2 ] !== '' ) {
109
+ $ lineCount = (int )$ contentMatches [2 ];
110
+ }
111
+
112
+ if (array_key_exists (3 , $ contentMatches )) {
113
+ $ description = $ contentMatches [3 ];
114
+ }
115
+ }
81
116
}
82
117
83
- return new static ($ filePath , $ fileUri , $ startingLine , $ lineCount , $ description );
118
+ return new static (
119
+ $ filePath !== null ?$ filePath :$ fileUri ,
120
+ $ fileUri !== null ,
121
+ $ startingLine ,
122
+ $ lineCount ,
123
+ $ description
124
+ );
84
125
}
85
126
86
127
/**
@@ -95,64 +136,40 @@ public function getFilePath()
95
136
}
96
137
97
138
/**
98
- * Sets the file path.
99
- *
100
- * @param string $filePath The new file path to use for the example.
139
+ * Returns a string representation for this tag.
101
140
*
102
- * @return $this
141
+ * @return string
103
142
*/
104
- public function setFilePath ( $ filePath )
143
+ public function __toString ( )
105
144
{
106
- $ this ->isURI = false ;
107
- $ this ->filePath = trim ($ filePath );
108
-
109
- $ this ->description = null ;
110
- return $ this ;
145
+ return $ this ->filePath . ($ this ->description ? ' ' . $ this ->description : '' );
111
146
}
112
147
113
148
/**
114
- * Sets the file path as an URI.
115
- *
116
- * This function is equivalent to {@link setFilePath()}, except that it
117
- * converts an URI to a file path before that.
118
- *
119
- * There is no getFileURI(), as {@link getFilePath()} is compatible.
149
+ * Returns true if the provided URI is relative or contains a complete scheme (and thus is absolute).
120
150
*
121
- * @param string $uri The new file URI to use as an example.
151
+ * @param string $uri
122
152
*
123
- * @return $this
153
+ * @return bool
124
154
*/
125
- public function setFileURI ($ uri )
155
+ private function isUriRelative ($ uri )
126
156
{
127
- $ this ->isURI = true ;
128
- $ this ->description = null ;
129
-
130
- $ this ->filePath = $ this ->isUriRelative ($ uri )
131
- ? rawurldecode (str_replace (array ('/ ' , '\\' ), '%2F ' , $ uri ))
132
- : $ this ->filePath = $ uri ;
133
-
134
- return $ this ;
157
+ return false === strpos ($ uri , ': ' );
135
158
}
136
159
137
160
/**
138
- * Returns a string representation for this tag.
139
- *
140
- * @return string
161
+ * @return int
141
162
*/
142
- public function __toString ()
163
+ public function getStartingLine ()
143
164
{
144
- return $ this ->filePath . ( $ this -> description ? ' ' . $ this -> description -> render () : '' ) ;
165
+ return $ this ->startingLine ;
145
166
}
146
167
147
168
/**
148
- * Returns true if the provided URI is relative or contains a complete scheme (and thus is absolute).
149
- *
150
- * @param string $uri
151
- *
152
- * @return bool
169
+ * @return int
153
170
*/
154
- private function isUriRelative ( $ uri )
171
+ public function getLineCount ( )
155
172
{
156
- return false === strpos ( $ uri , ' : ' ) ;
173
+ return $ this -> lineCount ;
157
174
}
158
175
}
0 commit comments