Skip to content

Commit 76cfb91

Browse files
authored
Merge pull request #8469 from kkmuffme/strictify-anchored-preg-replace
preg_replace with anchor will always only have 1 replacement
2 parents 6374a96 + 15046c9 commit 76cfb91

22 files changed

+42
-42
lines changed

src/Psalm/Codebase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ public function getCompletionItemsForPartialSymbol(
16691669
) {
16701670
$file_contents = $this->getFileContents($file_path);
16711671

1672-
$class_name = preg_replace('/^.*\\\/', '', $fq_class_name);
1672+
$class_name = preg_replace('/^.*\\\/', '', $fq_class_name, 1);
16731673

16741674
if ($aliases->uses_end) {
16751675
$position = self::getPositionFromOffset($aliases->uses_end, $file_contents);

src/Psalm/Config.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ public function setCustomErrorLevel(string $issue_key, string $error_level): voi
13121312
private function loadFileExtensions(SimpleXMLElement $extensions): void
13131313
{
13141314
foreach ($extensions as $extension) {
1315-
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name']);
1315+
$extension_name = preg_replace('/^\.?/', '', (string)$extension['name'], 1);
13161316
$this->file_extensions[] = $extension_name;
13171317

13181318
if (isset($extension['scanner'])) {
@@ -1506,7 +1506,7 @@ private function getPluginClassForPath(Codebase $codebase, string $path, string
15061506
public function shortenFileName(string $to): string
15071507
{
15081508
if (!is_file($to)) {
1509-
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $to);
1509+
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $to, 1);
15101510
}
15111511

15121512
$from = $this->base_dir;
@@ -1678,7 +1678,7 @@ public static function getParentIssueType(string $issue_type): ?string
16781678
}
16791679

16801680
if (strpos($issue_type, 'Possibly') === 0) {
1681-
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type);
1681+
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type, 1);
16821682

16831683
if (strpos($stripped_issue_type, 'Invalid') === false && strpos($stripped_issue_type, 'Un') !== 0) {
16841684
$stripped_issue_type = 'Invalid' . $stripped_issue_type;
@@ -1692,7 +1692,7 @@ public static function getParentIssueType(string $issue_type): ?string
16921692
}
16931693

16941694
if (preg_match('/^(False|Null)[A-Z]/', $issue_type) && !strpos($issue_type, 'Reference')) {
1695-
return preg_replace('/^(False|Null)/', 'Invalid', $issue_type);
1695+
return preg_replace('/^(False|Null)/', 'Invalid', $issue_type, 1);
16961696
}
16971697

16981698
if ($issue_type === 'UndefinedInterfaceMethod') {

src/Psalm/Config/Creator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private static function getPsr4Or0Paths(string $current_dir, array $composer_jso
242242
continue;
243243
}
244244

245-
$path = preg_replace('@[\\\\/]$@', '', $path);
245+
$path = preg_replace('@[/\\\]$@', '', $path, 1);
246246

247247
if ($path !== 'tests') {
248248
$nodes[] = '<directory name="' . $path . '" />';

src/Psalm/Config/FileFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function (): bool {
421421
*/
422422
protected static function slashify(string $str): string
423423
{
424-
return preg_replace('/\/?$/', DIRECTORY_SEPARATOR, $str);
424+
return rtrim( $str, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
425425
}
426426

427427
public function allows(string $file_name, bool $case_sensitive = false): bool

src/Psalm/Context.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ public function hasVariable(string $var_name): bool
752752
return false;
753753
}
754754

755-
$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name);
755+
$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name, 1);
756756

757757
if ($stripped_var !== '$this' || $var_name !== $stripped_var) {
758758
$this->referenced_var_ids[$var_name] = true;

src/Psalm/DocComment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public static function parse(string $docblock, ?int $line_number = null, bool $p
6464
{
6565
// Strip off comments.
6666
$docblock = trim($docblock);
67-
$docblock = preg_replace('@^/\*\*@', '', $docblock);
68-
$docblock = preg_replace('@\*/$@', '', $docblock);
69-
$docblock = preg_replace('@^[ \t]*\*@m', '', $docblock);
67+
$docblock = preg_replace('@^/\*\*@', '', $docblock, 1);
68+
$docblock = preg_replace('@\*/$@', '', $docblock, 1);
69+
$docblock = preg_replace('@^[ \t]*\*@m', '', $docblock, 1);
7070

7171
// Normalize multi-line @specials.
7272
$lines = explode("\n", $docblock);
@@ -157,7 +157,7 @@ public static function parse(string $docblock, ?int $line_number = null, bool $p
157157

158158
// Trim any empty lines off the front, but leave the indent level if there
159159
// is one.
160-
$docblock = preg_replace('/^\s*\n/', '', $docblock);
160+
$docblock = preg_replace('/^\s*\n/', '', $docblock, 1);
161161

162162
foreach ($special as $special_key => $_) {
163163
if (strpos($special_key, 'psalm-') === 0) {

src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public static function checkFullyQualifiedClassLikeName(
231231
return null;
232232
}
233233

234-
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name);
234+
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name, 1);
235235

236236
if (in_array($fq_class_name, ['callable', 'iterable', 'self', 'static', 'parent'], true)) {
237237
return true;

src/Psalm/Internal/Analyzer/NamespaceAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public static function isWithinAny(string $calling_identifier, array $identifier
221221
*/
222222
public static function getNameSpaceRoot(string $fullyQualifiedClassName): string
223223
{
224-
$root_namespace = preg_replace('/^([^\\\]+).*/', '$1', $fullyQualifiedClassName);
224+
$root_namespace = preg_replace('/^([^\\\]+).*/', '$1', $fullyQualifiedClassName, 1);
225225
if ($root_namespace === "") {
226226
throw new InvalidArgumentException("Invalid classname \"$fullyQualifiedClassName\"");
227227
}

src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ private static function getAnalyzeNamedExpression(
735735
if (strpos($var_type_part->value, '::')) {
736736
$parts = explode('::', strtolower($var_type_part->value));
737737
$fq_class_name = $parts[0];
738-
$fq_class_name = preg_replace('/^\\\\/', '', $fq_class_name);
738+
$fq_class_name = preg_replace('/^\\\/', '', $fq_class_name, 1);
739739
$potential_method_id = new MethodIdentifier($fq_class_name, $parts[1]);
740740
} else {
741741
$function_call_info->new_function_name = new VirtualFullyQualified(

src/Psalm/Internal/Analyzer/Statements/Expression/CallAnalyzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ public static function getFunctionIdsFromCallableArg(
522522
}
523523

524524
if ($callable_arg instanceof PhpParser\Node\Scalar\String_) {
525-
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value);
525+
$potential_id = preg_replace('/^\\\/', '', $callable_arg->value, 1);
526526

527527
if (preg_match('/^[A-Za-z0-9_]+(\\\[A-Za-z0-9_]+)*(::[A-Za-z0-9_]+)?$/', $potential_id)) {
528528
return [$potential_id];
@@ -552,7 +552,7 @@ public static function getFunctionIdsFromCallableArg(
552552
}
553553

554554
if ($class_arg instanceof PhpParser\Node\Scalar\String_) {
555-
return [preg_replace('/^\\\/', '', $class_arg->value) . '::' . $method_name_arg->value];
555+
return [preg_replace('/^\\\/', '', $class_arg->value, 1) . '::' . $method_name_arg->value];
556556
}
557557

558558
if ($class_arg instanceof PhpParser\Node\Expr\ClassConstFetch

0 commit comments

Comments
 (0)