1212
1313namespace phpDocumentor \Reflection \DocBlock \Tags ;
1414
15+ use phpDocumentor \Reflection \DocBlock \Description ;
1516use phpDocumentor \Reflection \DocBlock \Tag ;
17+ use Webmozart \Assert \Assert ;
1618
1719/**
1820 * Reflection class for a {@}example tag in a Docblock.
@@ -22,14 +24,40 @@ final class Example extends BaseTag
2224 /**
2325 * @var string Path to a file to use as an example. May also be an absolute URI.
2426 */
25- private $ filePath = '' ;
27+ private $ filePath ;
2628
2729 /**
2830 * @var bool Whether the file path component represents an URI. This determines how the file portion
2931 * appears at {@link getContent()}.
3032 */
3133 private $ isURI = false ;
3234
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+
3361 /**
3462 * {@inheritdoc}
3563 */
@@ -43,7 +71,7 @@ public function getContent()
4371 :$ this ->filePath ;
4472 }
4573
46- $ this -> description = $ filePath . ' ' . parent ::getContent ( );
74+ return trim ( $ filePath . ' ' . parent ::getDescription () );
4775 }
4876
4977 return $ this ->description ;
@@ -71,16 +99,29 @@ public static function create($body)
7199 $ lineCount = null ;
72100 $ description = null ;
73101
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 )) {
80103 $ 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+ }
81116 }
82117
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+ );
84125 }
85126
86127 /**
@@ -95,64 +136,40 @@ public function getFilePath()
95136 }
96137
97138 /**
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.
101140 *
102- * @return $this
141+ * @return string
103142 */
104- public function setFilePath ( $ filePath )
143+ public function __toString ( )
105144 {
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 : '' );
111146 }
112147
113148 /**
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).
120150 *
121- * @param string $uri The new file URI to use as an example.
151+ * @param string $uri
122152 *
123- * @return $this
153+ * @return bool
124154 */
125- public function setFileURI ($ uri )
155+ private function isUriRelative ($ uri )
126156 {
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 , ': ' );
135158 }
136159
137160 /**
138- * Returns a string representation for this tag.
139- *
140- * @return string
161+ * @return int
141162 */
142- public function __toString ()
163+ public function getStartingLine ()
143164 {
144- return $ this ->filePath . ( $ this -> description ? ' ' . $ this -> description -> render () : '' ) ;
165+ return $ this ->startingLine ;
145166 }
146167
147168 /**
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
153170 */
154- private function isUriRelative ( $ uri )
171+ public function getLineCount ( )
155172 {
156- return false === strpos ( $ uri , ' : ' ) ;
173+ return $ this -> lineCount ;
157174 }
158175}
0 commit comments