Skip to content

CS modernize_strpos #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
'@Symfony' => true,
'@Symfony:risky' => true,
'fopen_flags' => false,
'modernize_strpos' => false, // requires PHP 8
'protected_to_private' => false,
])
->setRiskyAllowed(true)
Expand Down
6 changes: 3 additions & 3 deletions src/Configurator/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function write($messages, $verbosity = IOInterface::VERBOSE)

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

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

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

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

if (false === strpos($contents, $startMark) || false === strpos($contents, $endMark)) {
if (!str_contains($contents, $startMark) || !str_contains($contents, $endMark)) {
return null;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Configurator/AddLinesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function getPatchedContents(string $file, string $value, string $positio
{
$fileContents = $this->readFile($file);

if (false !== strpos($fileContents, $value)) {
if (str_contains($fileContents, $value)) {
return $fileContents; // already includes value, skip
}

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

Expand Down Expand Up @@ -214,13 +214,13 @@ private function getUnPatchedContents(string $file, $value): string
{
$fileContents = $this->readFile($file);

if (false === strpos($fileContents, $value)) {
if (!str_contains($fileContents, $value)) {
return $fileContents; // value already gone!
}

if (false !== strpos($fileContents, "\n".$value)) {
if (str_contains($fileContents, "\n".$value)) {
$value = "\n".$value;
} elseif (false !== strpos($fileContents, $value."\n")) {
} elseif (str_contains($fileContents, $value."\n")) {
$value .= "\n";
}

Expand Down Expand Up @@ -249,7 +249,7 @@ private function isPackageInstalled($packages): bool
private function relativize(string $path): string
{
$rootDir = $this->options->get('root-dir');
if (0 === strpos($path, $rootDir)) {
if (str_starts_with($path, $rootDir)) {
$path = substr($path, \strlen($rootDir) + 1);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
private function resolveTargetFolder(string $path, array $config): string
{
foreach ($config as $key => $target) {
if (0 === strpos($path, $key)) {
if (str_starts_with($path, $key)) {
return $this->options->expandTargetDir($target).substr($path, \strlen($key));
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ private function copyDir(string $source, string $target, array $files, array $op
{
$copiedFiles = [];
foreach ($files as $file => $data) {
if (0 === strpos($file, $source)) {
if (str_starts_with($file, $source)) {
$file = $this->path->concatenate([$target, substr($file, \strlen($source))]);
$copiedFiles[] = $this->copyFile($file, $data['contents'], $data['executable'], $options);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ private function removeFiles(array $manifest, array $files, string $to)

if ('/' === substr($source, -1)) {
foreach (array_keys($files) as $file) {
if (0 === strpos($file, $source)) {
if (str_starts_with($file, $source)) {
$this->removeFile($this->path->concatenate([$to, $target, substr($file, \strlen($source))]));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Configurator/DockerComposeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private function configureDockerCompose(Recipe $recipe, array $config, bool $upd
}

// Skip blank lines and comments
if (('' !== $ltrimedLine && 0 === strpos($ltrimedLine, '#')) || '' === trim($line)) {
if (('' !== $ltrimedLine && str_starts_with($ltrimedLine, '#')) || '' === trim($line)) {
continue;
}

Expand Down Expand Up @@ -349,7 +349,7 @@ private function getContentsAfterApplyingRecipe(string $rootDir, Recipe $recipe,
$updatedContents = [];
foreach ($files as $file) {
$localPath = $file;
if (0 === strpos($file, $rootDir)) {
if (str_starts_with($file, $rootDir)) {
$localPath = substr($file, \strlen($rootDir) + 1);
}
$localPath = ltrim($localPath, '/\\');
Expand Down
8 changes: 4 additions & 4 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function __construct(Composer $composer, IOInterface $io, HttpDownloader

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

if (false === $endpoint = getenv('SYMFONY_ENDPOINT')) {
// no-op
} elseif (false !== strpos($endpoint, '.json') || 'flex://defaults' === $endpoint) {
} elseif (str_contains($endpoint, '.json') || 'flex://defaults' === $endpoint) {
$this->endpoints ?? $this->endpoints = self::DEFAULT_ENDPOINTS;
array_unshift($this->endpoints, $endpoint);
$this->legacyEndpoint = null;
Expand Down Expand Up @@ -174,7 +174,7 @@ public function getRecipes(array $operations): array
if ($operation instanceof InformationOperation && $operation->getVersion()) {
$version = $operation->getVersion();
}
if (0 === strpos($version, 'dev-') && isset($package->getExtra()['branch-alias'])) {
if (str_starts_with($version, 'dev-') && isset($package->getExtra()['branch-alias'])) {
$branchAliases = $package->getExtra()['branch-alias'];
if (
(isset($branchAliases[$version]) && $alias = $branchAliases[$version])
Expand Down
2 changes: 1 addition & 1 deletion src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public function install(Event $event)
$runtime = $this->options->get('runtime');
$dotenvPath = $rootDir.'/'.($runtime['dotenv_path'] ?? '.env');

if (!file_exists($dotenvPath) && !file_exists($dotenvPath.'.local') && file_exists($dotenvPath.'.dist') && false === strpos(file_get_contents($dotenvPath.'.dist'), '.env.local')) {
if (!file_exists($dotenvPath) && !file_exists($dotenvPath.'.local') && file_exists($dotenvPath.'.dist') && !str_contains(file_get_contents($dotenvPath.'.dist'), '.env.local')) {
copy($dotenvPath.'.dist', $dotenvPath);
}

Expand Down
4 changes: 2 additions & 2 deletions src/GithubApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function getPullRequestForCommit(string $commit, string $repo): ?array
$bestItem = null;
foreach ($data['items'] as $item) {
// make sure the PR referenced isn't from a different repository
if (false === strpos($item['html_url'], \sprintf('%s/pull', $repositoryName))) {
if (!str_contains($item['html_url'], \sprintf('%s/pull', $repositoryName))) {
continue;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ private function requestGitHubApi(string $path)
private function getRepositoryName(string $repo): ?string
{
// only supports public repository placement
if (0 !== strpos($repo, 'github.com')) {
if (!str_starts_with($repo, 'github.com')) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions src/PackageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function parseVersion(string $package, string $version, bool $isRequire):
{
$guess = 'guess' === ($version ?: 'guess');

if (0 !== strpos($package, 'symfony/')) {
if (!str_starts_with($package, 'symfony/')) {
return $guess ? '' : ':'.$version;
}

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

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)) {
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)) {
return $argument;
}

Expand Down Expand Up @@ -140,7 +140,7 @@ private function throwAlternatives(string $argument, int $position)
$alternatives = [];
foreach ($this->downloader->getAliases() as $alias => $package) {
$lev = levenshtein($argument, $alias);
if ($lev <= \strlen($argument) / 3 || ('' !== $argument && false !== strpos($alias, $argument))) {
if ($lev <= \strlen($argument) / 3 || ('' !== $argument && str_contains($alias, $argument))) {
$alternatives[$package][] = $alias;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SymfonyBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function isBundleClass(string $class, string $path, bool $isPsr4): bool
// heuristic that should work in almost all cases
$classContents = file_get_contents($classPath);

return (false !== strpos($classContents, 'Symfony\Component\HttpKernel\Bundle\Bundle'))
|| (false !== strpos($classContents, 'Symfony\Component\HttpKernel\Bundle\AbstractBundle'));
return str_contains($classContents, 'Symfony\Component\HttpKernel\Bundle\Bundle')
|| str_contains($classContents, 'Symfony\Component\HttpKernel\Bundle\AbstractBundle');
}
}
2 changes: 1 addition & 1 deletion src/Update/RecipePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private function _applyPatchFile(RecipePatch $patch)
return true;
}

if (false !== strpos($this->processExecutor->getErrorOutput(), 'with conflicts')) {
if (str_contains($this->processExecutor->getErrorOutput(), 'with conflicts')) {
// successful with conflicts
return false;
}
Expand Down