mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 12:55:26 +01:00
Merge pull request #8469 from kkmuffme/strictify-anchored-preg-replace
preg_replace with anchor will always only have 1 replacement
This commit is contained in:
commit
76cfb911b2
@ -1669,7 +1669,7 @@ class Codebase
|
||||
) {
|
||||
$file_contents = $this->getFileContents($file_path);
|
||||
|
||||
$class_name = preg_replace('/^.*\\\/', '', $fq_class_name);
|
||||
$class_name = preg_replace('/^.*\\\/', '', $fq_class_name, 1);
|
||||
|
||||
if ($aliases->uses_end) {
|
||||
$position = self::getPositionFromOffset($aliases->uses_end, $file_contents);
|
||||
|
@ -1312,7 +1312,7 @@ class Config
|
||||
private function loadFileExtensions(SimpleXMLElement $extensions): void
|
||||
{
|
||||
foreach ($extensions as $extension) {
|
||||
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name']);
|
||||
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name'], 1);
|
||||
$this->file_extensions[] = $extension_name;
|
||||
|
||||
if (isset($extension['scanner'])) {
|
||||
@ -1506,7 +1506,7 @@ class Config
|
||||
public function shortenFileName(string $to): string
|
||||
{
|
||||
if (!is_file($to)) {
|
||||
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $to);
|
||||
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $to, 1);
|
||||
}
|
||||
|
||||
$from = $this->base_dir;
|
||||
@ -1678,7 +1678,7 @@ class Config
|
||||
}
|
||||
|
||||
if (strpos($issue_type, 'Possibly') === 0) {
|
||||
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type);
|
||||
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type, 1);
|
||||
|
||||
if (strpos($stripped_issue_type, 'Invalid') === false && strpos($stripped_issue_type, 'Un') !== 0) {
|
||||
$stripped_issue_type = 'Invalid' . $stripped_issue_type;
|
||||
@ -1692,7 +1692,7 @@ class Config
|
||||
}
|
||||
|
||||
if (preg_match('/^(False|Null)[A-Z]/', $issue_type) && !strpos($issue_type, 'Reference')) {
|
||||
return preg_replace('/^(False|Null)/', 'Invalid', $issue_type);
|
||||
return preg_replace('/^(False|Null)/', 'Invalid', $issue_type, 1);
|
||||
}
|
||||
|
||||
if ($issue_type === 'UndefinedInterfaceMethod') {
|
||||
|
@ -242,7 +242,7 @@ class Creator
|
||||
continue;
|
||||
}
|
||||
|
||||
$path = preg_replace('@[\\\\/]$@', '', $path);
|
||||
$path = preg_replace('@[/\\\]$@', '', $path, 1);
|
||||
|
||||
if ($path !== 'tests') {
|
||||
$nodes[] = '<directory name="' . $path . '" />';
|
||||
|
@ -421,7 +421,7 @@ class FileFilter
|
||||
*/
|
||||
protected static function slashify(string $str): string
|
||||
{
|
||||
return preg_replace('/\/?$/', DIRECTORY_SEPARATOR, $str);
|
||||
return rtrim( $str, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
public function allows(string $file_name, bool $case_sensitive = false): bool
|
||||
|
@ -752,7 +752,7 @@ class Context
|
||||
return false;
|
||||
}
|
||||
|
||||
$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name);
|
||||
$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name, 1);
|
||||
|
||||
if ($stripped_var !== '$this' || $var_name !== $stripped_var) {
|
||||
$this->referenced_var_ids[$var_name] = true;
|
||||
|
@ -64,9 +64,9 @@ class DocComment
|
||||
{
|
||||
// Strip off comments.
|
||||
$docblock = trim($docblock);
|
||||
$docblock = preg_replace('@^/\*\*@', '', $docblock);
|
||||
$docblock = preg_replace('@\*/$@', '', $docblock);
|
||||
$docblock = preg_replace('@^[ \t]*\*@m', '', $docblock);
|
||||
$docblock = preg_replace('@^/\*\*@', '', $docblock, 1);
|
||||
$docblock = preg_replace('@\*/$@', '', $docblock, 1);
|
||||
$docblock = preg_replace('@^[ \t]*\*@m', '', $docblock, 1);
|
||||
|
||||
// Normalize multi-line @specials.
|
||||
$lines = explode("\n", $docblock);
|
||||
@ -157,7 +157,7 @@ class DocComment
|
||||
|
||||
// Trim any empty lines off the front, but leave the indent level if there
|
||||
// is one.
|
||||
$docblock = preg_replace('/^\s*\n/', '', $docblock);
|
||||
$docblock = preg_replace('/^\s*\n/', '', $docblock, 1);
|
||||
|
||||
foreach ($special as $special_key => $_) {
|
||||
if (strpos($special_key, 'psalm-') === 0) {
|
||||
|
@ -231,7 +231,7 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer
|
||||
return null;
|
||||
}
|
||||
|
||||
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
|
||||
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name, 1);
|
||||
|
||||
if (in_array($fq_class_name, ['callable', 'iterable', 'self', 'static', 'parent'], true)) {
|
||||
return true;
|
||||
|
@ -221,7 +221,7 @@ class NamespaceAnalyzer extends SourceAnalyzer
|
||||
*/
|
||||
public static function getNameSpaceRoot(string $fullyQualifiedClassName): string
|
||||
{
|
||||
$root_namespace = preg_replace('/^([^\\\]+).*/', '$1', $fullyQualifiedClassName);
|
||||
$root_namespace = preg_replace('/^([^\\\]+).*/', '$1', $fullyQualifiedClassName, 1);
|
||||
if ($root_namespace === "") {
|
||||
throw new InvalidArgumentException("Invalid classname \"$fullyQualifiedClassName\"");
|
||||
}
|
||||
|
@ -735,7 +735,7 @@ class FunctionCallAnalyzer extends CallAnalyzer
|
||||
if (strpos($var_type_part->value, '::')) {
|
||||
$parts = explode('::', strtolower($var_type_part->value));
|
||||
$fq_class_name = $parts[0];
|
||||
$fq_class_name = preg_replace('/^\\\\/', '', $fq_class_name);
|
||||
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name, 1);
|
||||
$potential_method_id = new MethodIdentifier($fq_class_name, $parts[1]);
|
||||
} else {
|
||||
$function_call_info->new_function_name = new VirtualFullyQualified(
|
||||
|
@ -522,7 +522,7 @@ class CallAnalyzer
|
||||
}
|
||||
|
||||
if ($callable_arg instanceof PhpParser\Node\Scalar\String_) {
|
||||
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value);
|
||||
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value, 1);
|
||||
|
||||
if (preg_match('/^[A-Za-z0-9_]+(\\\[A-Za-z0-9_]+)*(::[A-Za-z0-9_]+)?$/', $potential_id)) {
|
||||
return [$potential_id];
|
||||
@ -552,7 +552,7 @@ class CallAnalyzer
|
||||
}
|
||||
|
||||
if ($class_arg instanceof PhpParser\Node\Scalar\String_) {
|
||||
return [preg_replace('/^\\\/', '', $class_arg->value) . '::' . $method_name_arg->value];
|
||||
return [preg_replace('/^\\\/', '', $class_arg->value, 1) . '::' . $method_name_arg->value];
|
||||
}
|
||||
|
||||
if ($class_arg instanceof PhpParser\Node\Expr\ClassConstFetch
|
||||
|
@ -93,7 +93,7 @@ final class LanguageServer
|
||||
array_map(
|
||||
function (string $arg) use ($valid_long_options): void {
|
||||
if (strpos($arg, '--') === 0 && $arg !== '--') {
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2), 1);
|
||||
|
||||
if (!in_array($arg_name, $valid_long_options, true)
|
||||
&& !in_array($arg_name . ':', $valid_long_options, true)
|
||||
|
@ -431,7 +431,7 @@ final class Psalm
|
||||
array_map(
|
||||
function (string $arg): void {
|
||||
if (strpos($arg, '--') === 0 && $arg !== '--') {
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2), 1);
|
||||
|
||||
if (!in_array($arg_name, self::LONG_OPTIONS)
|
||||
&& !in_array($arg_name . ':', self::LONG_OPTIONS)
|
||||
|
@ -432,7 +432,7 @@ HELP;
|
||||
array_map(
|
||||
function (string $arg): void {
|
||||
if (strpos($arg, '--') === 0 && $arg !== '--') {
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2), 1);
|
||||
|
||||
if ($arg_name === 'alter') {
|
||||
// valid option for psalm, ignored by psalter
|
||||
|
@ -82,7 +82,7 @@ final class Refactor
|
||||
array_map(
|
||||
function (string $arg) use ($valid_long_options): void {
|
||||
if (strpos($arg, '--') === 0 && $arg !== '--') {
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
|
||||
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2), 1);
|
||||
|
||||
if ($arg_name === 'refactor') {
|
||||
// valid option for psalm, ignored by psalter
|
||||
|
@ -773,7 +773,7 @@ class Analyzer
|
||||
$method_param_uses[$member_id]
|
||||
);
|
||||
|
||||
$member_stub = preg_replace('/::.*$/', '::*', $member_id);
|
||||
$member_stub = preg_replace('/::.*$/', '::*', $member_id, 1);
|
||||
|
||||
if (isset($all_referencing_methods[$member_stub])) {
|
||||
$newly_invalidated_methods = array_merge(
|
||||
|
@ -185,7 +185,7 @@ class ClassLikes
|
||||
$predefined_classes = get_declared_classes();
|
||||
|
||||
foreach ($predefined_classes as $predefined_class) {
|
||||
$predefined_class = preg_replace('/^\\\/', '', $predefined_class);
|
||||
$predefined_class = preg_replace('/^\\\/', '', $predefined_class, 1);
|
||||
/** @psalm-suppress ArgumentTypeCoercion */
|
||||
$reflection_class = new ReflectionClass($predefined_class);
|
||||
|
||||
@ -201,7 +201,7 @@ class ClassLikes
|
||||
$predefined_interfaces = get_declared_interfaces();
|
||||
|
||||
foreach ($predefined_interfaces as $predefined_interface) {
|
||||
$predefined_interface = preg_replace('/^\\\/', '', $predefined_interface);
|
||||
$predefined_interface = preg_replace('/^\\\/', '', $predefined_interface, 1);
|
||||
/** @psalm-suppress ArgumentTypeCoercion */
|
||||
$reflection_class = new ReflectionClass($predefined_interface);
|
||||
|
||||
|
@ -83,8 +83,8 @@ class Properties
|
||||
?Context $context = null,
|
||||
?CodeLocation $code_location = null
|
||||
): bool {
|
||||
// remove trailing backslash if it exists
|
||||
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
||||
// remove leading backslash if it exists
|
||||
$property_id = ltrim( $property_id, '\\' );
|
||||
|
||||
[$fq_class_name, $property_name] = explode('::$', $property_id);
|
||||
$fq_class_name_lc = strtolower($fq_class_name);
|
||||
@ -248,8 +248,8 @@ class Properties
|
||||
|
||||
public function getStorage(string $property_id): PropertyStorage
|
||||
{
|
||||
// remove trailing backslash if it exists
|
||||
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
||||
// remove leading backslash if it exists
|
||||
$property_id = ltrim( $property_id, '\\' );
|
||||
|
||||
[$fq_class_name, $property_name] = explode('::$', $property_id);
|
||||
|
||||
@ -269,8 +269,8 @@ class Properties
|
||||
|
||||
public function hasStorage(string $property_id): bool
|
||||
{
|
||||
// remove trailing backslash if it exists
|
||||
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
||||
// remove leading backslash if it exists
|
||||
$property_id = ltrim( $property_id, '\\' );
|
||||
|
||||
[$fq_class_name, $property_name] = explode('::$', $property_id);
|
||||
|
||||
@ -291,8 +291,8 @@ class Properties
|
||||
?StatementsSource $source = null,
|
||||
?Context $context = null
|
||||
): ?Union {
|
||||
// remove trailing backslash if it exists
|
||||
$property_id = preg_replace('/^\\\\/', '', $property_id);
|
||||
// remove leading backslash if it exists
|
||||
$property_id = ltrim( $property_id, '\\' );
|
||||
|
||||
[$fq_class_name, $property_name] = explode('::$', $property_id);
|
||||
|
||||
|
@ -56,8 +56,8 @@ class MethodIdentifier
|
||||
if (!static::isValidMethodIdReference($method_id)) {
|
||||
throw new InvalidArgumentException('Invalid method id reference provided: ' . $method_id);
|
||||
}
|
||||
// remove trailing backslash if it exists
|
||||
$method_id = preg_replace('/^\\\\/', '', $method_id);
|
||||
// remove leading backslash if it exists
|
||||
$method_id = ltrim($method_id, '\\');
|
||||
$method_id_parts = explode('::', $method_id);
|
||||
return new self($method_id_parts[0], strtolower($method_id_parts[1]));
|
||||
}
|
||||
|
@ -506,7 +506,7 @@ class ClassLikeDocblockParser
|
||||
) {
|
||||
$line_parts[1] = str_replace('&', '', $line_parts[1]);
|
||||
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1], 1);
|
||||
|
||||
$end = $offset + strlen($line_parts[0]);
|
||||
|
||||
|
@ -1838,8 +1838,8 @@ class ClassLikeNodeScanner
|
||||
|
||||
$type_string = str_replace("\n", '', implode('', $var_line_parts));
|
||||
|
||||
$type_string = preg_replace('/>[^>^\}]*$/', '>', $type_string);
|
||||
$type_string = preg_replace('/\}[^>^\}]*$/', '}', $type_string);
|
||||
$type_string = preg_replace('/>[^>^\}]*$/', '>', $type_string, 1);
|
||||
$type_string = preg_replace('/\}[^>^\}]*$/', '}', $type_string, 1);
|
||||
|
||||
try {
|
||||
$type_tokens = TypeTokenizer::getFullyQualifiedTokens(
|
||||
|
@ -76,7 +76,7 @@ class FunctionLikeDocblockParser
|
||||
) {
|
||||
$line_parts[1] = str_replace('&', '', $line_parts[1]);
|
||||
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1], 1);
|
||||
|
||||
$end = $offset + strlen($line_parts[0]);
|
||||
|
||||
@ -152,7 +152,7 @@ class FunctionLikeDocblockParser
|
||||
throw new IncorrectDocblockException('Misplaced variable');
|
||||
}
|
||||
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1], 1);
|
||||
|
||||
$info->params_out[] = [
|
||||
'name' => trim($line_parts[1]),
|
||||
@ -340,7 +340,7 @@ class FunctionLikeDocblockParser
|
||||
throw new IncorrectDocblockException('Misplaced variable');
|
||||
}
|
||||
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1]);
|
||||
$line_parts[1] = preg_replace('/,$/', '', $line_parts[1], 1);
|
||||
|
||||
$info->globals[] = [
|
||||
'name' => $line_parts[1],
|
||||
|
@ -111,7 +111,7 @@ class DocblockParser
|
||||
// Strip the leading *, if present.
|
||||
$text = $lines[$k];
|
||||
$text = str_replace("\t", ' ', $text);
|
||||
$text = preg_replace('/^ *\*/', '', $text);
|
||||
$text = preg_replace('/^ *\*/', '', $text, 1);
|
||||
$lines[$k] = $text;
|
||||
}
|
||||
|
||||
@ -142,7 +142,7 @@ class DocblockParser
|
||||
|
||||
// Trim any empty lines off the front, but leave the indent level if there
|
||||
// is one.
|
||||
$docblock = preg_replace('/^\s*\n/', '', $docblock);
|
||||
$docblock = preg_replace('/^\s*\n/', '', $docblock, 1);
|
||||
|
||||
$parsed = new ParsedDocblock($docblock, $special, $first_line_padding ?: '');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user