From a1343fae048da12ea6bd4e8bd7f67ff8b6ad0469 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Fri, 3 Dec 2021 00:55:19 +0100 Subject: [PATCH] Modernize strpos() calls --- .../API/PostgreSQL/ExceptionConverter.php | 6 ++-- src/Driver/API/SQLite/ExceptionConverter.php | 32 +++++++++---------- src/Driver/IBMDB2/DataSourceName.php | 4 +-- src/DriverManager.php | 4 +-- src/Platforms/AbstractPlatform.php | 4 +-- src/Platforms/DB2Platform.php | 4 +-- src/Platforms/OraclePlatform.php | 4 +-- src/Platforms/PostgreSQLPlatform.php | 6 ++-- src/Platforms/SQLServerPlatform.php | 10 +++--- src/Schema/AbstractAsset.php | 4 +-- src/Schema/MySQLSchemaManager.php | 12 +++---- src/Schema/OracleSchemaManager.php | 7 ++-- src/Schema/SQLServerSchemaManager.php | 4 +-- src/Schema/Schema.php | 4 +-- src/Schema/SqliteSchemaManager.php | 7 ++-- tests/TestUtil.php | 4 +-- 16 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/Driver/API/PostgreSQL/ExceptionConverter.php b/src/Driver/API/PostgreSQL/ExceptionConverter.php index df34802cfb7..0dd44119fe8 100644 --- a/src/Driver/API/PostgreSQL/ExceptionConverter.php +++ b/src/Driver/API/PostgreSQL/ExceptionConverter.php @@ -21,7 +21,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\DBAL\Query; -use function strpos; +use function str_contains; /** * @internal @@ -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); } @@ -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); } diff --git a/src/Driver/API/SQLite/ExceptionConverter.php b/src/Driver/API/SQLite/ExceptionConverter.php index e19b3ab8cef..ec5163b4cf8 100644 --- a/src/Driver/API/SQLite/ExceptionConverter.php +++ b/src/Driver/API/SQLite/ExceptionConverter.php @@ -20,7 +20,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Doctrine\DBAL\Query; -use function strpos; +use function str_contains; /** * @internal @@ -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); } diff --git a/src/Driver/IBMDB2/DataSourceName.php b/src/Driver/IBMDB2/DataSourceName.php index 6379efcfebf..069e8497cb8 100644 --- a/src/Driver/IBMDB2/DataSourceName.php +++ b/src/Driver/IBMDB2/DataSourceName.php @@ -6,7 +6,7 @@ use function implode; use function sprintf; -use function strpos; +use function str_contains; /** * IBM DB2 DSN @@ -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']); } diff --git a/src/DriverManager.php b/src/DriverManager.php index afd50041819..e8fb5b9e2ed 100644 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -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; /** @@ -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); } diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index 9c86ebc60dc..91b2cd1458e 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -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; @@ -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); diff --git a/src/Platforms/DB2Platform.php b/src/Platforms/DB2Platform.php index 5ed699af071..8a3e28c85c9 100644 --- a/src/Platforms/DB2Platform.php +++ b/src/Platforms/DB2Platform.php @@ -19,7 +19,7 @@ use function explode; use function implode; use function sprintf; -use function strpos; +use function str_contains; class DB2Platform extends AbstractPlatform { @@ -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; } diff --git a/src/Platforms/OraclePlatform.php b/src/Platforms/OraclePlatform.php index 15ac00b7100..8eaa116d545 100644 --- a/src/Platforms/OraclePlatform.php +++ b/src/Platforms/OraclePlatform.php @@ -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; @@ -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; } diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 5565779beb2..c9a6570e7c2 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -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; @@ -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 { @@ -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; } diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index a0affa93740..8dc347fb450 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/Schema/AbstractAsset.php b/src/Schema/AbstractAsset.php index dca2d597a2e..5cbfa4746b7 100644 --- a/src/Schema/AbstractAsset.php +++ b/src/Schema/AbstractAsset.php @@ -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; @@ -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]; diff --git a/src/Schema/MySQLSchemaManager.php b/src/Schema/MySQLSchemaManager.php index 6112eac0a39..46dcd4bec1d 100644 --- a/src/Schema/MySQLSchemaManager.php +++ b/src/Schema/MySQLSchemaManager.php @@ -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; @@ -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; } @@ -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'])) { diff --git a/src/Schema/OracleSchemaManager.php b/src/Schema/OracleSchemaManager.php index 2ddbc3266cc..f86b0f4cbb4 100644 --- a/src/Schema/OracleSchemaManager.php +++ b/src/Schema/OracleSchemaManager.php @@ -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; @@ -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'; diff --git a/src/Schema/SQLServerSchemaManager.php b/src/Schema/SQLServerSchemaManager.php index 53a250e67e5..0cc29fe1a40 100644 --- a/src/Schema/SQLServerSchemaManager.php +++ b/src/Schema/SQLServerSchemaManager.php @@ -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; /** @@ -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 []; } diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 80be94ab9aa..5a3917faad8 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -16,7 +16,7 @@ use Doctrine\DBAL\Schema\Visitor\Visitor; use function array_values; -use function strpos; +use function str_contains; use function strtolower; /** @@ -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; } diff --git a/src/Schema/SqliteSchemaManager.php b/src/Schema/SqliteSchemaManager.php index 7cb42b542c0..efb53b2509d 100644 --- a/src/Schema/SqliteSchemaManager.php +++ b/src/Schema/SqliteSchemaManager.php @@ -24,8 +24,9 @@ use function preg_quote; use function preg_replace; use function rtrim; +use function str_contains; use function str_replace; -use function strpos; +use function str_starts_with; use function strtolower; use function trim; use function usort; @@ -190,7 +191,7 @@ static function (array $a, array $b): int { // fetch regular indexes foreach ($tableIndexes as $tableIndex) { // Ignore indexes with reserved names, e.g. autoindexes - if (strpos($tableIndex['name'], 'sqlite_') === 0) { + if (str_starts_with($tableIndex['name'], 'sqlite_')) { continue; } @@ -292,7 +293,7 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column $length = (int) $matches[4]; } - if (strpos($dbType, ' unsigned') !== false) { + if (str_contains($dbType, ' unsigned')) { $dbType = str_replace(' unsigned', '', $dbType); $unsigned = true; } diff --git a/tests/TestUtil.php b/tests/TestUtil.php index de5bd141457..0bf3be6aa47 100644 --- a/tests/TestUtil.php +++ b/tests/TestUtil.php @@ -23,8 +23,8 @@ use function implode; use function in_array; use function is_string; +use function str_starts_with; use function strlen; -use function strpos; use function substr; use function unlink; @@ -232,7 +232,7 @@ private static function mapConnectionParameters(array $configuration, string $pr } foreach ($configuration as $param => $value) { - if (strpos($param, $prefix . 'driver_option_') !== 0) { + if (! str_starts_with($param, $prefix . 'driver_option_')) { continue; }