@@ -36,9 +36,11 @@ class PathHelper
3636 /**
3737 * Do not create an instance of this class
3838 */
39+ // @codeCoverageIgnoreStart
3940 private function __construct ()
4041 {
4142 }
43+ // @codeCoverageIgnoreEnd
4244
4345 /**
4446 * Check whether this is a syntactically valid absolute path.
@@ -66,19 +68,10 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
6668 || strlen ($ path ) > 1 && '/ ' === $ path [strlen ($ path ) - 1 ]
6769 || preg_match ('-//|/\./|/\.\./- ' , $ path )
6870 ) {
69- if ($ throw ) {
70- throw new RepositoryException ("Invalid path $ path " );
71- }
72-
73- return false ;
71+ return self ::error ("Invalid path $ path " , $ throw );
7472 }
7573 if ($ destination && '] ' === $ path [strlen ($ path ) - 1 ]) {
76- if ($ throw ) {
77- throw new RepositoryException ("Destination path may not end with index $ path " );
78- }
79-
80- return false ;
81-
74+ return self ::error ("Destination path may not end with index $ path " , $ throw );
8275 }
8376
8477 return true ;
@@ -95,7 +88,8 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
9588 * encode and decode characters that are not natively allowed by a storage
9689 * engine.
9790 *
98- * @param string $name The name to check
91+ * @param string $name The name to check
92+ * @param boolean $throw whether to throw an exception on validation errors.
9993 *
10094 * @return bool true if valid, false if not valid and $throw was false
10195 *
@@ -106,11 +100,11 @@ public static function assertValidAbsolutePath($path, $destination = false, $thr
106100 public static function assertValidLocalName ($ name , $ throw = true )
107101 {
108102 if ('. ' == $ name || '.. ' == $ name ) {
109- throw new RepositoryException ( ' Name may not be parent or self identifier: ' . $ name );
103+ return self :: error ( " Name may not be parent or self identifier: $ name" , $ throw );
110104 }
111105
112106 if (preg_match ('/ \\/|:| \\[| \\]| \\|| \\*/ ' , $ name )) {
113- throw new RepositoryException ( ' Name contains illegal characters: ' . $ name );
107+ return self :: error ( " Name contains illegal characters: $ name" , $ throw );
114108 }
115109
116110 return true ;
@@ -141,24 +135,18 @@ public static function assertValidLocalName($name, $throw = true)
141135 public static function normalizePath ($ path , $ destination = false , $ throw = true )
142136 {
143137 if (!is_string ($ path )) {
144- throw new RepositoryException ('Expected string but got ' . gettype ($ path ));
138+ return self :: error ('Expected string but got ' . gettype ($ path ), $ throw );
145139 }
146-
147140 if (strlen ($ path ) === 0 ) {
148- throw new RepositoryException ('Path must not be of zero length ' );
141+ return self :: error ('Path must not be of zero length ' , $ throw );
149142 }
150143
151144 if ('/ ' === $ path ) {
152-
153145 return '/ ' ;
154146 }
155147
156148 if ('/ ' !== $ path [0 ]) {
157- if ($ throw ) {
158- throw new RepositoryException ("Not an absolute path ' $ path' " );
159- }
160-
161- return false ;
149+ return self ::error ("Not an absolute path ' $ path' " , $ throw );
162150 }
163151
164152 $ finalParts = array ();
@@ -211,9 +199,16 @@ public static function normalizePath($path, $destination = false, $throw = true)
211199 */
212200 public static function absolutizePath ($ path , $ context , $ destination = false , $ throw = true )
213201 {
214- if (! $ path ) {
215- throw new RepositoryException ('empty path ' );
202+ if (!is_string ($ path )) {
203+ return self ::error ('Expected string path but got ' . gettype ($ path ), $ throw );
204+ }
205+ if (!is_string ($ context )) {
206+ return self ::error ('Expected string context but got ' . gettype ($ context ), $ throw );
216207 }
208+ if (strlen ($ path ) === 0 ) {
209+ return self ::error ('Path must not be of zero length ' , $ throw );
210+ }
211+
217212 if ('/ ' !== $ path [0 ]) {
218213 $ path = ('/ ' === $ context ) ? "/ $ path " : "$ context/ $ path " ;
219214 }
@@ -270,4 +265,24 @@ public static function getPathDepth($path)
270265 {
271266 return substr_count (rtrim ($ path , '/ ' ), '/ ' );
272267 }
268+
269+ /**
270+ * If $throw is true, throw a RepositoryException with $msg. Otherwise
271+ * return false.
272+ *
273+ * @param string $msg the exception message to use in case of throw being true
274+ * @param boolean $throw whether to throw the exception or return false
275+ *
276+ * @return boolean false
277+ *
278+ * @throws RepositoryException
279+ */
280+ private static function error ($ msg , $ throw )
281+ {
282+ if ($ throw ) {
283+ throw new RepositoryException ($ msg );
284+ }
285+
286+ return false ;
287+ }
273288}
0 commit comments