1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Add support for regexes as referencedMethods

This commit is contained in:
Brown 2019-04-12 15:15:21 -04:00
parent 6e010d9db9
commit a86bbad796

View File

@ -273,7 +273,7 @@ class FileFilter
foreach ($e->referencedMethod as $referenced_method) { foreach ($e->referencedMethod as $referenced_method) {
$method_id = (string)$referenced_method['name']; $method_id = (string)$referenced_method['name'];
if (!preg_match('/^[^:]+::[^:]+$/', $method_id)) { if (!preg_match('/^[^:]+::[^:]+$/', $method_id) && !static::isRegularExpression($method_id)) {
echo 'Invalid referencedMethod ' . $method_id . PHP_EOL; echo 'Invalid referencedMethod ' . $method_id . PHP_EOL;
exit(1); exit(1);
} }
@ -299,6 +299,19 @@ class FileFilter
return $filter; return $filter;
} }
private static function isRegularExpression(string $string) : bool
{
set_error_handler(
function () : bool {
return false;
},
E_WARNING
);
$is_regexp = preg_match($string, "") !== FALSE;
restore_error_handler();
return $is_regexp;
}
/** /**
* @param string $str * @param string $str
* *
@ -394,13 +407,30 @@ class FileFilter
*/ */
public function allowsMethod($method_id) public function allowsMethod($method_id)
{ {
if (!$this->method_ids) {
return false;
}
if (preg_match('/^[^:]+::[^:]+$/', $method_id)) { if (preg_match('/^[^:]+::[^:]+$/', $method_id)) {
$method_stub = '*::' . explode('::', $method_id)[1]; $method_stub = '*::' . explode('::', $method_id)[1];
if (in_array($method_stub, $this->method_ids)) { foreach ($this->method_ids as $config_method_id) {
return true; if ($config_method_id === $method_id) {
return true;
}
if ($config_method_id === $method_stub) {
return true;
}
if ($config_method_id[0] === '/' && preg_match($config_method_id, $method_id)) {
return true;
}
} }
return false;
} }
return in_array($method_id, $this->method_ids); return in_array($method_id, $this->method_ids);
} }