Skip to content

Commit dcd0c73

Browse files
committed
minor #1053 CS modernize_strpos (rosier)
This PR was merged into the 2.x branch. Discussion ---------- CS modernize_strpos Add CS rule modernize_strpos Commits ------- 1cacd32 CS modernize_strpos
2 parents 5d743b3 + 1cacd32 commit dcd0c73

11 files changed

+27
-28
lines changed

.php-cs-fixer.dist.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'@Symfony' => true,
99
'@Symfony:risky' => true,
1010
'fopen_flags' => false,
11-
'modernize_strpos' => false, // requires PHP 8
1211
'protected_to_private' => false,
1312
])
1413
->setRiskyAllowed(true)

src/Configurator/AbstractConfigurator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function write($messages, $verbosity = IOInterface::VERBOSE)
5656

5757
protected function isFileMarked(Recipe $recipe, string $file): bool
5858
{
59-
return is_file($file) && false !== strpos(file_get_contents($file), \sprintf('###> %s ###', $recipe->getName()));
59+
return is_file($file) && str_contains(file_get_contents($file), \sprintf('###> %s ###', $recipe->getName()));
6060
}
6161

6262
protected function markData(Recipe $recipe, string $data): string
@@ -66,7 +66,7 @@ protected function markData(Recipe $recipe, string $data): string
6666

6767
protected function isFileXmlMarked(Recipe $recipe, string $file): bool
6868
{
69-
return is_file($file) && false !== strpos(file_get_contents($file), \sprintf('###+ %s ###', $recipe->getName()));
69+
return is_file($file) && str_contains(file_get_contents($file), \sprintf('###+ %s ###', $recipe->getName()));
7070
}
7171

7272
protected function markXmlData(Recipe $recipe, string $data): string
@@ -104,7 +104,7 @@ protected function updateDataString(string $contents, string $data): ?string
104104
$startMark = trim(reset($pieces));
105105
$endMark = trim(end($pieces));
106106

