Skip to content

Commit dd53e6c

Browse files
committed
Throw exception if the source file unreadable
1 parent bcad0aa commit dd53e6c

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

PHPCtags.class.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ public function __construct()
2020
$this->mParser = new PHPParser_Parser(new PHPParser_Lexer);
2121
}
2222

23+
public function setMFile($file) {
24+
if(empty($file)) {
25+
throw new PHPCtagsException('No File specified.');
26+
}
27+
28+
if(!file_exists($file)) {
29+
throw new PHPCtagsException('Warning: cannot open source file "' . $file . '" : No such file');
30+
}
31+
32+
if(!is_readable($file)) {
33+
throw new PHPCtagsException('Warning: cannot open source file "' . $file . '" : File is not readable');
34+
}
35+
36+
$this->mFile = realpath($file);
37+
}
38+
2339
public static function getMKinds()
2440
{
2541
return self::$mKinds;
@@ -225,9 +241,14 @@ private function render($structs, $options)
225241

226242
public function export($file, $options)
227243
{
228-
//@todo Check for existence
229-
$this->mFile = $file;
244+
$this->setMFile($file);
230245
$structs = $this->struct($this->mParser->parse(file_get_contents($this->mFile)), TRUE);
231246
echo $this->render($structs, $options);
232247
}
233248
}
249+
250+
class PHPCtagsException extends Exception {
251+
public function __toString() {
252+
return "PHPCtags: {$this->message}\n";
253+
}
254+
}

phpctags

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ EOF;
3535
exit;
3636
}
3737

38-
$file = realpath(array_pop($argv));
38+
$file = array_pop($argv);
3939

4040
if(!isset($options['excmd']))
4141
$options['excmd'] = 'pattern';
@@ -47,5 +47,9 @@ if(!isset($options['fields'])) {
4747
$options['fields'] = str_split($options['fields']);
4848
}
4949

50-
$ctags = new PHPCtags();
51-
$ctags->export($file, $options);
50+
try {
51+
$ctags = new PHPCtags();
52+
$ctags->export($file, $options);
53+
} catch (Exception $e) {
54+
die("phpctags: {$e->getMessage()}");
55+
}

0 commit comments

Comments
 (0)