Skip to content
This repository has been archived by the owner on Dec 26, 2021. It is now read-only.

Commit

Permalink
conditional code detection by reg exp is now confirmed by token strat…
Browse files Browse the repository at this point in the history
…egy (to avoid false positive)
  • Loading branch information
llaville committed Jan 26, 2015
1 parent ba1df8f commit 42eb4e9
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/Bartlett/Reflect.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,21 @@ public function parse(Finder $finder)
array(
function (\SplFileInfo $fileinfo) {
$content = php_strip_whitespace($fileinfo->getPathname());
return preg_match('/defined\s*\(/i', $content) > 0;
if (preg_match('/defined\s*\(/i', $content) > 0) {
// must be confirmed to avoid false positive with string content
$tokens = token_get_all($content);

for ($i = 0, $max = count($tokens); $i < $max; $i++) {
if (is_array($tokens[$i])
&& $tokens[$i][0] == T_STRING
&& strcasecmp($tokens[$i][1], 'defined') == 0
) {
// confirmed by token strategy
return true;
}
}
}
return false;
}
)
);
Expand All @@ -159,17 +173,32 @@ function (\SplFileInfo $fileinfo) {
function (\SplFileInfo $fileinfo) {
$content = php_strip_whitespace($fileinfo->getPathname());

$patterns = array(
'/extension_loaded\s*\(/i',
'/function_exists\s*\(/i',
'/method_exists\s*\(/i',
'/class_exists\s*\(/i',
'/interface_exists\s*\(/i',
'/trait_exists\s*\(/i',
$checks = array(
'extension_loaded',
'function_exists',
'method_exists',
'class_exists',
'interface_exists',
'trait_exists',
);
$patterns = array_map(
function($a) { return "/$a\s*\(/i"; },
$checks
);
foreach ($patterns as $regexp) {
if (preg_match($regexp, $content) > 0) {
return true;
// must be confirmed to avoid false positive with string content
$tokens = token_get_all($content);

for ($i = 0, $max = count($tokens); $i < $max; $i++) {
if (is_array($tokens[$i])
&& $tokens[$i][0] == T_STRING
&& in_array(strtolower($tokens[$i][1]), $checks)
) {
// confirmed by token strategy
return true;
}
}
}
}
return false;
Expand Down

0 comments on commit 42eb4e9

Please sign in to comment.