Skip to content

Commit

Permalink
Adding -l, --directory CLI option for specifying a directory to recur…
Browse files Browse the repository at this point in the history
…sively scan for PHP files
  • Loading branch information
morria committed Jan 7, 2016
1 parent 7ab21cb commit 5ac1893
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
40 changes: 29 additions & 11 deletions src/Phan/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function __construct() {
// file_put_contents('/tmp/file', implode("\n", $argv));

// Parse command line args
// still available: e,g,j,k,l,n,t,u,v,w,x,z
// still available: e,g,j,k,n,t,u,v,w,x,z
$opts = getopt(
"f:m:o:c:aqbrpid:s:3:y:h::", [
"f:m:o:c:aqbrpid:s:3:y:l:h::", [
'fileset:',
'output-mode:',
'output:',
Expand All @@ -40,6 +40,7 @@ public function __construct() {
'state-file:',
'exclude-directory-list:',
'minimum-severity:',
'directory:',
'help',
]
);
Expand All @@ -63,12 +64,30 @@ public function __construct() {
case 'f':
case 'fileset':
if(is_file($value) && is_readable($value)) {
$this->file_list = file(
$value,
FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES
$this->file_list = array_merge(
$this->file_list,
file($value, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)
);
} else {
throw new \Exception("Unable to open $value");
error_log("Unable to read file $value");
}
break;
case 'l':
case 'directory':
try {
$iterator = new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($value)
),
'/^.+\.php$/i',
\RecursiveRegexIterator::GET_MATCH
);
$this->file_list = array_merge(
$this->file_list,
array_keys(iterator_to_array($iterator))
);
} catch (\Exception $exception) {
error_log($exception->getMessage());
}
break;
case 'm':
Expand Down Expand Up @@ -153,10 +172,6 @@ public function __construct() {
unset($argv[$key]);
}

if(empty($this->file_list) && count($argv) < 2) {
// Ignore
}

foreach($argv as $arg) if($arg[0]=='-') {
$this->usage("Unknown option '{$arg}'");
}
Expand Down Expand Up @@ -187,6 +202,9 @@ private function usage(string $msg='') {
-f, --fileset <filename>
A file containing a list of PHP files to be analyzed
-l, --directory <directory>
A directory to recursively read PHP files from to analyze
-3, --exclude-directory-list <dir_list>
A comma-separated list of directories for which any files
therein should be parsed but not analyzed.
Expand All @@ -200,7 +218,7 @@ private function usage(string $msg='') {
-i, --ignore-undeclared
Ignore undeclared functions and classes
-y, --minimum-severity <level in {0,5,10}>
-y, --minimum-severity <level in {0,5,10}>
Minimum severity level (low=0, normal=5, critical=10) to report.
Defaults to 0.
Expand Down
25 changes: 15 additions & 10 deletions src/Phan/Phan.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,26 @@ public function analyzeFileList(
if (Config::get()->reanalyze_file_list
|| !$code_base->isParseUpToDateForFile($file_path)) {

// Save this to the set of files to analyze
$analyze_file_path_list[] = $file_path;

// Kick out anything we read from the former version
// of this file
$code_base->flushDependenciesForFile(
$file_path
);

// Parse the file
$this->parseFile($code_base, $file_path);
try {
// Parse the file
$this->parseFile($code_base, $file_path);

// Update the timestamp on when it was last
// parsed
$code_base->setParseUpToDateForFile($file_path);

// Update the timestamp on when it was last
// parsed
$code_base->setParseUpToDateForFile($file_path);
// Save this to the set of files to analyze
$analyze_file_path_list[] = $file_path;

} catch (\Throwable $throwable) {
error_log($throwable->getMessage());
}
}
}

Expand Down Expand Up @@ -144,6 +149,8 @@ public function parseFile(
string $file_path
) : Context {

$context = (new Context)->withFile($file_path);

// Convert the file to an Abstract Syntax Tree
// before passing it on to the recursive version
// of this method
Expand All @@ -152,8 +159,6 @@ public function parseFile(
Config::get()->ast_version
);

$context = (new Context)->withFile($file_path);

if (Config::get()->dump_ast) {
echo $file_path . "\n"
. str_repeat("\u{00AF}", strlen($file_path))
Expand Down

0 comments on commit 5ac1893

Please sign in to comment.