Skip to content

Commit ee63140

Browse files
committed
refactor error enhandling of path_helper
Signed-off-by: Hongyi Zhang <hongyi73.zhang@gmail.com>
1 parent 2eacf4c commit ee63140

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

system/helpers/path_helper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@
5757
* @param string
5858
* @param bool checks to see if the path exists
5959
* @return string
60+
* @throws InvalidArgumentException
6061
*/
6162
function set_realpath($path, $check_existance = FALSE)
6263
{
6364
// Security check to make sure the path is NOT a URL. No remote file inclusion!
6465
if (preg_match('#^(http:\/\/|https:\/\/|www\.|ftp)#i', $path) OR filter_var($path, FILTER_VALIDATE_IP) === $path )
6566
{
66-
show_error('The path you submitted must be a local server path, not a URL');
67+
throw new InvalidArgumentException('The path you submitted must be a local server path, not a URL');
6768
}
6869

6970
// Resolve the path
@@ -73,7 +74,7 @@ function set_realpath($path, $check_existance = FALSE)
7374
}
7475
elseif ($check_existance && ! is_dir($path) && ! is_file($path))
7576
{
76-
show_error('Not a valid path: '.$path);
77+
throw new InvalidArgumentException('Not a valid path: '.$path);
7778
}
7879

7980
// Add a trailing slash, if this is a directory

tests/codeigniter/helpers/path_helper_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function test_set_realpath_nonexistent_directory()
2121
public function test_set_realpath_error_trigger()
2222
{
2323
$this->setExpectedException(
24-
'RuntimeException', 'CI Error: Not a valid path: /path/to/nowhere'
24+
'InvalidArgumentException', 'Not a valid path: /path/to/nowhere'
2525
);
2626

2727
set_realpath('/path/to/nowhere', TRUE);

user_guide_src/source/helpers/path_helper.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following functions are available:
3131
:param bool $check_existance: Whether to check if the path actually exists
3232
:returns: An absolute path
3333
:rtype: string
34+
:throws: InvalidArgumentException
3435

3536
This function will return a server path without symbolic links or
3637
relative directory structures. An optional second argument will
@@ -50,4 +51,4 @@ The following functions are available:
5051

5152
$non_existent_directory = '/path/to/nowhere';
5253
echo set_realpath($non_existent_directory, TRUE); // Shows an error, as the path cannot be resolved
53-
echo set_realpath($non_existent_directory, FALSE); // Prints '/path/to/nowhere'
54+
echo set_realpath($non_existent_directory, FALSE); // Prints '/path/to/nowhere'

0 commit comments

Comments
 (0)