From ce518870f868f4db0f32fc8b98a88c853ab7bf53 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 12 May 2022 11:17:32 +0800 Subject: [PATCH 1/2] WIP: PHPSTAN --- .../{tests.yml => continuous-integration.yml} | 8 +- .github/workflows/static-analysis.yml | 54 ++++++ composer.json | 1 + phpstan.neon.dist | 17 ++ src/Oci8/Oci8Connection.php | 99 ++++++----- src/Oci8/Schema/Grammars/OracleGrammar.php | 160 +++++++++--------- src/Oci8/Schema/OracleBlueprint.php | 17 +- src/Oci8/Schema/OracleBuilder.php | 34 ++-- src/Oci8/Schema/Sequence.php | 59 ++++--- 9 files changed, 270 insertions(+), 179 deletions(-) rename .github/workflows/{tests.yml => continuous-integration.yml} (94%) create mode 100644 .github/workflows/static-analysis.yml create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/tests.yml b/.github/workflows/continuous-integration.yml similarity index 94% rename from .github/workflows/tests.yml rename to .github/workflows/continuous-integration.yml index 273f90f1..f4d81a68 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/continuous-integration.yml @@ -1,4 +1,4 @@ -name: tests +name: "Continuous Integration" on: push: @@ -7,7 +7,7 @@ on: - cron: '0 0 * * *' jobs: - tests: + phpunit: runs-on: ubuntu-latest services: @@ -19,8 +19,8 @@ jobs: strategy: fail-fast: true matrix: - php: [8.0, 8.1] - stability: [prefer-stable] + php: [ 8.0, 8.1 ] + stability: [ prefer-stable ] name: PHP ${{ matrix.php }} - STABILITY ${{ matrix.stability }} diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 00000000..7f1ebd92 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,54 @@ +name: "Static Analysis" + +on: + push: + paths: + - .github/workflows/static-analysis.yml + - composer.* + - phpstan.neon.dist + - src/** + - tests/** + + pull_request: + paths: + - .github/workflows/static-analysis.yml + - composer.* + - phpstan.neon.dist + - src/** + - tests/** + + schedule: + - cron: '0 0 * * *' + +jobs: + static-analysis-phpstan: + + name: "Static Analysis with PHPStan" + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [8.1] + stability: [prefer-stable] + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 5 + max_attempts: 5 + command: COMPOSER_ROOT_VERSION=dev-master composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress + + - name: "Run a static analysis with phpstan/phpstan" + run: "vendor/bin/phpstan --error-format=checkstyle" diff --git a/composer.json b/composer.json index 85b4f12c..c5a6a72c 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "require-dev": { "doctrine/dbal": "^3.3", "mockery/mockery": "^1.4.4", + "nunomaduro/larastan": "^2.1", "orchestra/testbench": "^7.0", "phpunit/phpunit": "^9.5.8" }, diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..b37df3ec --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,17 @@ +includes: + - ./vendor/nunomaduro/larastan/extension.neon + +parameters: + + paths: + - src + + level: max + + ignoreErrors: + - '#Unsafe usage of new static\(\).#' + + excludePaths: + - src/helper.php + + checkMissingIterableValueType: false diff --git a/src/Oci8/Oci8Connection.php b/src/Oci8/Oci8Connection.php index ec31f4cd..f3215a73 100644 --- a/src/Oci8/Oci8Connection.php +++ b/src/Oci8/Oci8Connection.php @@ -4,7 +4,6 @@ use Doctrine\DBAL\Driver\OCI8\Driver as DoctrineDriver; use Doctrine\DBAL\Version; -use Exception; use Illuminate\Database\Connection; use Illuminate\Database\Grammar; use Illuminate\Support\Str; @@ -12,11 +11,11 @@ use PDOStatement; use Throwable; use Yajra\Oci8\PDO\Oci8Driver; -use Yajra\Oci8\Query\Grammars\OracleGrammar as QueryGrammar; -use Yajra\Oci8\Query\OracleBuilder as QueryBuilder; -use Yajra\Oci8\Query\Processors\OracleProcessor as Processor; -use Yajra\Oci8\Schema\Grammars\OracleGrammar as SchemaGrammar; -use Yajra\Oci8\Schema\OracleBuilder as SchemaBuilder; +use Yajra\Oci8\Query\Grammars\OracleGrammar as OracleQueryGrammar; +use Yajra\Oci8\Query\OracleBuilder as OracleQueryBuilder; +use Yajra\Oci8\Query\Processors\OracleProcessor; +use Yajra\Oci8\Schema\Grammars\OracleGrammar as OracleSchemaGrammar; +use Yajra\Oci8\Schema\OracleBuilder as OracleSchemaBuilder; use Yajra\Oci8\Schema\Sequence; use Yajra\Oci8\Schema\Trigger; use Yajra\Pdo\Oci8\Statement; @@ -26,19 +25,19 @@ class Oci8Connection extends Connection const RECONNECT_ERRORS = 'reconnect_errors'; /** - * @var string + * @var string|null */ - protected $schema; + protected ?string $schema; /** * @var \Yajra\Oci8\Schema\Sequence */ - protected $sequence; + protected Sequence $sequence; /** * @var \Yajra\Oci8\Schema\Trigger */ - protected $trigger; + protected Trigger $trigger; /** * @param PDO|\Closure $pdo @@ -50,7 +49,7 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf { parent::__construct($pdo, $database, $tablePrefix, $config); $this->sequence = new Sequence($this); - $this->trigger = new Trigger($this); + $this->trigger = new Trigger($this); } /** @@ -69,10 +68,10 @@ public function getSchema() * @param string $schema * @return $this */ - public function setSchema($schema) + public function setSchema(string $schema): static { $this->schema = $schema; - $sessionVars = [ + $sessionVars = [ 'CURRENT_SCHEMA' => $schema, ]; @@ -85,7 +84,7 @@ public function setSchema($schema) * @param array $sessionVars * @return $this */ - public function setSessionVars(array $sessionVars) + public function setSessionVars(array $sessionVars): static { $vars = []; foreach ($sessionVars as $option => $value) { @@ -97,7 +96,7 @@ public function setSessionVars(array $sessionVars) } if ($vars) { - $sql = 'ALTER SESSION SET ' . implode(' ', $vars); + $sql = 'ALTER SESSION SET '.implode(' ', $vars); $this->statement($sql); } @@ -109,7 +108,7 @@ public function setSessionVars(array $sessionVars) * * @return \Yajra\Oci8\Schema\Sequence */ - public function getSequence() + public function getSequence(): Sequence { return $this->sequence; } @@ -120,7 +119,7 @@ public function getSequence() * @param \Yajra\Oci8\Schema\Sequence $sequence * @return \Yajra\Oci8\Schema\Sequence */ - public function setSequence(Sequence $sequence) + public function setSequence(Sequence $sequence): Sequence { return $this->sequence = $sequence; } @@ -130,7 +129,7 @@ public function setSequence(Sequence $sequence) * * @return \Yajra\Oci8\Schema\Trigger */ - public function getTrigger() + public function getTrigger(): Trigger { return $this->trigger; } @@ -141,7 +140,7 @@ public function getTrigger() * @param \Yajra\Oci8\Schema\Trigger $trigger * @return \Yajra\Oci8\Schema\Trigger */ - public function setTrigger(Trigger $trigger) + public function setTrigger(Trigger $trigger): Trigger { return $this->trigger = $trigger; } @@ -151,23 +150,23 @@ public function setTrigger(Trigger $trigger) * * @return \Yajra\Oci8\Schema\OracleBuilder */ - public function getSchemaBuilder() + public function getSchemaBuilder(): OracleSchemaBuilder { if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); } - return new SchemaBuilder($this); + return new OracleSchemaBuilder($this); } /** * Get a new query builder instance. * - * @return \Illuminate\Database\Query\Builder + * @return \Yajra\Oci8\Query\OracleBuilder */ - public function query() + public function query(): OracleQueryBuilder { - return new QueryBuilder( + return new OracleQueryBuilder( $this, $this->getQueryGrammar(), $this->getPostProcessor() ); } @@ -178,10 +177,10 @@ public function query() * @param string $format * @return $this */ - public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS') + public function setDateFormat(string $format = 'YYYY-MM-DD HH24:MI:SS'): static { $sessionVars = [ - 'NLS_DATE_FORMAT' => $format, + 'NLS_DATE_FORMAT' => $format, 'NLS_TIMESTAMP_FORMAT' => $format, ]; @@ -193,7 +192,7 @@ public function setDateFormat($format = 'YYYY-MM-DD HH24:MI:SS') * * @return \Doctrine\DBAL\Driver\OCI8\Driver|\Yajra\Oci8\PDO\Oci8Driver */ - protected function getDoctrineDriver() + protected function getDoctrineDriver(): DoctrineDriver|Oci8Driver { return class_exists(Version::class) ? new DoctrineDriver : new Oci8Driver(); } @@ -206,11 +205,15 @@ protected function getDoctrineDriver() * @param string $functionName * @param array $bindings (kvp array) * @param int $returnType (PDO::PARAM_*) - * @param int $length + * @param int|null $length * @return mixed $returnType */ - public function executeFunction($functionName, array $bindings = [], $returnType = PDO::PARAM_STR, $length = null) - { + public function executeFunction( + string $functionName, + array $bindings = [], + int $returnType = PDO::PARAM_STR, + int $length = null + ): mixed { $stmt = $this->createStatementFromFunction($functionName, $bindings); $stmt = $this->addBindingsToStatement($stmt, $bindings); @@ -234,7 +237,7 @@ public function executeFunction($functionName, array $bindings = [], $returnType * @param array $bindings * @return bool */ - public function executeProcedure($procedureName, array $bindings = []) + public function executeProcedure(string $procedureName, array $bindings = []): bool { $stmt = $this->createStatementFromProcedure($procedureName, $bindings); @@ -253,9 +256,13 @@ public function executeProcedure($procedureName, array $bindings = []) * @param array $bindings * @param string $cursorName * @return array + * @throws \ReflectionException */ - public function executeProcedureWithCursor($procedureName, array $bindings = [], $cursorName = ':cursor') - { + public function executeProcedureWithCursor( + string $procedureName, + array $bindings = [], + string $cursorName = ':cursor' + ): array { $stmt = $this->createStatementFromProcedure($procedureName, $bindings, $cursorName); $stmt = $this->addBindingsToStatement($stmt, $bindings); @@ -283,11 +290,11 @@ public function executeProcedureWithCursor($procedureName, array $bindings = [], public function createSqlFromProcedure($procedureName, array $bindings, $cursor = false) { $paramsString = implode(',', array_map(function ($param) { - return ':' . $param; + return ':'.$param; }, array_keys($bindings))); $prefix = count($bindings) ? ',' : ''; - $cursor = $cursor ? $prefix . $cursor : null; + $cursor = $cursor ? $prefix.$cursor : null; return sprintf('begin %s(%s%s); end;', $procedureName, $paramsString, $cursor); } @@ -316,7 +323,7 @@ public function createStatementFromProcedure($procedureName, array $bindings, $c */ public function createStatementFromFunction($functionName, array $bindings) { - $bindings = $bindings ? ':' . implode(', :', array_keys($bindings)) : ''; + $bindings = $bindings ? ':'.implode(', :', array_keys($bindings)) : ''; $sql = sprintf('begin :result := %s(%s); end;', $functionName, $bindings); @@ -330,7 +337,7 @@ public function createStatementFromFunction($functionName, array $bindings) */ protected function getDefaultQueryGrammar() { - return $this->withTablePrefix(new QueryGrammar()); + return $this->withTablePrefix(new OracleQueryGrammar()); } /** @@ -370,11 +377,11 @@ protected function getConfigSchemaPrefix() /** * Get the default schema grammar instance. * - * @return \Illuminate\Database\Grammar|\Yajra\Oci8\Schema\Grammars\OracleGrammar + * @return \Yajra\Oci8\Schema\Grammars\OracleGrammar */ protected function getDefaultSchemaGrammar() { - return $this->withTablePrefix(new SchemaGrammar()); + return $this->withTablePrefix(new OracleSchemaGrammar()); } /** @@ -382,9 +389,9 @@ protected function getDefaultSchemaGrammar() * * @return \Yajra\Oci8\Query\Processors\OracleProcessor */ - protected function getDefaultPostProcessor() + protected function getDefaultPostProcessor(): OracleProcessor { - return new Processor(); + return new OracleProcessor(); } /** @@ -397,17 +404,17 @@ protected function getDefaultPostProcessor() public function addBindingsToStatement(PDOStatement $stmt, array $bindings) { foreach ($bindings as $key => &$binding) { - $value = &$binding; - $type = PDO::PARAM_STR; + $value = &$binding; + $type = PDO::PARAM_STR; $length = -1; if (is_array($binding)) { - $value = &$binding['value']; - $type = array_key_exists('type', $binding) ? $binding['type'] : PDO::PARAM_STR; + $value = &$binding['value']; + $type = array_key_exists('type', $binding) ? $binding['type'] : PDO::PARAM_STR; $length = array_key_exists('length', $binding) ? $binding['length'] : -1; } - $stmt->bindParam(':' . $key, $value, $type, $length); + $stmt->bindParam(':'.$key, $value, $type, $length); } return $stmt; diff --git a/src/Oci8/Schema/Grammars/OracleGrammar.php b/src/Oci8/Schema/Grammars/OracleGrammar.php index c66a3ffa..e2b59762 100644 --- a/src/Oci8/Schema/Grammars/OracleGrammar.php +++ b/src/Oci8/Schema/Grammars/OracleGrammar.php @@ -18,26 +18,26 @@ class OracleGrammar extends Grammar * * @var string */ - protected $wrapper = '%s'; + protected string $wrapper = '%s'; /** * The possible column modifiers. * * @var array */ - protected $modifiers = ['Increment', 'Nullable', 'Default']; + protected array $modifiers = ['Increment', 'Nullable', 'Default']; /** * The possible column serials. * * @var array */ - protected $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; + protected array $serials = ['bigInteger', 'integer', 'mediumInteger', 'smallInteger', 'tinyInteger']; /** * @var string */ - protected $schema_prefix = ''; + protected string $schema_prefix = ''; /** * If this Grammar supports schema changes wrapped in a transaction. @@ -53,17 +53,17 @@ class OracleGrammar extends Grammar * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileCreate(Blueprint $blueprint, Fluent $command) + public function compileCreate(Blueprint $blueprint, Fluent $command): string { $columns = implode(', ', $this->getColumns($blueprint)); - $sql = 'create table ' . $this->wrapTable($blueprint) . " ( $columns"; + $sql = 'create table '.$this->wrapTable($blueprint)." ( $columns"; /* * To be able to name the primary/foreign keys when the table is * initially created we will need to check for a primary/foreign * key commands and add the columns to the table's declaration - * here so they can be created on the tables. + * here, so they can be created on the tables. */ $sql .= (string) $this->addForeignKeys($blueprint); @@ -80,9 +80,9 @@ public function compileCreate(Blueprint $blueprint, Fluent $command) * @param mixed $table * @return string */ - public function wrapTable($table) + public function wrapTable($table): string { - return $this->getSchemaPrefix() . parent::wrapTable($table); + return $this->getSchemaPrefix().parent::wrapTable($table); } /** @@ -90,9 +90,9 @@ public function wrapTable($table) * * @return string */ - public function getSchemaPrefix() + public function getSchemaPrefix(): string { - return ! empty($this->schema_prefix) ? $this->schema_prefix . '.' : ''; + return ! empty($this->schema_prefix) ? $this->schema_prefix.'.' : ''; } /** @@ -100,7 +100,7 @@ public function getSchemaPrefix() * * @param string $prefix */ - public function setSchemaPrefix($prefix) + public function setSchemaPrefix(string $prefix) { $this->schema_prefix = $prefix; } @@ -111,7 +111,7 @@ public function setSchemaPrefix($prefix) * @param \Illuminate\Database\Schema\Blueprint $blueprint * @return string */ - protected function addForeignKeys(Blueprint $blueprint) + protected function addForeignKeys(Blueprint $blueprint): string { $sql = ''; @@ -144,9 +144,9 @@ protected function addForeignKeys(Blueprint $blueprint) * Get the primary key syntax for a table creation statement. * * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @return string|null + * @return string */ - protected function addPrimaryKeys(Blueprint $blueprint) + protected function addPrimaryKeys(Blueprint $blueprint): string { $primary = $this->getCommandByName($blueprint, 'primary'); @@ -164,7 +164,7 @@ protected function addPrimaryKeys(Blueprint $blueprint) * * @return string */ - public function compileTableExists() + public function compileTableExists(): string { return 'select * from all_tables where upper(owner) = upper(?) and upper(table_name) = upper(?)'; } @@ -176,7 +176,7 @@ public function compileTableExists() * @param string $table * @return string */ - public function compileColumnExists($database, $table) + public function compileColumnExists(string $database, string $table): string { return "select column_name from all_tab_cols where upper(owner) = upper('{$database}') and upper(table_name) = upper('{$table}')"; } @@ -188,11 +188,11 @@ public function compileColumnExists($database, $table) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileAdd(Blueprint $blueprint, Fluent $command) + public function compileAdd(Blueprint $blueprint, Fluent $command): string { $columns = implode(', ', $this->getColumns($blueprint)); - $sql = 'alter table ' . $this->wrapTable($blueprint) . " add ( $columns"; + $sql = 'alter table '.$this->wrapTable($blueprint)." add ( $columns"; $sql .= (string) $this->addPrimaryKeys($blueprint); @@ -206,7 +206,7 @@ public function compileAdd(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compilePrimary(Blueprint $blueprint, Fluent $command) + public function compilePrimary(Blueprint $blueprint, Fluent $command): string { $create = $this->getCommandByName($blueprint, 'create'); @@ -264,9 +264,9 @@ public function compileForeign(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileUnique(Blueprint $blueprint, Fluent $command) + public function compileUnique(Blueprint $blueprint, Fluent $command): string { - $columns = array_map(function ($column) { + $columns = array_map(function ($column) { $column = $this->wrap($column); return "lower({$column})"; @@ -284,9 +284,9 @@ public function compileUnique(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileIndex(Blueprint $blueprint, Fluent $command) + public function compileIndex(Blueprint $blueprint, Fluent $command): string { - return "create index {$command->index} on " . $this->wrapTable($blueprint) . ' ( ' . $this->columnize($command->columns) . ' )'; + return "create index {$command->index} on ".$this->wrapTable($blueprint).' ( '.$this->columnize($command->columns).' )'; } /** @@ -296,9 +296,9 @@ public function compileIndex(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDrop(Blueprint $blueprint, Fluent $command) + public function compileDrop(Blueprint $blueprint, Fluent $command): string { - return 'drop table ' . $this->wrapTable($blueprint); + return 'drop table '.$this->wrapTable($blueprint); } /** @@ -306,7 +306,7 @@ public function compileDrop(Blueprint $blueprint, Fluent $command) * * @return string */ - public function compileDropAllTables() + public function compileDropAllTables(): string { return 'BEGIN FOR c IN (SELECT table_name FROM user_tables) LOOP @@ -327,7 +327,7 @@ public function compileDropAllTables() * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropIfExists(Blueprint $blueprint, Fluent $command) + public function compileDropIfExists(Blueprint $blueprint, Fluent $command): string { $table = $this->wrapTable($blueprint); @@ -347,13 +347,13 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropColumn(Blueprint $blueprint, Fluent $command) + public function compileDropColumn(Blueprint $blueprint, Fluent $command): string { $columns = $this->wrapArray($command->columns); $table = $this->wrapTable($blueprint); - return 'alter table ' . $table . ' drop ( ' . implode(', ', $columns) . ' )'; + return 'alter table '.$table.' drop ( '.implode(', ', $columns).' )'; } /** @@ -363,7 +363,7 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropPrimary(Blueprint $blueprint, Fluent $command) + public function compileDropPrimary(Blueprint $blueprint, Fluent $command): string { return $this->dropConstraint($blueprint, $command, 'primary'); } @@ -374,16 +374,16 @@ public function compileDropPrimary(Blueprint $blueprint, Fluent $command) * @param string $type * @return string */ - private function dropConstraint(Blueprint $blueprint, Fluent $command, $type) + private function dropConstraint(Blueprint $blueprint, Fluent $command, string $type): string { $table = $this->wrapTable($blueprint); $index = substr($command->index, 0, 30); if ($type === 'index') { - return "drop index {$index}"; + return "drop index $index"; } - return "alter table {$table} drop constraint {$index}"; + return "alter table $table drop constraint $index"; } /** @@ -393,7 +393,7 @@ private function dropConstraint(Blueprint $blueprint, Fluent $command, $type) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropUnique(Blueprint $blueprint, Fluent $command) + public function compileDropUnique(Blueprint $blueprint, Fluent $command): string { return $this->dropConstraint($blueprint, $command, 'unique'); } @@ -405,7 +405,7 @@ public function compileDropUnique(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropIndex(Blueprint $blueprint, Fluent $command) + public function compileDropIndex(Blueprint $blueprint, Fluent $command): string { return $this->dropConstraint($blueprint, $command, 'index'); } @@ -417,23 +417,23 @@ public function compileDropIndex(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileDropForeign(Blueprint $blueprint, Fluent $command) + public function compileDropForeign(Blueprint $blueprint, Fluent $command): string { return $this->dropConstraint($blueprint, $command, 'foreign'); } /** - * Compile a rename table command. + * Compile a renamed table command. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $command * @return string */ - public function compileRename(Blueprint $blueprint, Fluent $command) + public function compileRename(Blueprint $blueprint, Fluent $command): string { $from = $this->wrapTable($blueprint); - return "alter table {$from} rename to " . $this->wrapTable($command->to); + return "alter table $from rename to ".$this->wrapTable($command->to); } /** @@ -444,14 +444,14 @@ public function compileRename(Blueprint $blueprint, Fluent $command) * @param \Illuminate\Database\Connection $connection * @return array */ - public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) + public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection): array { $table = $this->wrapTable($blueprint); - $rs = []; - $rs[0] = 'alter table ' . $table . ' rename column ' . $command->from . ' to ' . $command->to; + $rs = []; + $rs[0] = 'alter table '.$table.' rename column '.$command->from.' to '.$command->to; - return (array) $rs; + return $rs; } /** @@ -460,9 +460,9 @@ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Conne * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeChar(Fluent $column) + protected function typeChar(Fluent $column): string { - return "char({$column->length})"; + return "char($column->length)"; } /** @@ -471,9 +471,9 @@ protected function typeChar(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeString(Fluent $column) + protected function typeString(Fluent $column): string { - return "varchar2({$column->length})"; + return "varchar2($column->length)"; } /** @@ -482,9 +482,9 @@ protected function typeString(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeNvarchar2(Fluent $column) + protected function typeNvarchar2(Fluent $column): string { - return "nvarchar2({$column->length})"; + return "nvarchar2($column->length)"; } /** @@ -493,7 +493,7 @@ protected function typeNvarchar2(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeText(Fluent $column) + protected function typeText(Fluent $column): string { return 'clob'; } @@ -504,7 +504,7 @@ protected function typeText(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeMediumText(Fluent $column) + protected function typeMediumText(Fluent $column): string { return 'clob'; } @@ -515,7 +515,7 @@ protected function typeMediumText(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeLongText(Fluent $column) + protected function typeLongText(Fluent $column): string { return 'clob'; } @@ -526,7 +526,7 @@ protected function typeLongText(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeInteger(Fluent $column) + protected function typeInteger(Fluent $column): string { $length = ($column->length) ? $column->length : 10; @@ -539,7 +539,7 @@ protected function typeInteger(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeBigInteger(Fluent $column) + protected function typeBigInteger(Fluent $column): string { $length = ($column->length) ? $column->length : 19; @@ -552,7 +552,7 @@ protected function typeBigInteger(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeMediumInteger(Fluent $column) + protected function typeMediumInteger(Fluent $column): string { $length = ($column->length) ? $column->length : 7; @@ -565,7 +565,7 @@ protected function typeMediumInteger(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeSmallInteger(Fluent $column) + protected function typeSmallInteger(Fluent $column): string { $length = ($column->length) ? $column->length : 5; @@ -578,7 +578,7 @@ protected function typeSmallInteger(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeTinyInteger(Fluent $column) + protected function typeTinyInteger(Fluent $column): string { $length = ($column->length) ? $column->length : 3; @@ -591,7 +591,7 @@ protected function typeTinyInteger(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeFloat(Fluent $column) + protected function typeFloat(Fluent $column): string { return "number({$column->total}, {$column->places})"; } @@ -602,7 +602,7 @@ protected function typeFloat(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeDouble(Fluent $column) + protected function typeDouble(Fluent $column): string { return "number({$column->total}, {$column->places})"; } @@ -613,7 +613,7 @@ protected function typeDouble(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeDecimal(Fluent $column) + protected function typeDecimal(Fluent $column): string { return "number({$column->total}, {$column->places})"; } @@ -624,7 +624,7 @@ protected function typeDecimal(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeBoolean(Fluent $column) + protected function typeBoolean(Fluent $column): string { return 'char(1)'; } @@ -635,7 +635,7 @@ protected function typeBoolean(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeEnum(Fluent $column) + protected function typeEnum(Fluent $column): string { $length = ($column->length) ? $column->length : 255; @@ -648,7 +648,7 @@ protected function typeEnum(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeDate(Fluent $column) + protected function typeDate(Fluent $column): string { return 'date'; } @@ -659,7 +659,7 @@ protected function typeDate(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeDateTime(Fluent $column) + protected function typeDateTime(Fluent $column): string { return 'date'; } @@ -670,7 +670,7 @@ protected function typeDateTime(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeTime(Fluent $column) + protected function typeTime(Fluent $column): string { return 'date'; } @@ -681,7 +681,7 @@ protected function typeTime(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeTimestamp(Fluent $column) + protected function typeTimestamp(Fluent $column): string { return 'timestamp'; } @@ -692,7 +692,7 @@ protected function typeTimestamp(Fluent $column) * @param Fluent $column * @return string */ - protected function typeTimestampTz(Fluent $column) + protected function typeTimestampTz(Fluent $column): string { return 'timestamp with time zone'; } @@ -703,7 +703,7 @@ protected function typeTimestampTz(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeBinary(Fluent $column) + protected function typeBinary(Fluent $column): string { return 'blob'; } @@ -714,7 +714,7 @@ protected function typeBinary(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeUuid(Fluent $column) + protected function typeUuid(Fluent $column): string { return 'char(36)'; } @@ -725,7 +725,7 @@ protected function typeUuid(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeIpAddress(Fluent $column) + protected function typeIpAddress(Fluent $column): string { return 'varchar(45)'; } @@ -736,7 +736,7 @@ protected function typeIpAddress(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeMacAddress(Fluent $column) + protected function typeMacAddress(Fluent $column): string { return 'varchar(17)'; } @@ -747,7 +747,7 @@ protected function typeMacAddress(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeJson(Fluent $column) + protected function typeJson(Fluent $column): string { return 'clob'; } @@ -758,7 +758,7 @@ protected function typeJson(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeJsonb(Fluent $column) + protected function typeJsonb(Fluent $column): string { return 'clob'; } @@ -770,20 +770,20 @@ protected function typeJsonb(Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function modifyNullable(Blueprint $blueprint, Fluent $column) + protected function modifyNullable(Blueprint $blueprint, Fluent $column): string { // check if field is declared as enum $enum = ''; if (count((array) $column->allowed)) { $columnName = $this->wrapValue($column->name); - $enum = " check ({$columnName} in ('" . implode("', '", $column->allowed) . "'))"; + $enum = " check ({$columnName} in ('".implode("', '", $column->allowed)."'))"; } $null = $column->nullable ? ' null' : ' not null'; $null .= $enum; if (! is_null($column->default)) { - return ' default ' . $this->getDefaultValue($column->default) . $null; + return ' default '.$this->getDefaultValue($column->default).$null; } return $null; @@ -796,7 +796,7 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column) * @param \Illuminate\Support\Fluent $column * @return string */ - protected function modifyDefault(Blueprint $blueprint, Fluent $column) + protected function modifyDefault(Blueprint $blueprint, Fluent $column): string { // implemented @modifyNullable return ''; @@ -807,7 +807,7 @@ protected function modifyDefault(Blueprint $blueprint, Fluent $column) * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column - * @return string|null + * @return void */ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { @@ -822,7 +822,7 @@ protected function modifyIncrement(Blueprint $blueprint, Fluent $column) * @param string $value * @return string */ - protected function wrapValue($value) + protected function wrapValue($value): string { if ($this->isReserved($value)) { return Str::upper(parent::wrapValue($value)); diff --git a/src/Oci8/Schema/OracleBlueprint.php b/src/Oci8/Schema/OracleBlueprint.php index 3582931b..031495ba 100644 --- a/src/Oci8/Schema/OracleBlueprint.php +++ b/src/Oci8/Schema/OracleBlueprint.php @@ -3,22 +3,23 @@ namespace Yajra\Oci8\Schema; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\ColumnDefinition; class OracleBlueprint extends Blueprint { /** * Table comment. * - * @var string + * @var string|null */ - public $comment = null; + public ?string $comment = null; /** * Column comments. * * @var array */ - public $commentColumns = []; + public array $commentColumns = []; /** * Database prefix variable. @@ -32,7 +33,7 @@ class OracleBlueprint extends Blueprint * * @param string $prefix */ - public function setTablePrefix($prefix = '') + public function setTablePrefix(string $prefix = '') { $this->prefix = $prefix; } @@ -44,7 +45,7 @@ public function setTablePrefix($prefix = '') * @param array $columns * @return string */ - protected function createIndexName($type, array $columns) + protected function createIndexName($type, array $columns): string { $short_type = [ 'primary' => 'pk', @@ -52,7 +53,7 @@ protected function createIndexName($type, array $columns) 'unique' => 'uk', ]; - $type = isset($short_type[$type]) ? $short_type[$type] : $type; + $type = $short_type[$type] ?? $type; $index = strtolower($this->prefix . $this->table . '_' . implode('_', $columns) . '_' . $type); @@ -81,9 +82,9 @@ protected function createIndexName($type, array $columns) * * @param string $column * @param int $length - * @return \Illuminate\Support\Fluent + * @return \Illuminate\Database\Schema\ColumnDefinition */ - public function nvarchar2($column, $length = 255) + public function nvarchar2(string $column, int $length = 255): ColumnDefinition { return $this->addColumn('nvarchar2', $column, compact('length')); } diff --git a/src/Oci8/Schema/OracleBuilder.php b/src/Oci8/Schema/OracleBuilder.php index cfa942d3..58645519 100644 --- a/src/Oci8/Schema/OracleBuilder.php +++ b/src/Oci8/Schema/OracleBuilder.php @@ -5,23 +5,27 @@ use Closure; use Illuminate\Database\Connection; use Illuminate\Database\Schema\Builder; +use Yajra\Oci8\Oci8Connection; +/** + * @property-read Oci8Connection $connection + */ class OracleBuilder extends Builder { /** * @var \Yajra\Oci8\Schema\OracleAutoIncrementHelper */ - public $helper; + public OracleAutoIncrementHelper $helper; /** * @var \Yajra\Oci8\Schema\Comment */ - public $comment; + public Comment $comment; /** - * @param Connection $connection + * @param \Yajra\Oci8\Oci8Connection $connection */ - public function __construct(Connection $connection) + public function __construct(Oci8Connection $connection) { parent::__construct($connection); $this->helper = new OracleAutoIncrementHelper($connection); @@ -33,9 +37,9 @@ public function __construct(Connection $connection) * * @param string $table * @param Closure $callback - * @return \Illuminate\Database\Schema\Blueprint + * @return void */ - public function create($table, Closure $callback) + public function create($table, Closure $callback): void { $blueprint = $this->createBlueprint($table); @@ -54,10 +58,10 @@ public function create($table, Closure $callback) * Create a new command set with a Closure. * * @param string $table - * @param Closure $callback - * @return \Illuminate\Database\Schema\Blueprint + * @param \Closure|null $callback + * @return \Yajra\Oci8\Schema\OracleBlueprint */ - protected function createBlueprint($table, Closure $callback = null) + protected function createBlueprint($table, Closure $callback = null): OracleBlueprint { $blueprint = new OracleBlueprint($table, $callback); $blueprint->setTablePrefix($this->connection->getTablePrefix()); @@ -70,9 +74,9 @@ protected function createBlueprint($table, Closure $callback = null) * * @param string $table * @param Closure $callback - * @return \Illuminate\Database\Schema\Blueprint + * @return void */ - public function table($table, Closure $callback) + public function table($table, Closure $callback): void { $blueprint = $this->createBlueprint($table); @@ -93,9 +97,9 @@ public function table($table, Closure $callback) * Drop a table from the schema. * * @param string $table - * @return \Illuminate\Database\Schema\Blueprint + * @return void */ - public function drop($table) + public function drop($table): void { $this->helper->dropAutoIncrementObjects($table); parent::drop($table); @@ -106,9 +110,9 @@ public function drop($table) * * @return void */ - public function dropAllTables() + public function dropAllTables(): void { - $this->connection->statement($this->grammar->compileDropAllTables()); + $this->connection->statement($this->connection->getSchemaGrammar()->compileDropAllTables()); } /** diff --git a/src/Oci8/Schema/Sequence.php b/src/Oci8/Schema/Sequence.php index dab9d561..edfe282d 100644 --- a/src/Oci8/Schema/Sequence.php +++ b/src/Oci8/Schema/Sequence.php @@ -2,19 +2,19 @@ namespace Yajra\Oci8\Schema; -use Illuminate\Database\Connection; +use Yajra\Oci8\Oci8Connection; class Sequence { /** - * @var \Illuminate\Database\Connection|\Yajra\Oci8\Oci8Connection + * @var \Yajra\Oci8\Oci8Connection */ - protected $connection; + protected Oci8Connection $connection; /** - * @param Connection $connection + * @param \Yajra\Oci8\Oci8Connection $connection */ - public function __construct(Connection $connection) + public function __construct(Oci8Connection $connection) { $this->connection = $connection; } @@ -30,12 +30,14 @@ public function __construct(Connection $connection) * @param int $increment * @return bool */ - public function create($name, $start = 1, $nocache = false, $min = 1, $max = false, $increment = 1) - { - if (! $name) { - return false; - } - + public function create( + string $name, + int $start = 1, + bool $nocache = false, + int $min = 1, + bool $max = false, + int $increment = 1 + ): bool { $name = $this->wrap($name); $nocache = $nocache ? 'nocache' : ''; @@ -53,10 +55,10 @@ public function create($name, $start = 1, $nocache = false, $min = 1, $max = fal * @param string $name * @return string */ - public function wrap($name) + public function wrap(string $name): string { if ($this->connection->getConfig('prefix_schema')) { - return $this->connection->getConfig('prefix_schema') . '.' . $name; + return $this->connection->getConfig('prefix_schema').'.'.$name; } return $name; @@ -68,7 +70,7 @@ public function wrap($name) * @param string $name * @return bool */ - public function drop($name) + public function drop(string $name): bool { // check if a valid name and sequence exists if (! $name || ! $this->exists($name)) { @@ -95,26 +97,25 @@ public function drop($name) * @param string $name * @return bool */ - public function exists($name) + public function exists(string $name): bool { - if (! $name) { - return false; - } - $name = $this->wrap($name); - return $this->connection->selectOne( + /** @var \stdClass $sequence */ + $sequence = $this->connection->selectOne( "select * from all_sequences where sequence_name=upper('{$name}') and sequence_owner=upper(user)" ); + + return is_object($sequence); } /** * get sequence next value. * - * @param string $name + * @param string|null $name * @return int */ - public function nextValue($name) + public function nextValue(string $name = null): int { if (! $name) { return 0; @@ -122,7 +123,10 @@ public function nextValue($name) $name = $this->wrap($name); - return $this->connection->selectOne("SELECT $name.NEXTVAL as \"id\" FROM DUAL")->id; + /** @var \stdClass $sequence */ + $sequence = $this->connection->selectOne("SELECT $name.NEXTVAL as \"id\" FROM DUAL"); + + return $sequence->id; } /** @@ -131,7 +135,7 @@ public function nextValue($name) * @param string $name * @return int */ - public function currentValue($name) + public function currentValue(string $name): int { return $this->lastInsertId($name); } @@ -142,7 +146,7 @@ public function currentValue($name) * @param string $name * @return int */ - public function lastInsertId($name) + public function lastInsertId(string $name): int { // check if a valid name and sequence exists if (! $name || ! $this->exists($name)) { @@ -151,6 +155,9 @@ public function lastInsertId($name) $name = $this->wrap($name); - return $this->connection->selectOne("select {$name}.currval as \"id\" from dual")->id; + /** @var \stdClass $sequence */ + $sequence = $this->connection->selectOne("select $name.currval as \"id\" from dual"); + + return $sequence->id; } } From 4b3813dc031481178a6826cecf511409aad966e9 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Thu, 12 May 2022 11:43:38 +0800 Subject: [PATCH 2/2] stan fixes --- src/Oci8/Auth/OracleUserProvider.php | 15 +++++++++++---- src/Oci8/Oci8ServiceProvider.php | 22 ++++++++++++---------- src/Oci8/PDO/Oci8Driver.php | 2 +- src/Oci8/Schema/OracleBlueprint.php | 3 ++- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Oci8/Auth/OracleUserProvider.php b/src/Oci8/Auth/OracleUserProvider.php index f813cd12..b8d95d03 100644 --- a/src/Oci8/Auth/OracleUserProvider.php +++ b/src/Oci8/Auth/OracleUserProvider.php @@ -3,6 +3,7 @@ namespace Yajra\Oci8\Auth; use Illuminate\Auth\EloquentUserProvider; +use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Str; class OracleUserProvider extends EloquentUserProvider @@ -13,10 +14,10 @@ class OracleUserProvider extends EloquentUserProvider * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ - public function retrieveByCredentials(array $credentials) + public function retrieveByCredentials(array $credentials): ?Authenticatable { if (empty($credentials)) { - return; + return null; } // First we will add each credential element to the query as a where clause. @@ -26,10 +27,16 @@ public function retrieveByCredentials(array $credentials) foreach ($credentials as $key => $value) { if (! Str::contains($key, 'password')) { - $query->whereRaw("upper({$key}) = upper(?)", [$value]); + $query->whereRaw("upper($key) = upper(?)", [$value]); } } - return $query->first(); + $model = $query->first(); + + if (! $model instanceof Authenticatable) { + return null; + } + + return $model; } } diff --git a/src/Oci8/Oci8ServiceProvider.php b/src/Oci8/Oci8ServiceProvider.php index e0540fb0..f85d18b7 100644 --- a/src/Oci8/Oci8ServiceProvider.php +++ b/src/Oci8/Oci8ServiceProvider.php @@ -15,15 +15,17 @@ class Oci8ServiceProvider extends ServiceProvider * * @var bool */ - protected $defer = false; + protected bool $defer = false; /** * Boot Oci8 Provider. + * + * @return void */ - public function boot() + public function boot(): void { $this->publishes([ - __DIR__ . '/../config/oracle.php' => config_path('oracle.php'), + __DIR__.'/../config/oracle.php' => config_path('oracle.php'), ], 'oracle'); Auth::provider('oracle', function ($app, array $config) { @@ -36,12 +38,12 @@ public function boot() * * @return void */ - public function register() + public function register(): void { if (file_exists(config_path('oracle.php'))) { $this->mergeConfigFrom(config_path('oracle.php'), 'database.connections'); } else { - $this->mergeConfigFrom(__DIR__ . '/../config/oracle.php', 'database.connections'); + $this->mergeConfigFrom(__DIR__.'/../config/oracle.php', 'database.connections'); } Connection::resolverFor('oracle', function ($connection, $database, $prefix, $config) { @@ -59,11 +61,11 @@ public function register() // set oracle session variables $sessionVars = [ - 'NLS_TIME_FORMAT' => 'HH24:MI:SS', - 'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', - 'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', + 'NLS_TIME_FORMAT' => 'HH24:MI:SS', + 'NLS_DATE_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', + 'NLS_TIMESTAMP_FORMAT' => 'YYYY-MM-DD HH24:MI:SS', 'NLS_TIMESTAMP_TZ_FORMAT' => 'YYYY-MM-DD HH24:MI:SS TZH:TZM', - 'NLS_NUMERIC_CHARACTERS' => '.,', + 'NLS_NUMERIC_CHARACTERS' => '.,', ]; // Like Postgres, Oracle allows the concept of "schema" @@ -93,7 +95,7 @@ public function register() * * @return string[] */ - public function provides() + public function provides(): array { return []; } diff --git a/src/Oci8/PDO/Oci8Driver.php b/src/Oci8/PDO/Oci8Driver.php index 4e8d7d26..2c47362d 100644 --- a/src/Oci8/PDO/Oci8Driver.php +++ b/src/Oci8/PDO/Oci8Driver.php @@ -12,7 +12,7 @@ class Oci8Driver extends AbstractOracleDriver /** * {@inheritdoc} */ - public function getName() + public function getName(): string { return 'oci8'; } diff --git a/src/Oci8/Schema/OracleBlueprint.php b/src/Oci8/Schema/OracleBlueprint.php index 031495ba..c28b0d5e 100644 --- a/src/Oci8/Schema/OracleBlueprint.php +++ b/src/Oci8/Schema/OracleBlueprint.php @@ -32,8 +32,9 @@ class OracleBlueprint extends Blueprint * Set table prefix settings. * * @param string $prefix + * @return void */ - public function setTablePrefix(string $prefix = '') + public function setTablePrefix(string $prefix = ''): void { $this->prefix = $prefix; }