Skip to content

[9.x] Replace strpos with str_contains and str_starts_with #40353

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
Jan 12, 2022
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
2 changes: 1 addition & 1 deletion src/Illuminate/Auth/Access/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ protected function callPolicyMethod($policy, $method, $user, array $arguments)
*/
protected function formatAbilityToMethod($ability)
{
return strpos($ability, '-') !== false ? Str::camel($ability) : $ability;
return str_contains($ability, '-') ? Str::camel($ability) : $ability;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public static function get($array, $key, $default = null)
return $array[$key];
}

if (strpos($key, '.') === false) {
if (! str_contains($key, '.')) {
return $array[$key] ?? value($default);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Cookie/CookieValuePrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function remove($cookieValue)
*/
public static function validate($cookieName, $cookieValue, $key)
{
$hasValidPrefix = strpos($cookieValue, static::create($cookieName, $key)) === 0;
$hasValidPrefix = str_starts_with($cookieValue, static::create($cookieName, $key));

return $hasValidPrefix ? static::remove($cookieValue) : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Console/Seeds/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function getSeeder()
{
$class = $this->input->getArgument('class') ?? $this->input->getOption('class');

if (strpos($class, '\\') === false) {
if (! str_contains($class, '\\')) {
$class = 'Database\\Seeders\\'.$class;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public function eagerLoadRelations(array $models)
// For nested eager loads we'll skip loading them here and they will be set as an
// eager load on the query to retrieve the relation so that they will be eager
// loaded on that query, because that is where they get hydrated as models.
if (strpos($name, '.') === false) {
if (! str_contains($name, '.')) {
$models = $this->eagerLoadRelation($models, $name, $constraints);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function isFillable($key)
}

return empty($this->getFillable()) &&
strpos($key, '.') === false &&
! str_contains($key, '.') &&
! Str::startsWith($key, '_');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ protected function resolveCasterClass($key)

$arguments = [];

if (is_string($castType) && strpos($castType, ':') !== false) {
if (is_string($castType) && str_contains($castType, ':')) {
$segments = explode(':', $castType, 2);

$castType = $segments[0];
Expand All @@ -1580,7 +1580,7 @@ protected function resolveCasterClass($key)
*/
protected function parseCasterClass($class)
{
return strpos($class, ':') === false
return ! str_contains($class, ':')
? $class
: explode(':', $class, 2)[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ trait QueriesRelationships
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
{
if (is_string($relation)) {
if (strpos($relation, '.') !== false) {
if (str_contains($relation, '.')) {
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ protected function migratePivotAttributes(Model $model)
// To get the pivots attributes we will just take any of the attributes which
// begin with "pivot_" and add those to this arrays, as well as unsetting
// them from the parent's models since they exist in a different table.
if (strpos($key, 'pivot_') === 0) {
if (str_starts_with($key, 'pivot_')) {
$values[substr($key, 6)] = $value;

unset($model->$key);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MySqlConnection extends Connection
*/
public function isMaria()
{
return strpos($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB') !== false;
return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/PDO/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function quote($value, $type = ParameterType::STRING)
$val = $this->connection->quote($value, $type);

// Fix for a driver version terminating all values with null byte...
if (\is_string($val) && \strpos($val, "\0") !== false) {
if (\is_string($val) && str_contains($val, "\0")) {
$val = \substr($val, 0, -1);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ protected function prependDatabaseNameIfCrossDatabaseQuery($query)
$this->getConnection()->getDatabaseName()) {
$databaseName = $query->getConnection()->getDatabaseName();

if (strpos($query->from, $databaseName) !== 0 && strpos($query->from, '.') === false) {
if (! str_starts_with($query->from, $databaseName) && ! str_contains($query->from, '.')) {
$query->from($databaseName.'.'.$query->from);
}
}
Expand Down Expand Up @@ -2203,7 +2203,7 @@ protected function stripTableForPluck($column)
return $column;
}

$separator = strpos(strtolower($column), ' as ') !== false ? ' as ' : '\.';
$separator = str_contains(strtolower($column), ' as ') ? ' as ' : '\.';

return last(preg_split('~'.$separator.'~i', $column));
}
Expand Down Expand Up @@ -2439,7 +2439,7 @@ public function numericAggregate($function, $columns = ['*'])
// If the result doesn't contain a decimal place, we will assume it is an int then
// cast it to one. When it does we will cast it to a float since it needs to be
// cast to the expected data type for the developers out of pure convenience.
return strpos((string) $result, '.') === false
return ! str_contains((string) $result, '.')
? (int) $result : (float) $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public function registerConfiguredProviders()
{
$providers = Collection::make($this->make('config')->get('app.providers'))
->partition(function ($provider) {
return strpos($provider, 'Illuminate\\') === 0;
return str_starts_with($provider, 'Illuminate\\');
});

$providers->splice(1, 0, [$this->make(PackageManifest::class)->providers()]);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function bearerToken()
if ($position !== false) {
$header = substr($header, $position + 7);

return strpos($header, ',') !== false ? strstr(',', $header, true) : $header;
return str_contains($header, ',') ? strstr(',', $header, true) : $header;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/ProcessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function escapeArgument($argument)
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
} else {
// escape trailing backslash
if ('\\' === substr($part, -1)) {
if (str_ends_with($part, '\\')) {
$part .= '\\';
}
$quote = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null)
// that is not attribute specific. If we find either we'll return it.
foreach ($keys as $key) {
foreach (array_keys($source) as $sourceKey) {
if (strpos($sourceKey, '*') !== false) {
if (str_contains($sourceKey, '*')) {
$pattern = str_replace('\*', '([^.]*)', preg_quote($sourceKey, '#'));

if (preg_match('#^'.$pattern.'\z#u', $key) === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/ValidationRuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ protected static function parseStringRule($rule)
// The format for specifying validation rules and parameters follows an
// easy {rule}:{parameters} formatting convention. For instance the
// rule "Max:3" states that the value may only be three letters.
if (strpos($rule, ':') !== false) {
if (str_contains($rule, ':')) {
[$rule, $parameter] = explode(':', $rule, 2);

$parameters = static::parseParameters($rule, $parameter);
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ public function compileString($value)
*/
protected function storeUncompiledBlocks($value)
{
if (strpos($value, '@verbatim') !== false) {
if (str_contains($value, '@verbatim')) {
$value = $this->storeVerbatimBlocks($value);
}

if (strpos($value, '@php') !== false) {
if (str_contains($value, '@php')) {
$value = $this->storePhpBlocks($value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait CompilesComponents
*/
protected function compileComponent($expression)
{
[$component, $alias, $data] = strpos($expression, ',') !== false
[$component, $alias, $data] = str_contains($expression, ',')
? array_map('trim', explode(',', trim($expression, '()'), 3)) + ['', '', '']
: [trim($expression, '()'), '', ''];

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,7 @@ public function testMacroable($collection)
// Foo() macro : unique values starting with A
$collection::macro('foo', function () {
return $this->filter(function ($item) {
return strpos($item, 'a') === 0;
return str_starts_with($item, 'a');
})
->unique()
->values();
Expand Down