107-
if (false === strpos($contents, $startMark) || false === strpos($contents, $endMark)) {
107+
if (!str_contains($contents, $startMark) || !str_contains($contents, $endMark)) {
108108
return null;
109109
}
110110

src/Configurator/AddLinesConfigurator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private function getPatchedContents(string $file, string $value, string $positio
168168
{
169169
$fileContents = $this->readFile($file);
170170

171-
if (false !== strpos($fileContents, $value)) {
171+
if (str_contains($fileContents, $value)) {
172172
return $fileContents; // already includes value, skip
173173
}
174174

@@ -185,7 +185,7 @@ private function getPatchedContents(string $file, string $value, string $positio
185185
$lines = explode("\n", $fileContents);
186186
$targetFound = false;
187187
foreach ($lines as $key => $line) {
188-
if (false !== strpos($line, $target)) {
188+
if (str_contains($line, $target)) {
189189
array_splice($lines, $key + 1, 0, $value);
190190
$targetFound = true;
191191

@@ -214,13 +214,13 @@ private function getUnPatchedContents(string $file, $value): string
214214
{
215215
$fileContents = $this->readFile($file);
216216

217-
if (false === strpos($fileContents, $value)) {
217+
if (!str_contains($fileContents, $value)) {
218218
return $fileContents; // value already gone!
219219
}
220220

221-
if (false !== strpos($fileContents, "\n".$value)) {
221+
if (str_contains($fileContents, "\n".$value)) {
222222
$value = "\n".$value;
223-
} elseif (false !== strpos($fileContents, $value."\n")) {
223+
} elseif (str_contains($fileContents, $value."\n")) {
224224
$value .= "\n";
225225
}
226226

@@ -249,7 +249,7 @@ private function isPackageInstalled($packages): bool
249249
private function relativize(string $path): string
250250
{
251251
$rootDir = $this->options->get('root-dir');
252-
if (0 === strpos($path, $rootDir)) {
252+
if (str_starts_with($path, $rootDir)) {
253253
$path = substr($path, \strlen($rootDir) + 1);
254254
}
255255

src/Configurator/CopyFromRecipeConfigurator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
5858
private function resolveTargetFolder(string $path, array $config): string
5959
{
6060
foreach ($config as $key => $target) {
61-
if (0 === strpos($path, $key)) {
61+
if (str_starts_with($path, $key)) {
6262
return $this->options->expandTargetDir($target).substr($path, \strlen($key));
6363
}
6464
}
@@ -116,7 +116,7 @@ private function copyDir(string $source, string $target, array $files, array $op
116116
{
117117
$copiedFiles = [];
118118
foreach ($files as $file => $data) {
119-
if (0 === strpos($file, $source)) {
119+
if (str_starts_with($file, $source)) {
120120
$file = $this->path->concatenate([$target, substr($file, \strlen($source))]);
121121
$copiedFiles[] = $this->copyFile($file, $data['contents'], $data['executable'], $options);
122122
}
@@ -160,7 +160,7 @@ private function removeFiles(array $manifest, array $files, string $to)
160160

161161
if ('/' === substr($source, -1)) {
162162
foreach (array_keys($files) as $file) {
163-
if (0 === strpos($file, $source)) {
163+
if (str_starts_with($file, $source)) {
164164
$this->removeFile($this->path->concatenate([$to, $target, substr($file, \strlen($source))]));
165165
}
166166
}

src/Configurator/DockerComposeConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private function configureDockerCompose(Recipe $recipe, array $config, bool $upd
260260
}
261261

262262
// Skip blank lines and comments
263-
if (('' !== $ltrimedLine && 0 === strpos($ltrimedLine, '#')) || '' === trim($line)) {
263+
if (('' !== $ltrimedLine && str_starts_with($ltrimedLine, '#')) || '' === trim($line)) {
264264
continue;
265265
}
266266

@@ -349,7 +349,7 @@ private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe,
349349
$updatedContents = [];
350350
foreach ($files as $file) {
351351
$localPath = $file;
352-
if (0 === strpos($file, $rootDir)) {
352+
if (str_starts_with($file, $rootDir)) {
353353
$localPath = substr($file, \strlen($rootDir) + 1);
354354
}
355355
$localPath = ltrim($localPath, '/\\');

src/Downloader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public function __construct(Composer $composer, IOInterface $io, HttpDownloader
6060

6161
if (null === $endpoint = $composer->getPackage()->getExtra()['symfony']['endpoint'] ?? null) {
6262
$this->endpoints = self::DEFAULT_ENDPOINTS;
63-
} elseif (\is_array($endpoint) || false !== strpos($endpoint, '.json') || 'flex://defaults' === $endpoint) {
63+
} elseif (\is_array($endpoint) || str_contains($endpoint, '.json') || 'flex://defaults' === $endpoint) {
6464
$this->endpoints = array_values((array) $endpoint);
65-
if (\is_string($endpoint) && false !== strpos($endpoint, '.json')) {
65+
if (\is_string($endpoint) && str_contains($endpoint, '.json')) {
6666
$this->endpoints[] = 'flex://defaults';
6767
}
6868
} else {
@@ -71,7 +71,7 @@ public function __construct(Composer $composer, IOInterface $io, HttpDownloader
7171

7272
if (false === $endpoint = getenv('SYMFONY_ENDPOINT')) {
7373
// no-op
74-
} elseif (false !== strpos($endpoint, '.json') || 'flex://defaults' === $endpoint) {
74+
} elseif (str_contains($endpoint, '.json') || 'flex://defaults' === $endpoint) {
7575
$this->endpoints ?? $this->endpoints = self::DEFAULT_ENDPOINTS;
7676
array_unshift($this->endpoints, $endpoint);
7777
$this->legacyEndpoint = null;
@@ -174,7 +174,7 @@ public function getRecipes(array $operations): array
174174
if ($operation instanceof InformationOperation && $operation->getVersion()) {
175175
$version = $operation->getVersion();
176176
}
177-
if (0 === strpos($version, 'dev-') && isset($package->getExtra()['branch-alias'])) {
177+
if (str_starts_with($version, 'dev-') && isset($package->getExtra()['branch-alias'])) {
178178
$branchAliases = $package->getExtra()['branch-alias'];
179179
if (
180180
(isset($branchAliases[$version]) && $alias = $branchAliases[$version])

src/Flex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function install(Event $event)
351351
$runtime = $this->options->get('runtime');
352352
$dotenvPath = $rootDir.'/'.($runtime['dotenv_path'] ?? '.env');
353353

354-
if (!file_exists($dotenvPath) && !file_exists($dotenvPath.'.local') && file_exists($dotenvPath.'.dist') && false === strpos(file_get_contents($dotenvPath.'.dist'), '.env.local')) {
354+
if (!file_exists($dotenvPath) && !file_exists($dotenvPath.'.local') && file_exists($dotenvPath.'.dist') && !str_contains(file_get_contents($dotenvPath.'.dist'), '.env.local')) {
355355
copy($dotenvPath.'.dist', $dotenvPath);
356356
}
357357

src/GithubApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function getPullRequestForCommit(string $commit, string $repo): ?array
142142
$bestItem = null;
143143
foreach ($data['items'] as $item) {
144144
// make sure the PR referenced isn't from a different repository
145-
if (false === strpos($item['html_url'], \sprintf('%s/pull', $repositoryName))) {
145+
if (!str_contains($item['html_url'], \sprintf('%s/pull', $repositoryName))) {
146146
continue;
147147
}
148148

@@ -186,7 +186,7 @@ private function requestGitHubApi(string $path)
186186
private function getRepositoryName(string $repo): ?string
187187
{
188188
// only supports public repository placement
189-
if (0 !== strpos($repo, 'github.com')) {
189+
if (!str_starts_with($repo, 'github.com')) {
190190
return null;
191191
}
192192

src/PackageResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function parseVersion(string $package, string $version, bool $isRequire):
7070
{
7171
$guess = 'guess' === ($version ?: 'guess');
7272

73-
if (0 !== strpos($package, 'symfony/')) {
73+
if (!str_starts_with($package, 'symfony/')) {
7474
return $guess ? '' : ':'.$version;
7575
}
7676

@@ -108,7 +108,7 @@ private function resolvePackageName(string $argument, int $position, bool $isReq
108108
$skippedPackages[] = 'lock';
109109
}
110110

111-
if (false !== strpos($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, $skippedPackages)) {
111+
if (str_contains($argument, '/') || preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $argument) || preg_match('{(?<=[a-z0-9_/-])\*|\*(?=[a-z0-9_/-])}i', $argument) || \in_array($argument, $skippedPackages)) {
112112
return $argument;
113113
}
114114

@@ -140,7 +140,7 @@ private function throwAlternatives(string $argument, int $position)
140140
$alternatives = [];
141141
foreach ($this->downloader->getAliases() as $alias => $package) {
142142
$lev = levenshtein($argument, $alias);
143-
if ($lev <= \strlen($argument) / 3 || ('' !== $argument && false !== strpos($alias, $argument))) {
143+
if ($lev <= \strlen($argument) / 3 || ('' !== $argument && str_contains($alias, $argument))) {
144144
$alternatives[$package][] = $alias;
145145
}
146146
}

src/SymfonyBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function isBundleClass(string $class, string $path, bool $isPsr4): bool
109109
// heuristic that should work in almost all cases
110110
$classContents = file_get_contents($classPath);
111111

112-
return (false !== strpos($classContents, 'Symfony\Component\HttpKernel\Bundle\Bundle'))
113-
|| (false !== strpos($classContents, 'Symfony\Component\HttpKernel\Bundle\AbstractBundle'));
112+
return str_contains($classContents, 'Symfony\Component\HttpKernel\Bundle\Bundle')
113+
|| str_contains($classContents, 'Symfony\Component\HttpKernel\Bundle\AbstractBundle');
114114
}
115115
}

src/Update/RecipePatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private function _applyPatchFile(RecipePatch $patch)
233233
return true;
234234
}
235235

236-
if (false !== strpos($this->processExecutor->getErrorOutput(), 'with conflicts')) {
236+
if (str_contains($this->processExecutor->getErrorOutput(), 'with conflicts')) {
237237
// successful with conflicts
238238
return false;
239239
}

0 commit comments

Comments
 (0)