Skip to content

Commit

Permalink
Merge pull request #5080 from derrabus/improvement/modernize-strpos
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Dec 3, 2021
2 parents 00af17a + a1343fa commit 847025d
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/Driver/API/PostgreSQL/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Query;

use function strpos;
use function str_contains;

/**
* @internal
Expand All @@ -41,7 +41,7 @@ public function convert(Exception $exception, ?Query $query): DriverException
case '0A000':
// Foreign key constraint violations during a TRUNCATE operation
// are considered "feature not supported" in PostgreSQL.
if (strpos($exception->getMessage(), 'truncate') !== false) {
if (str_contains($exception->getMessage(), 'truncate')) {
return new ForeignKeyConstraintViolationException($exception, $query);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public function convert(Exception $exception, ?Query $query): DriverException
// Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.3.22 and PHP 7.4.10),
// in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code.
// We have to match against the SQLSTATE in the error message in these cases.
if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) {
if ($exception->getCode() === 7 && str_contains($exception->getMessage(), 'SQLSTATE[08006]')) {
return new ConnectionException($exception, $query);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Driver/API/SQLite/ExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Query;

use function strpos;
use function str_contains;

/**
* @internal
Expand All @@ -32,55 +32,55 @@ final class ExceptionConverter implements ExceptionConverterInterface
*/
public function convert(Exception $exception, ?Query $query): DriverException
{
if (strpos($exception->getMessage(), 'database is locked') !== false) {
if (str_contains($exception->getMessage(), 'database is locked')) {
return new LockWaitTimeoutException($exception, $query);
}

if (
strpos($exception->getMessage(), 'must be unique') !== false ||
strpos($exception->getMessage(), 'is not unique') !== false ||
strpos($exception->getMessage(), 'are not unique') !== false ||
strpos($exception->getMessage(), 'UNIQUE constraint failed') !== false
str_contains($exception->getMessage(), 'must be unique') ||
str_contains($exception->getMessage(), 'is not unique') ||
str_contains($exception->getMessage(), 'are not unique') ||
str_contains($exception->getMessage(), 'UNIQUE constraint failed')
) {
return new UniqueConstraintViolationException($exception, $query);
}

if (
strpos($exception->getMessage(), 'may not be NULL') !== false ||
strpos($exception->getMessage(), 'NOT NULL constraint failed') !== false
str_contains($exception->getMessage(), 'may not be NULL') ||
str_contains($exception->getMessage(), 'NOT NULL constraint failed')
) {
return new NotNullConstraintViolationException($exception, $query);
}

if (strpos($exception->getMessage(), 'no such table:') !== false) {
if (str_contains($exception->getMessage(), 'no such table:')) {
return new TableNotFoundException($exception, $query);
}

if (strpos($exception->getMessage(), 'already exists') !== false) {
if (str_contains($exception->getMessage(), 'already exists')) {
return new TableExistsException($exception, $query);
}

if (strpos($exception->getMessage(), 'has no column named') !== false) {
if (str_contains($exception->getMessage(), 'has no column named')) {
return new InvalidFieldNameException($exception, $query);
}

if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
if (str_contains($exception->getMessage(), 'ambiguous column name')) {
return new NonUniqueFieldNameException($exception, $query);
}

if (strpos($exception->getMessage(), 'syntax error') !== false) {
if (str_contains($exception->getMessage(), 'syntax error')) {
return new SyntaxErrorException($exception, $query);
}

if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
if (str_contains($exception->getMessage(), 'attempt to write a readonly database')) {
return new ReadOnlyException($exception, $query);
}

if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
if (str_contains($exception->getMessage(), 'unable to open database file')) {
return new ConnectionException($exception, $query);
}

if (strpos($exception->getMessage(), 'FOREIGN KEY constraint failed') !== false) {
if (str_contains($exception->getMessage(), 'FOREIGN KEY constraint failed')) {
return new ForeignKeyConstraintViolationException($exception, $query);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Driver/IBMDB2/DataSourceName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use function implode;
use function sprintf;
use function strpos;
use function str_contains;

/**
* IBM DB2 DSN
Expand Down Expand Up @@ -48,7 +48,7 @@ public static function fromArray(array $params): self
*/
public static function fromConnectionParameters(array $params): self
{
if (isset($params['dbname']) && strpos($params['dbname'], '=') !== false) {
if (isset($params['dbname']) && str_contains($params['dbname'], '=')) {
return new self($params['dbname']);
}

Expand Down
4 changes: 2 additions & 2 deletions src/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use function parse_url;
use function preg_replace;
use function rawurldecode;
use function str_contains;
use function str_replace;
use function strpos;
use function substr;

/**
Expand Down Expand Up @@ -369,7 +369,7 @@ private static function parseDatabaseUrlPath(array $url, array $params): array
return self::parseRegularDatabaseUrlPath($url, $params);
}

if (strpos($params['driver'], 'sqlite') !== false) {
if (str_contains($params['driver'], 'sqlite')) {
return self::parseSqliteDatabaseUrlPath($url, $params);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
use function preg_quote;
use function preg_replace;
use function sprintf;
use function str_contains;
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function strtoupper;

Expand Down Expand Up @@ -1235,7 +1235,7 @@ public function getDropSchemaSQL(string $schemaName): string
*/
public function quoteIdentifier(string $identifier): string
{
if (strpos($identifier, '.') !== false) {
if (str_contains($identifier, '.')) {
$parts = array_map([$this, 'quoteSingleIdentifier'], explode('.', $identifier));

return implode('.', $parts);
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use function explode;
use function implode;
use function sprintf;
use function strpos;
use function str_contains;

class DB2Platform extends AbstractPlatform
{
Expand Down Expand Up @@ -605,7 +605,7 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
*/
protected function getRenameIndexSQL(string $oldIndexName, Index $index, string $tableName): array
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schema] = explode('.', $tableName);
$oldIndexName = $schema . '.' . $oldIndexName;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
use function implode;
use function preg_match;
use function sprintf;
use function str_contains;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;

Expand Down Expand Up @@ -822,7 +822,7 @@ public function getColumnDeclarationSQL(string $name, array $column): string
*/
protected function getRenameIndexSQL(string $oldIndexName, Index $index, string $tableName): array
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schema] = explode('.', $tableName);
$oldIndexName = $schema . '.' . $oldIndexName;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use function is_numeric;
use function is_string;
use function sprintf;
use function strpos;
use function str_contains;
use function strtolower;
use function trim;

Expand Down Expand Up @@ -257,7 +257,7 @@ public function getListTableIndexesSQL(string $table, ?string $database = null):
private function getTableWhereClause(string $table, string $classAlias = 'c', string $namespaceAlias = 'n'): string
{
$whereClause = $namespaceAlias . ".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND ";
if (strpos($table, '.') !== false) {
if (str_contains($table, '.')) {
[$schema, $table] = explode('.', $table);
$schema = $this->quoteStringLiteral($schema);
} else {
Expand Down Expand Up @@ -523,7 +523,7 @@ private function isUnchangedBinaryColumn(ColumnDiff $columnDiff): bool
*/
protected function getRenameIndexSQL(string $oldIndexName, Index $index, string $tableName): array
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schema] = explode('.', $tableName);
$oldIndexName = $schema . '.' . $oldIndexName;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
use function preg_match;
use function preg_match_all;
use function sprintf;
use function str_contains;
use function str_replace;
use function strpos;
use function strtoupper;
use function substr_count;

Expand Down Expand Up @@ -283,7 +283,7 @@ public function getCreatePrimaryKeySQL(Index $index, string $table): string
*/
protected function getCreateColumnCommentSQL(string $tableName, string $columnName, string $comment): string
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
Expand Down Expand Up @@ -598,7 +598,7 @@ private function alterColumnRequiresDropDefaultConstraint(ColumnDiff $columnDiff
*/
protected function getAlterColumnCommentSQL(string $tableName, string $columnName, string $comment): string
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
Expand Down Expand Up @@ -635,7 +635,7 @@ protected function getAlterColumnCommentSQL(string $tableName, string $columnNam
*/
protected function getDropColumnCommentSQL(string $tableName, string $columnName): string
{
if (strpos($tableName, '.') !== false) {
if (str_contains($tableName, '.')) {
[$schemaSQL, $tableSQL] = explode('.', $tableName);
$schemaSQL = $this->quoteStringLiteral($schemaSQL);
$tableSQL = $this->quoteStringLiteral($tableSQL);
Expand Down Expand Up @@ -859,7 +859,7 @@ public function getListViewsSQL(string $database): string
*/
private function getTableWhereClause(string $table, string $schemaColumn, string $tableColumn): string
{
if (strpos($table, '.') !== false) {
if (str_contains($table, '.')) {
[$schema, $table] = explode('.', $table);
$schema = $this->quoteStringLiteral($schema);
$table = $this->quoteStringLiteral($table);
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/AbstractAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use function dechex;
use function explode;
use function implode;
use function str_contains;
use function str_replace;
use function strpos;
use function strtolower;
use function strtoupper;
use function substr;
Expand Down Expand Up @@ -44,7 +44,7 @@ protected function _setName(string $name): void
$name = $this->trimQuotes($name);
}

if (strpos($name, '.') !== false) {
if (str_contains($name, '.')) {
$parts = explode('.', $name);
$this->_namespace = $parts[0];
$name = $parts[1];
Expand Down
12 changes: 6 additions & 6 deletions src/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use function explode;
use function is_string;
use function preg_match;
use function strpos;
use function str_contains;
use function strtok;
use function strtolower;
use function strtr;
Expand Down Expand Up @@ -81,14 +81,14 @@ protected function _getPortableTableIndexesList(array $tableIndexes, string $tab
$v['primary'] = false;
}

if (strpos($v['index_type'], 'FULLTEXT') !== false) {
if (str_contains($v['index_type'], 'FULLTEXT')) {
$v['flags'] = ['FULLTEXT'];
} elseif (strpos($v['index_type'], 'SPATIAL') !== false) {
} elseif (str_contains($v['index_type'], 'SPATIAL')) {
$v['flags'] = ['SPATIAL'];
}

// Ignore prohibited prefix `length` for spatial index
if (strpos($v['index_type'], 'SPATIAL') === false) {
if (! str_contains($v['index_type'], 'SPATIAL')) {
$v['length'] = isset($v['sub_part']) ? (int) $v['sub_part'] : null;
}

Expand Down Expand Up @@ -199,13 +199,13 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column

$options = [
'length' => $length !== null ? (int) $length : null,
'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false,
'unsigned' => str_contains($tableColumn['type'], 'unsigned'),
'fixed' => $fixed,
'default' => $columnDefault,
'notnull' => $tableColumn['null'] !== 'YES',
'scale' => $scale,
'precision' => $precision,
'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false,
'autoincrement' => str_contains($tableColumn['extra'], 'auto_increment'),
];

if (isset($tableColumn['comment'])) {
Expand Down
7 changes: 4 additions & 3 deletions src/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use function array_values;
use function is_string;
use function preg_match;
use function str_contains;
use function str_replace;
use function strpos;
use function str_starts_with;
use function strtolower;
use function trim;

Expand Down Expand Up @@ -98,8 +99,8 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

$dbType = strtolower($tableColumn['data_type']);
if (strpos($dbType, 'timestamp(') === 0) {
if (strpos($dbType, 'with time zone') !== false) {
if (str_starts_with($dbType, 'timestamp(')) {
if (str_contains($dbType, 'with time zone')) {
$dbType = 'timestamptz';
} else {
$dbType = 'timestamp';
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use function preg_match;
use function sprintf;
use function str_replace;
use function strpos;
use function str_starts_with;
use function strtok;

/**
Expand Down Expand Up @@ -255,7 +255,7 @@ public function listTableIndexes(string $table): array
try {
$tableIndexes = $this->_conn->fetchAllAssociative($sql);
} catch (Exception $e) {
if (strpos($e->getMessage(), 'SQLSTATE [01000, 15472]') === 0) {
if (str_starts_with($e->getMessage(), 'SQLSTATE [01000, 15472]')) {
return [];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\DBAL\Schema\Visitor\Visitor;

use function array_values;
use function strpos;
use function str_contains;
use function strtolower;

/**
Expand Down Expand Up @@ -182,7 +182,7 @@ private function getFullQualifiedAssetName(string $name): string
{
$name = $this->getUnquotedAssetName($name);

if (strpos($name, '.') === false) {
if (! str_contains($name, '.')) {
$name = $this->getName() . '.' . $name;
}

Expand Down
Loading

0 comments on commit 847025d

Please sign in to comment.