Skip to content

Commit 05607cf

Browse files
zonuexeondrejmirtes
authored andcommitted
Use str_starts_with() and str_contains() instead of strpos()
1 parent 5380414 commit 05607cf

33 files changed

+84
-81
lines changed

src/Analyser/NameScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use function implode;
1414
use function ltrim;
1515
use function sprintf;
16-
use function strpos;
16+
use function str_starts_with;
1717
use function strtolower;
1818

1919
/** @api */
@@ -71,7 +71,7 @@ public function getClassNameForTypeAlias(): ?string
7171

7272
public function resolveStringName(string $name): string
7373
{
74-
if (strpos($name, '\\') === 0) {
74+
if (str_starts_with($name, '\\')) {
7575
return ltrim($name, '\\');
7676
}
7777

@@ -97,7 +97,7 @@ public function resolveStringName(string $name): string
9797
*/
9898
public function resolveConstantNames(string $name): array
9999
{
100-
if (strpos($name, '\\') === 0) {
100+
if (str_starts_with($name, '\\')) {
101101
return [ltrim($name, '\\')];
102102
}
103103

src/Command/CommandHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
use function register_shutdown_function;
5555
use function spl_autoload_functions;
5656
use function sprintf;
57+
use function str_contains;
5758
use function str_repeat;
58-
use function strpos;
5959
use function sys_get_temp_dir;
6060
use const DIRECTORY_SEPARATOR;
6161
use const E_ERROR;
@@ -144,7 +144,7 @@ public static function begin(
144144
return;
145145
}
146146

147-
if (strpos($error['message'], 'Allowed memory size') === false) {
147+
if (!str_contains($error['message'], 'Allowed memory size')) {
148148
return;
149149
}
150150

src/Command/IgnoredRegexValidator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use PHPStan\Type\ObjectType;
1313
use PHPStan\Type\VerbosityLevel;
1414
use function count;
15-
use function strpos;
15+
use function str_contains;
16+
use function str_starts_with;
1617
use function strrpos;
1718
use function substr;
1819

@@ -34,12 +35,12 @@ public function validate(string $regex): IgnoredRegexValidatorResult
3435
/** @var TreeNode $ast */
3536
$ast = $this->parser->parse($regex);
3637
} catch (Exception $e) {
37-
if (strpos($e->getMessage(), 'Unexpected token "|" (alternation) at line 1') === 0) {
38+
if (str_starts_with($e->getMessage(), 'Unexpected token "|" (alternation) at line 1')) {
3839
return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|');
3940
}
4041
if (
41-
strpos($regex, '()') !== false
42-
&& strpos($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1') === 0
42+
str_contains($regex, '()')
43+
&& str_starts_with($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1')
4344
) {
4445
return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)');
4546
}

src/DependencyInjection/NeonAdapter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
use function is_string;
2323
use function ltrim;
2424
use function sprintf;
25-
use function strpos;
25+
use function str_contains;
26+
use function str_starts_with;
2627
use function substr;
2728

2829
class NeonAdapter implements Adapter
@@ -121,7 +122,7 @@ public function process(array $arr, string $fileKey, string $file): array
121122
'[parameters][symfony][container_xml_path]',
122123
'[parameters][symfony][containerXmlPath]',
123124
'[parameters][doctrine][objectManagerLoader]',
124-
], true) && is_string($val) && strpos($val, '%') === false && strpos($val, '*') !== 0) {
125+
], true) && is_string($val) && !str_contains($val, '%') && !str_starts_with($val, '*')) {
125126
$fileHelper = $this->createFileHelperByFile($file);
126127
$val = $fileHelper->normalizePath($fileHelper->absolutizePath($val));
127128
}

src/File/FileExcluder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use function fnmatch;
66
use function in_array;
77
use function preg_match;
8+
use function str_starts_with;
89
use function strlen;
9-
use function strpos;
1010
use const DIRECTORY_SEPARATOR;
1111
use const FNM_CASEFOLD;
1212
use const FNM_NOESCAPE;
@@ -65,7 +65,7 @@ public function __construct(
6565
public function isExcludedFromAnalysing(string $file): bool
6666
{
6767
foreach ($this->literalAnalyseExcludes as $exclude) {
68-
if (strpos($file, $exclude) === 0) {
68+
if (str_starts_with($file, $exclude)) {
6969
return true;
7070
}
7171
}

src/File/FileHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function rtrim;
1212
use function str_replace;
1313
use function strlen;
14-
use function strpos;
14+
use function str_starts_with;
1515
use function strtolower;
1616
use function substr;
1717
use function trim;
@@ -78,7 +78,7 @@ public function normalizePath(string $originalPath, string $directorySeparator =
7878

7979
$path = str_replace(['\\', '//', '///', '////'], '/', $path);
8080

81-
$pathRoot = strpos($path, '/') === 0 ? $directorySeparator : '';
81+
$pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';
8282
$pathParts = explode('/', trim($path, '/'));
8383

8484
$normalizedPathParts = [];

src/File/FuzzyRelativePathHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use function ltrim;
1010
use function realpath;
1111
use function str_ends_with;
12+
use function str_starts_with;
1213
use function strlen;
13-
use function strpos;
1414
use function substr;
1515
use const DIRECTORY_SEPARATOR;
1616

@@ -107,7 +107,7 @@ public function getRelativePath(string $filename): string
107107
{
108108
if (
109109
$this->pathToTrim !== null
110-
&& strpos($filename, $this->pathToTrim) === 0
110+
&& str_starts_with($filename, $this->pathToTrim)
111111
) {
112112
return ltrim(substr($filename, strlen($this->pathToTrim)), $this->directorySeparator);
113113
}

src/File/SimpleRelativePathHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace PHPStan\File;
44

55
use function str_replace;
6+
use function str_starts_with;
67
use function strlen;
7-
use function strpos;
88
use function substr;
99

1010
class SimpleRelativePathHelper implements RelativePathHelper
@@ -16,7 +16,7 @@ public function __construct(private string $currentWorkingDirectory)
1616

1717
public function getRelativePath(string $filename): string
1818
{
19-
if ($this->currentWorkingDirectory !== '' && strpos($filename, $this->currentWorkingDirectory) === 0) {
19+
if ($this->currentWorkingDirectory !== '' && str_starts_with($filename, $this->currentWorkingDirectory)) {
2020
return str_replace('\\', '/', substr($filename, strlen($this->currentWorkingDirectory) + 1));
2121
}
2222

src/Parser/PathRoutingParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPStan\File\FileHelper;
66
use function array_fill_keys;
7-
use function strpos;
7+
use function str_contains;
88

99
class PathRoutingParser implements Parser
1010
{
@@ -32,10 +32,10 @@ public function setAnalysedFiles(array $files): void
3232
public function parseFile(string $file): array
3333
{
3434
$normalizedPath = $this->fileHelper->normalizePath($file, '/');
35-
if (strpos($normalizedPath, 'vendor/jetbrains/phpstorm-stubs') !== false) {
35+
if (str_contains($normalizedPath, 'vendor/jetbrains/phpstorm-stubs')) {
3636
return $this->php8Parser->parseFile($file);
3737
}
38-
if (strpos($normalizedPath, 'vendor/phpstan/php-8-stubs/stubs') !== false) {
38+
if (str_contains($normalizedPath, 'vendor/phpstan/php-8-stubs/stubs')) {
3939
return $this->php8Parser->parseFile($file);
4040
}
4141

src/Parser/RichParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPStan\ShouldNotHappenException;
1313
use function array_filter;
1414
use function is_string;
15-
use function strpos;
15+
use function str_contains;
1616
use function substr_count;
1717
use const ARRAY_FILTER_USE_KEY;
1818
use const T_COMMENT;
@@ -103,9 +103,9 @@ private function getLinesToIgnore(array $tokens): array
103103

104104
$text = $token[1];
105105
$line = $token[2];
106-
if (strpos($text, '@phpstan-ignore-next-line') !== false) {
106+
if (str_contains($text, '@phpstan-ignore-next-line')) {
107107
$line++;
108-
} elseif (strpos($text, '@phpstan-ignore-line') === false) {
108+
} elseif (!str_contains($text, '@phpstan-ignore-line')) {
109109
continue;
110110
}
111111

0 commit comments

Comments
 (0)