diff --git a/3.0.0.md b/3.0.0.md index b1c3abfb90..fd56b6c77e 100644 --- a/3.0.0.md +++ b/3.0.0.md @@ -28,3 +28,11 @@ Changed `setDriver(DriverInterface $driver)` for all the following platforms: - Zend\Db\Adapter\Platform\Postgresql - Zend\Db\Adapter\Platform\Sqlite - Zend\Db\Adapter\Platform\SqlServer + +- Zend\Db\Sql\Platform\PlatformDecoratorInterface + +`setSubject()` cannot be type hinted, since implementations use various types + +- Zend\Db\Sql\AbstractSql + +`Zend\Db\Sql\Select::resolveTable()` returns an array where as its parent returns a string diff --git a/src/Sql/AbstractExpression.php b/src/Sql/AbstractExpression.php index 3fbee7c1c6..73cb1c474d 100644 --- a/src/Sql/AbstractExpression.php +++ b/src/Sql/AbstractExpression.php @@ -1,19 +1,29 @@ buildNormalizedArgument($argument, self::TYPE_VALUE); @@ -50,20 +59,20 @@ protected function normalizeArgument($argument, $defaultType = self::TYPE_VALUE) $key = key($argument); - if (is_integer($key) && ! in_array($value, $this->allowedTypes)) { + if (is_int($key) && ! in_array($value, $this->allowedTypes)) { return $this->buildNormalizedArgument($value, $defaultType); } return $this->buildNormalizedArgument($key, $value); } - throw new Exception\InvalidArgumentException(sprintf( + throw new InvalidArgumentException(sprintf( '$argument should be %s or %s or %s or %s or %s, "%s" given', 'null', 'scalar', 'array', - 'Zend\Db\Sql\ExpressionInterface', - 'Zend\Db\Sql\SqlInterface', + ExpressionInterface::class, + SqlInterface::class, is_object($argument) ? get_class($argument) : gettype($argument) )); } @@ -71,15 +80,14 @@ protected function normalizeArgument($argument, $defaultType = self::TYPE_VALUE) /** * @param mixed $argument * @param string $argumentType - * - * @return array + * @return mixed[] * * @throws Exception\InvalidArgumentException */ - private function buildNormalizedArgument($argument, $argumentType) + private function buildNormalizedArgument($argument, string $argumentType) : array { if (! in_array($argumentType, $this->allowedTypes)) { - throw new Exception\InvalidArgumentException(sprintf( + throw new InvalidArgumentException(sprintf( 'Argument type should be in array(%s)', implode(',', $this->allowedTypes) )); diff --git a/src/Sql/AbstractPreparableSql.php b/src/Sql/AbstractPreparableSql.php index 6d95f90d84..ca8654a04f 100644 --- a/src/Sql/AbstractPreparableSql.php +++ b/src/Sql/AbstractPreparableSql.php @@ -1,27 +1,24 @@ getParameterContainer(); if (! $parameterContainer instanceof ParameterContainer) { diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index f8935ca44e..8318b72236 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -1,59 +1,55 @@ '', 'subselectCount' => 0]; - /** - * @var array - */ + /** @var array */ protected $instanceParameterIndex = []; - /** - * {@inheritDoc} - */ - public function getSqlString(PlatformInterface $adapterPlatform = null) + public function getSqlString(?PlatformInterface $adapterPlatform = null) : string { - $adapterPlatform = ($adapterPlatform) ?: new DefaultAdapterPlatform; + $adapterPlatform = $adapterPlatform ?? new DefaultAdapterPlatform(); return $this->buildSqlString($adapterPlatform); } - /** - * @param PlatformInterface $platform - * @param null|DriverInterface $driver - * @param null|ParameterContainer $parameterContainer - * @return string - */ protected function buildSqlString( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?string { $this->localizeVariables(); $sqls = []; @@ -86,39 +82,42 @@ protected function buildSqlString( * Render table with alias in from/join parts * * @todo move TableIdentifier concatenation here - * @param string $table - * @param string $alias + * @param string $table + * @param string|null $alias * @return string */ - protected function renderTable($table, $alias = null) + protected function renderTable(string $table, ?string $alias = null) : string { return $table . ($alias ? ' AS ' . $alias : ''); } /** * @staticvar int $runtimeExpressionPrefix - * @param ExpressionInterface $expression - * @param PlatformInterface $platform - * @param null|DriverInterface $driver + * + * @param ExpressionInterface $expression + * @param PlatformInterface $platform + * @param null|DriverInterface $driver * @param null|ParameterContainer $parameterContainer - * @param null|string $namedParameterPrefix + * @param null|string $namedParameterPrefix * @return string + * * @throws Exception\RuntimeException */ protected function processExpression( ExpressionInterface $expression, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null, - $namedParameterPrefix = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null, + ?string $namedParameterPrefix = null + ) : string { $namedParameterPrefix = ! $namedParameterPrefix ? $namedParameterPrefix : $this->processInfo['paramPrefix'] . $namedParameterPrefix; + // static counter for the number of times this method was invoked across the PHP runtime static $runtimeExpressionPrefix = 0; - if ($parameterContainer && ((! is_string($namedParameterPrefix) || $namedParameterPrefix == ''))) { + if ($parameterContainer && (! is_string($namedParameterPrefix) || $namedParameterPrefix === '')) { $namedParameterPrefix = sprintf('expr%04dParam', ++$runtimeExpressionPrefix); } else { $namedParameterPrefix = preg_replace('/\s/', '__', $namedParameterPrefix); @@ -151,7 +150,7 @@ protected function processExpression( } if (! is_array($part)) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'Elements returned from getExpressionData() array must be a string or array.' ); } @@ -159,7 +158,8 @@ protected function processExpression( // Process values and types (the middle and last position of the // expression data) $values = $part[1]; - $types = isset($part[2]) ? $part[2] : []; + $types = isset($part[2]) ?? []; + foreach ($values as $vIndex => $value) { if (! isset($types[$vIndex])) { continue; @@ -179,9 +179,9 @@ protected function processExpression( $parameterContainer, $namedParameterPrefix . $vIndex . 'subpart' ); - } elseif ($type == ExpressionInterface::TYPE_IDENTIFIER) { + } elseif ($type === ExpressionInterface::TYPE_IDENTIFIER) { $values[$vIndex] = $platform->quoteIdentifierInFragment($value); - } elseif ($type == ExpressionInterface::TYPE_VALUE) { + } elseif ($type === ExpressionInterface::TYPE_VALUE) { // if prepareType is set, it means that this particular value must be // passed back to the statement in a way it can be used as a placeholder value if ($parameterContainer) { @@ -193,7 +193,7 @@ protected function processExpression( // if not a preparable statement, simply quote the value and move on $values[$vIndex] = $platform->quoteValue($value); - } elseif ($type == ExpressionInterface::TYPE_LITERAL) { + } elseif ($type === ExpressionInterface::TYPE_LITERAL) { $values[$vIndex] = $value; } } @@ -208,13 +208,12 @@ protected function processExpression( /** * @param string|array $specifications - * @param array $parameters - * + * @param array $parameters * @return string * * @throws Exception\RuntimeException */ - protected function createSqlFromSpecificationAndParameters($specifications, $parameters) + protected function createSqlFromSpecificationAndParameters($specifications, array $parameters) : string { if (is_string($specifications)) { return vsprintf($specifications, $parameters); @@ -223,7 +222,7 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par $parametersCount = count($parameters); foreach ($specifications as $specificationString => $paramSpecs) { - if ($parametersCount == count($paramSpecs)) { + if ($parametersCount === count($paramSpecs)) { break; } @@ -231,7 +230,7 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par } if (! isset($specificationString)) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'A number of parameters was found that is not supported by this specification' ); } @@ -248,7 +247,7 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par } if (! isset($paramSpecs[$position][$ppCount])) { - throw new Exception\RuntimeException(sprintf( + throw new RuntimeException(sprintf( 'A number of parameters (%d) was found that is not supported by this specification', $ppCount )); @@ -259,7 +258,7 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par } elseif ($paramSpecs[$position] !== null) { $ppCount = count($paramsForPosition); if (! isset($paramSpecs[$position][$ppCount])) { - throw new Exception\RuntimeException(sprintf( + throw new RuntimeException(sprintf( 'A number of parameters (%d) was found that is not supported by this specification', $ppCount )); @@ -269,22 +268,16 @@ protected function createSqlFromSpecificationAndParameters($specifications, $par $topParameters[] = $paramsForPosition; } } + return vsprintf($specificationString, $topParameters); } - /** - * @param Select $subselect - * @param PlatformInterface $platform - * @param null|DriverInterface $driver - * @param null|ParameterContainer $parameterContainer - * @return string - */ protected function processSubSelect( - Select $subselect, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + Select $subselect, + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : string { if ($this instanceof PlatformDecoratorInterface) { $decorator = clone $this; $decorator->setSubject($subselect); @@ -304,33 +297,26 @@ protected function processSubSelect( // copy count $this->processInfo['subselectCount'] = $decorator->processInfo['subselectCount']; + return $sql; } return $decorator->buildSqlString($platform, $driver, $parameterContainer); } - /** - * @param Join[] $joins - * @param PlatformInterface $platform - * @param null|DriverInterface $driver - * @param null|ParameterContainer $parameterContainer - * @return null|string[] Null if no joins present, array of JOIN statements - * otherwise - * @throws Exception\InvalidArgumentException for invalid JOIN table names. - */ protected function processJoin( - Join $joins, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + Join $joins, + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?array { if (! $joins->count()) { - return; + return null; } // process joins $joinSpecArgArray = []; + foreach ($joins->getJoins() as $j => $join) { $joinName = null; $joinAs = null; @@ -355,7 +341,7 @@ protected function processJoin( } elseif (is_string($joinName) || (is_object($joinName) && is_callable([$joinName, '__toString']))) { $joinName = $platform->quoteIdentifier($joinName); } else { - throw new Exception\InvalidArgumentException(sprintf( + throw new InvalidArgumentException(sprintf( 'Join name expected to be Expression|TableIdentifier|Select|string, "%s" given', gettype($joinName) )); @@ -369,7 +355,7 @@ protected function processJoin( // on expression // note: for Expression objects, pass them to processExpression with a prefix specific to each join // (used for named parameters) - if (($join['on'] instanceof ExpressionInterface)) { + if ($join['on'] instanceof ExpressionInterface) { $joinSpecArgArray[$j][] = $this->processExpression( $join['on'], $platform, @@ -391,19 +377,19 @@ protected function processJoin( /** * @param null|array|ExpressionInterface|Select $column - * @param PlatformInterface $platform - * @param null|DriverInterface $driver - * @param null|string $namedParameterPrefix - * @param null|ParameterContainer $parameterContainer + * @param PlatformInterface $platform + * @param null|DriverInterface $driver + * @param null|ParameterContainer $parameterContainer + * @param null|string $namedParameterPrefix * @return string */ protected function resolveColumnValue( $column, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null, - $namedParameterPrefix = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null, + ?string $namedParameterPrefix = null + ) : string { $namedParameterPrefix = ! $namedParameterPrefix ? $namedParameterPrefix : $this->processInfo['paramPrefix'] . $namedParameterPrefix; @@ -422,12 +408,16 @@ protected function resolveColumnValue( if ($column instanceof ExpressionInterface) { return $this->processExpression($column, $platform, $driver, $parameterContainer, $namedParameterPrefix); } + if ($column instanceof Select) { return '(' . $this->processSubSelect($column, $platform, $driver, $parameterContainer) . ')'; } + if ($column === null) { return 'NULL'; } + + /** @var string $column */ return $isIdentifier ? $fromTable . $platform->quoteIdentifierInFragment($column) : $platform->quoteValue($column); @@ -435,24 +425,25 @@ protected function resolveColumnValue( /** * @param string|TableIdentifier|Select $table - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer + * @param PlatformInterface $platform + * @param DriverInterface $driver + * @param ParameterContainer $parameterContainer * @return string */ protected function resolveTable( $table, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { $schema = null; + if ($table instanceof TableIdentifier) { - list($table, $schema) = $table->getTableAndSchema(); + [$table, $schema] = $table->getTableAndSchema(); } if ($table instanceof Select) { - $table = '(' . $this->processSubselect($table, $platform, $driver, $parameterContainer) . ')'; + $table = '(' . $this->processSubSelect($table, $platform, $driver, $parameterContainer) . ')'; } elseif ($table) { $table = $platform->quoteIdentifier($table); } @@ -460,13 +451,14 @@ protected function resolveTable( if ($schema && $table) { $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table; } + return $table; } /** * Copy variables from the subject into the local properties */ - protected function localizeVariables() + protected function localizeVariables() : void { if (! $this instanceof PlatformDecoratorInterface) { return; diff --git a/src/Sql/Combine.php b/src/Sql/Combine.php index bedb170f2c..307d9184f6 100644 --- a/src/Sql/Combine.php +++ b/src/Sql/Combine.php @@ -1,39 +1,45 @@ '%1$s (%2$s) ', ]; - /** - * @var Select[][] - */ + /** @var Select[][] */ private $combine = []; /** @@ -41,7 +47,7 @@ class Combine extends AbstractPreparableSql * @param string $type * @param string $modifier */ - public function __construct($select = null, $type = self::COMBINE_UNION, $modifier = '') + public function __construct($select = null, string $type = self::COMBINE_UNION, string $modifier = '') { if ($select) { $this->combine($select, $type, $modifier); @@ -52,14 +58,13 @@ public function __construct($select = null, $type = self::COMBINE_UNION, $modifi * Create combine clause * * @param Select|array $select - * @param string $type - * @param string $modifier - * - * @return self Provides a fluent interface + * @param string $type + * @param string $modifier + * @return $this * * @throws Exception\InvalidArgumentException */ - public function combine($select, $type = self::COMBINE_UNION, $modifier = '') + public function combine($select, string $type = self::COMBINE_UNION, string $modifier = '') : self { if (is_array($select)) { foreach ($select as $combine) { @@ -83,11 +88,8 @@ public function combine($select, $type = self::COMBINE_UNION, $modifier = '') )); } - $this->combine[] = [ - 'select' => $select, - 'type' => $type, - 'modifier' => $modifier - ]; + $this->combine[] = compact('select', 'type', 'modifier'); + return $this; } @@ -96,10 +98,9 @@ public function combine($select, $type = self::COMBINE_UNION, $modifier = '') * * @param Select|array $select * @param string $modifier - * * @return self */ - public function union($select, $modifier = '') + public function union($select, string $modifier = '') : self { return $this->combine($select, self::COMBINE_UNION, $modifier); } @@ -109,10 +110,9 @@ public function union($select, $modifier = '') * * @param Select|array $select * @param string $modifier - * * @return self */ - public function except($select, $modifier = '') + public function except($select, string $modifier = '') : self { return $this->combine($select, self::COMBINE_EXCEPT, $modifier); } @@ -121,35 +121,27 @@ public function except($select, $modifier = '') * Create intersect clause * * @param Select|array $select - * @param string $modifier + * @param string $modifier * @return self */ - public function intersect($select, $modifier = '') + public function intersect($select, string $modifier = '') : self { return $this->combine($select, self::COMBINE_INTERSECT, $modifier); } - /** - * Build sql string - * - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * - * @return string - */ protected function buildSqlString( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?string { if (! $this->combine) { - return; + return null; } $sql = ''; + foreach ($this->combine as $i => $combine) { - $type = $i == 0 + $type = $i === 0 ? '' : strtoupper($combine['type'] . ($combine['modifier'] ? ' ' . $combine['modifier'] : '')); $select = $this->processSubSelect($combine['select'], $platform, $driver, $parameterContainer); @@ -159,13 +151,11 @@ protected function buildSqlString( $select ); } + return trim($sql, ' '); } - /** - * @return self Provides a fluent interface - */ - public function alignColumns() + public function alignColumns() : self { if (! $this->combine) { return $this; @@ -189,17 +179,11 @@ public function alignColumns() } $combine['select']->columns($aligned, false); } + return $this; } - /** - * Get raw state - * - * @param string $key - * - * @return array - */ - public function getRawState($key = null) + public function getRawState(?string $key = null) : array { $rawState = [ self::COMBINE => $this->combine, @@ -207,6 +191,7 @@ public function getRawState($key = null) ? $this->combine[0]['select']->getRawState(self::COLUMNS) : [], ]; + return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } } diff --git a/src/Sql/Ddl/AlterTable.php b/src/Sql/Ddl/AlterTable.php index bee08f9996..dc6ae8dbe4 100644 --- a/src/Sql/Ddl/AlterTable.php +++ b/src/Sql/Ddl/AlterTable.php @@ -1,50 +1,45 @@ "ALTER TABLE %1\$s\n", self::ADD_COLUMNS => [ - "%1\$s" => [ - [1 => "ADD COLUMN %1\$s,\n", 'combinedby' => ""] - ] + '%1$s' => [ + [1 => "ADD COLUMN %1\$s,\n", self::COMBINEDBY => ''], + ], ], self::CHANGE_COLUMNS => [ - "%1\$s" => [ - [2 => "CHANGE COLUMN %1\$s %2\$s,\n", 'combinedby' => ""], - ] + '%1$s' => [ + [2 => "CHANGE COLUMN %1\$s %2\$s,\n", self::COMBINEDBY => ''], + ], ], self::DROP_COLUMNS => [ - "%1\$s" => [ - [1 => "DROP COLUMN %1\$s,\n", 'combinedby' => ""], - ] + '%1$s' => [ + [1 => "DROP COLUMN %1\$s,\n", self::COMBINEDBY => ''], + ], ], self::ADD_CONSTRAINTS => [ - "%1\$s" => [ - [1 => "ADD %1\$s,\n", 'combinedby' => ""], - ] + '%1$s' => [ + [1 => "ADD %1\$s,\n", self::COMBINEDBY => ''], + ], ], self::DROP_CONSTRAINTS => [ - "%1\$s" => [ - [1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => ""], - ] - ] + '%1$s' => [ + [1 => "DROP CONSTRAINT %1\$s,\n", self::COMBINEDBY => ''], + ], + ], ]; - /** - * @var string - */ + /** @var string */ protected $table = ''; /** @@ -90,14 +83,10 @@ class AlterTable extends AbstractSql implements SqlInterface */ public function __construct($table = '') { - ($table) ? $this->setTable($table) : null; + $table ? $this->setTable($table) : null; } - /** - * @param string $name - * @return self Provides a fluent interface - */ - public function setTable($name) + public function setTable(string $name) : self { $this->table = $name; @@ -105,10 +94,10 @@ public function setTable($name) } /** - * @param Column\ColumnInterface $column - * @return self Provides a fluent interface + * @param ColumnInterface $column + * @return $this */ - public function addColumn(Column\ColumnInterface $column) + public function addColumn(ColumnInterface $column) : self { $this->addColumns[] = $column; @@ -116,11 +105,11 @@ public function addColumn(Column\ColumnInterface $column) } /** - * @param string $name - * @param Column\ColumnInterface $column - * @return self Provides a fluent interface + * @param string $name + * @param ColumnInterface $column + * @return $this */ - public function changeColumn($name, Column\ColumnInterface $column) + public function changeColumn(string $name, ColumnInterface $column) : self { $this->changeColumns[$name] = $column; @@ -128,10 +117,10 @@ public function changeColumn($name, Column\ColumnInterface $column) } /** - * @param string $name - * @return self Provides a fluent interface + * @param string $name + * @return $this */ - public function dropColumn($name) + public function dropColumn(string $name) : self { $this->dropColumns[] = $name; @@ -139,10 +128,10 @@ public function dropColumn($name) } /** - * @param string $name - * @return self Provides a fluent interface + * @param string $name + * @return $this */ - public function dropConstraint($name) + public function dropConstraint(string $name) : self { $this->dropConstraints[] = $name; @@ -150,10 +139,10 @@ public function dropConstraint($name) } /** - * @param Constraint\ConstraintInterface $constraint - * @return self Provides a fluent interface + * @param ConstraintInterface $constraint + * @return $this */ - public function addConstraint(Constraint\ConstraintInterface $constraint) + public function addConstraint(ConstraintInterface $constraint) : self { $this->addConstraints[] = $constraint; @@ -161,31 +150,32 @@ public function addConstraint(Constraint\ConstraintInterface $constraint) } /** - * @param string|null $key + * @param string|null $key * @return array */ - public function getRawState($key = null) + public function getRawState(?string $key = null) : array { $rawState = [ - self::TABLE => $this->table, - self::ADD_COLUMNS => $this->addColumns, - self::DROP_COLUMNS => $this->dropColumns, - self::CHANGE_COLUMNS => $this->changeColumns, - self::ADD_CONSTRAINTS => $this->addConstraints, + self::TABLE => $this->table, + self::ADD_COLUMNS => $this->addColumns, + self::DROP_COLUMNS => $this->dropColumns, + self::CHANGE_COLUMNS => $this->changeColumns, + self::ADD_CONSTRAINTS => $this->addConstraints, self::DROP_CONSTRAINTS => $this->dropConstraints, ]; return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } - protected function processTable(PlatformInterface $adapterPlatform = null) + protected function processTable(?PlatformInterface $adapterPlatform = null) : array { return [$this->resolveTable($this->table, $adapterPlatform)]; } - protected function processAddColumns(PlatformInterface $adapterPlatform = null) + protected function processAddColumns(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->addColumns as $column) { $sqls[] = $this->processExpression($column, $adapterPlatform); } @@ -193,22 +183,24 @@ protected function processAddColumns(PlatformInterface $adapterPlatform = null) return [$sqls]; } - protected function processChangeColumns(PlatformInterface $adapterPlatform = null) + protected function processChangeColumns(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->changeColumns as $name => $column) { $sqls[] = [ $adapterPlatform->quoteIdentifier($name), - $this->processExpression($column, $adapterPlatform) + $this->processExpression($column, $adapterPlatform), ]; } return [$sqls]; } - protected function processDropColumns(PlatformInterface $adapterPlatform = null) + protected function processDropColumns(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->dropColumns as $column) { $sqls[] = $adapterPlatform->quoteIdentifier($column); } @@ -216,9 +208,10 @@ protected function processDropColumns(PlatformInterface $adapterPlatform = null) return [$sqls]; } - protected function processAddConstraints(PlatformInterface $adapterPlatform = null) + protected function processAddConstraints(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->addConstraints as $constraint) { $sqls[] = $this->processExpression($constraint, $adapterPlatform); } @@ -226,9 +219,10 @@ protected function processAddConstraints(PlatformInterface $adapterPlatform = nu return [$sqls]; } - protected function processDropConstraints(PlatformInterface $adapterPlatform = null) + protected function processDropConstraints(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->dropConstraints as $constraint) { $sqls[] = $adapterPlatform->quoteIdentifier($constraint); } diff --git a/src/Sql/Ddl/Column/AbstractLengthColumn.php b/src/Sql/Ddl/Column/AbstractLengthColumn.php index 59e499d12e..8499fedb02 100644 --- a/src/Sql/Ddl/Column/AbstractLengthColumn.php +++ b/src/Sql/Ddl/Column/AbstractLengthColumn.php @@ -1,64 +1,49 @@ setLength($length); parent::__construct($name, $nullable, $default, $options); } - /** - * @param int $length - * @return self Provides a fluent interface - */ - public function setLength($length) + public function setLength(int $length) : self { - $this->length = (int) $length; + $this->length = $length; return $this; } - /** - * @return int - */ - public function getLength() + public function getLength() : int { return $this->length; } - /** - * @return string - */ - protected function getLengthExpression() + protected function getLengthExpression() : string { return (string) $this->length; } - /** - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $data = parent::getExpressionData(); diff --git a/src/Sql/Ddl/Column/AbstractPrecisionColumn.php b/src/Sql/Ddl/Column/AbstractPrecisionColumn.php index 3edba684c5..0a709a2227 100644 --- a/src/Sql/Ddl/Column/AbstractPrecisionColumn.php +++ b/src/Sql/Ddl/Column/AbstractPrecisionColumn.php @@ -1,86 +1,60 @@ setDecimal($decimal); parent::__construct($name, $digits, $nullable, $default, $options); } - /** - * @param int $digits - * - * @return self - */ - public function setDigits($digits) + public function setDigits(int $digits) : self { return $this->setLength($digits); } - /** - * @return int - */ - public function getDigits() + public function getDigits() : int { return $this->getLength(); } - /** - * @param int|null $decimal - * @return self Provides a fluent interface - */ - public function setDecimal($decimal) + public function setDecimal(?int $decimal) : self { $this->decimal = null === $decimal ? null : (int) $decimal; return $this; } - /** - * @return int|null - */ - public function getDecimal() + public function getDecimal() : ?int { return $this->decimal; } - /** - * {@inheritDoc} - */ - protected function getLengthExpression() + protected function getLengthExpression() : string { if ($this->decimal !== null) { return $this->length . ',' . $this->decimal; } - return $this->length; + return (string) $this->length; } } diff --git a/src/Sql/Ddl/Column/AbstractTimestampColumn.php b/src/Sql/Ddl/Column/AbstractTimestampColumn.php index e71d3791d4..d84e1f31b6 100644 --- a/src/Sql/Ddl/Column/AbstractTimestampColumn.php +++ b/src/Sql/Ddl/Column/AbstractTimestampColumn.php @@ -1,14 +1,16 @@ specification; diff --git a/src/Sql/Ddl/Column/BigInteger.php b/src/Sql/Ddl/Column/BigInteger.php index 83463381d0..554f1b0207 100644 --- a/src/Sql/Ddl/Column/BigInteger.php +++ b/src/Sql/Ddl/Column/BigInteger.php @@ -1,18 +1,16 @@ setName($name); $this->setNullable($nullable); @@ -62,49 +43,38 @@ public function __construct($name = null, $nullable = false, $default = null, ar $this->setOptions($options); } - /** - * @param string $name - * @return self Provides a fluent interface - */ - public function setName($name) + public function setName(?string $name) : self { - $this->name = (string) $name; + $this->name = $name; + return $this; } - /** - * @return null|string - */ - public function getName() + public function getName() : string { return $this->name; } - /** - * @param bool $nullable - * @return self Provides a fluent interface - */ - public function setNullable($nullable) + public function setNullable(bool $nullable) : self { - $this->isNullable = (bool) $nullable; + $this->isNullable = $nullable; + return $this; } - /** - * @return bool - */ - public function isNullable() + public function isNullable() : bool { return $this->isNullable; } /** - * @param null|string|int $default - * @return self Provides a fluent interface + * @param null|string|int $default + * @return $this */ - public function setDefault($default) + public function setDefault($default) : self { $this->default = $default; + return $this; } @@ -116,51 +86,33 @@ public function getDefault() return $this->default; } - /** - * @param array $options - * @return self Provides a fluent interface - */ - public function setOptions(array $options) + public function setOptions(array $options) : self { $this->options = $options; + return $this; } - /** - * @param string $name - * @param string $value - * @return self Provides a fluent interface - */ - public function setOption($name, $value) + public function setOption(string $name, string $value) : self { $this->options[$name] = $value; + return $this; } - /** - * @return array - */ - public function getOptions() + public function getOptions() : array { return $this->options; } - /** - * @param ConstraintInterface $constraint - * - * @return self Provides a fluent interface - */ - public function addConstraint(ConstraintInterface $constraint) + public function addConstraint(ConstraintInterface $constraint) : self { $this->constraints[] = $constraint; return $this; } - /** - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $spec = $this->specification; diff --git a/src/Sql/Ddl/Column/ColumnInterface.php b/src/Sql/Ddl/Column/ColumnInterface.php index ff38b5a080..b52d43cf52 100644 --- a/src/Sql/Ddl/Column/ColumnInterface.php +++ b/src/Sql/Ddl/Column/ColumnInterface.php @@ -1,12 +1,12 @@ getOptions(); diff --git a/src/Sql/Ddl/Column/Text.php b/src/Sql/Ddl/Column/Text.php index 53ceafaa02..06b21d64e0 100644 --- a/src/Sql/Ddl/Column/Text.php +++ b/src/Sql/Ddl/Column/Text.php @@ -1,18 +1,16 @@ setColumns($columns); @@ -49,76 +44,61 @@ public function __construct($columns = null, $name = null) $this->setName($name); } - /** - * @param string $name - * @return self Provides a fluent interface - */ - public function setName($name) + public function setName(string $name) : self { - $this->name = (string) $name; + $this->name = $name; + return $this; } - /** - * @return string - */ - public function getName() + public function getName() : string { return $this->name; } /** - * @param null|string|array $columns - * @return self Provides a fluent interface + * @param null|string|array $columns + * @return $this */ - public function setColumns($columns) + public function setColumns($columns) : self { $this->columns = (array) $columns; return $this; } - /** - * @param string $column - * @return self Provides a fluent interface - */ - public function addColumn($column) + public function addColumn(string $column) : self { $this->columns[] = $column; + return $this; } - /** - * {@inheritDoc} - */ - public function getColumns() + public function getColumns() : array { return $this->columns; } - /** - * {@inheritDoc} - */ - public function getExpressionData() + public function getExpressionData() : array { - $colCount = count($this->columns); + $colCount = count($this->columns); $newSpecTypes = []; - $values = []; - $newSpec = ''; + $values = []; + $newSpec = ''; if ($this->name) { - $newSpec .= $this->namedSpecification; - $values[] = $this->name; + $newSpec .= $this->namedSpecification; + $values[] = $this->name; $newSpecTypes[] = self::TYPE_IDENTIFIER; } $newSpec .= $this->specification; if ($colCount) { - $values = array_merge($values, $this->columns); + $values = array_merge($values, $this->columns); $newSpecParts = array_fill(0, $colCount, '%s'); $newSpecTypes = array_merge($newSpecTypes, array_fill(0, $colCount, self::TYPE_IDENTIFIER)); - $newSpec .= sprintf($this->columnSpecification, implode(', ', $newSpecParts)); + $newSpec .= sprintf($this->columnSpecification, implode(', ', $newSpecParts)); } return [[ diff --git a/src/Sql/Ddl/Constraint/Check.php b/src/Sql/Ddl/Constraint/Check.php index df3917b0e8..d8d0f2ca06 100644 --- a/src/Sql/Ddl/Constraint/Check.php +++ b/src/Sql/Ddl/Constraint/Check.php @@ -1,19 +1,21 @@ expression = $expression; $this->name = $name; } - /** - * {@inheritDoc} - */ - public function getExpressionData() + public function getExpressionData() : array { $newSpecTypes = [self::TYPE_LITERAL]; $values = [$this->expression]; diff --git a/src/Sql/Ddl/Constraint/ConstraintInterface.php b/src/Sql/Ddl/Constraint/ConstraintInterface.php index aaa092022a..40c429d53a 100644 --- a/src/Sql/Ddl/Constraint/ConstraintInterface.php +++ b/src/Sql/Ddl/Constraint/ConstraintInterface.php @@ -1,12 +1,12 @@ setName($name); $this->setColumns($columns); @@ -74,85 +70,59 @@ public function __construct( } } - /** - * @param string $referenceTable - * @return self Provides a fluent interface - */ - public function setReferenceTable($referenceTable) + public function setReferenceTable(string $referenceTable) : self { - $this->referenceTable = (string) $referenceTable; + $this->referenceTable = $referenceTable; + return $this; } - /** - * @return string - */ - public function getReferenceTable() + public function getReferenceTable() : string { return $this->referenceTable; } /** - * @param null|string|array $referenceColumn - * @return self Provides a fluent interface + * @param null|string|array $referenceColumn + * @return $this */ - public function setReferenceColumn($referenceColumn) + public function setReferenceColumn($referenceColumn) : self { $this->referenceColumn = (array) $referenceColumn; return $this; } - /** - * @return array - */ - public function getReferenceColumn() + public function getReferenceColumn() : array { return $this->referenceColumn; } - /** - * @param string $onDeleteRule - * @return self Provides a fluent interface - */ - public function setOnDeleteRule($onDeleteRule) + public function setOnDeleteRule(string $onDeleteRule) : self { - $this->onDeleteRule = (string) $onDeleteRule; + $this->onDeleteRule = $onDeleteRule; return $this; } - /** - * @return string - */ - public function getOnDeleteRule() + public function getOnDeleteRule() : string { return $this->onDeleteRule; } - /** - * @param string $onUpdateRule - * @return self Provides a fluent interface - */ - public function setOnUpdateRule($onUpdateRule) + public function setOnUpdateRule(string $onUpdateRule) : self { - $this->onUpdateRule = (string) $onUpdateRule; + $this->onUpdateRule = $onUpdateRule; return $this; } - /** - * @return string - */ - public function getOnUpdateRule() + public function getOnUpdateRule() : string { return $this->onUpdateRule; } - /** - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $data = parent::getExpressionData(); $colCount = count($this->referenceColumn); @@ -162,7 +132,8 @@ public function getExpressionData() $data[0][0] .= $this->referenceSpecification[0]; if ($colCount) { - $values = array_merge($values, $this->referenceColumn); + $values = array_merge($values, $this->referenceColumn); + $newSpecParts = array_fill(0, $colCount, '%s'); $newSpecTypes = array_merge($newSpecTypes, array_fill(0, $colCount, self::TYPE_IDENTIFIER)); @@ -171,8 +142,9 @@ public function getExpressionData() $data[0][0] .= $this->referenceSpecification[1]; - $values[] = $this->onDeleteRule; - $values[] = $this->onUpdateRule; + $values[] = $this->onDeleteRule; + $values[] = $this->onUpdateRule; + $newSpecTypes[] = self::TYPE_LITERAL; $newSpecTypes[] = self::TYPE_LITERAL; diff --git a/src/Sql/Ddl/Constraint/PrimaryKey.php b/src/Sql/Ddl/Constraint/PrimaryKey.php index 9775bb018f..07f781d789 100644 --- a/src/Sql/Ddl/Constraint/PrimaryKey.php +++ b/src/Sql/Ddl/Constraint/PrimaryKey.php @@ -1,18 +1,16 @@ 'CREATE %1$sTABLE %2$s (', self::COLUMNS => [ "\n %1\$s" => [ - [1 => '%1$s', 'combinedby' => ",\n "] - ] + [1 => '%1$s', 'combinedby' => ",\n "], + ], ], - 'combinedBy' => ",", + 'combinedBy' => ',', self::CONSTRAINTS => [ "\n %1\$s" => [ - [1 => '%1$s', 'combinedby' => ",\n "] - ] + [1 => '%1$s', 'combinedby' => ",\n "], + ], ], 'statementEnd' => '%1$s', ]; - /** - * @var string - */ + /** @var string */ protected $table = ''; /** * @param string|TableIdentifier $table - * @param bool $isTemporary + * @param bool $isTemporary */ - public function __construct($table = '', $isTemporary = false) + public function __construct($table = '', bool $isTemporary = false) { $this->table = $table; $this->setTemporary($isTemporary); } - /** - * @param bool $temporary - * @return self Provides a fluent interface - */ - public function setTemporary($temporary) + public function setTemporary(bool $temporary) : self { - $this->isTemporary = (bool) $temporary; + $this->isTemporary = $temporary; + return $this; } - /** - * @return bool - */ - public function isTemporary() + public function isTemporary() : bool { return $this->isTemporary; } - /** - * @param string $name - * @return self Provides a fluent interface - */ - public function setTable($name) + public function setTable(string $name) : self { $this->table = $name; + return $this; } - /** - * @param Column\ColumnInterface $column - * @return self Provides a fluent interface - */ - public function addColumn(Column\ColumnInterface $column) + public function addColumn(ColumnInterface $column) : self { $this->columns[] = $column; + return $this; } - /** - * @param Constraint\ConstraintInterface $constraint - * @return self Provides a fluent interface - */ - public function addConstraint(Constraint\ConstraintInterface $constraint) + public function addConstraint(ConstraintInterface $constraint) : self { $this->constraints[] = $constraint; + return $this; } - /** - * @param string|null $key - * @return array - */ - public function getRawState($key = null) + public function getRawState(?string $key = null) : array { $rawState = [ self::COLUMNS => $this->columns, @@ -131,12 +104,7 @@ public function getRawState($key = null) return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } - /** - * @param PlatformInterface $adapterPlatform - * - * @return string[] - */ - protected function processTable(PlatformInterface $adapterPlatform = null) + protected function processTable(?PlatformInterface $adapterPlatform = null) : array { return [ $this->isTemporary ? 'TEMPORARY ' : '', @@ -146,10 +114,9 @@ protected function processTable(PlatformInterface $adapterPlatform = null) /** * @param PlatformInterface $adapterPlatform - * * @return string[][]|null */ - protected function processColumns(PlatformInterface $adapterPlatform = null) + protected function processColumns(?PlatformInterface $adapterPlatform = null) { if (! $this->columns) { return; @@ -165,11 +132,10 @@ protected function processColumns(PlatformInterface $adapterPlatform = null) } /** - * @param PlatformInterface $adapterPlatform - * - * @return array|string + * @param PlatformInterface|null $adapterPlatform + * @return array|string|null */ - protected function processCombinedby(PlatformInterface $adapterPlatform = null) + protected function processCombinedby(?PlatformInterface $adapterPlatform = null) { if ($this->constraints && $this->columns) { return $this->specifications['combinedBy']; @@ -177,11 +143,10 @@ protected function processCombinedby(PlatformInterface $adapterPlatform = null) } /** - * @param PlatformInterface $adapterPlatform - * + * @param PlatformInterface|null $adapterPlatform * @return string[][]|null */ - protected function processConstraints(PlatformInterface $adapterPlatform = null) + protected function processConstraints(?PlatformInterface $adapterPlatform = null) { if (! $this->constraints) { return; @@ -196,12 +161,7 @@ protected function processConstraints(PlatformInterface $adapterPlatform = null) return [$sqls]; } - /** - * @param PlatformInterface $adapterPlatform - * - * @return string[] - */ - protected function processStatementEnd(PlatformInterface $adapterPlatform = null) + protected function processStatementEnd(?PlatformInterface $adapterPlatform = null) : array { return ["\n)"]; } diff --git a/src/Sql/Ddl/DropTable.php b/src/Sql/Ddl/DropTable.php index c42f8399cb..d7025f34fb 100644 --- a/src/Sql/Ddl/DropTable.php +++ b/src/Sql/Ddl/DropTable.php @@ -1,12 +1,12 @@ 'DROP TABLE %1$s' + self::TABLE => 'DROP TABLE %1$s', ]; - /** - * @var string - */ + /** @var string */ protected $table = ''; /** @@ -37,7 +33,7 @@ public function __construct($table = '') $this->table = $table; } - protected function processTable(PlatformInterface $adapterPlatform = null) + protected function processTable(?PlatformInterface $adapterPlatform = null) : array { return [$this->resolveTable($this->table, $adapterPlatform)]; } diff --git a/src/Sql/Ddl/Index/AbstractIndex.php b/src/Sql/Ddl/Index/AbstractIndex.php index c11efb5d5b..8f7cfa9489 100644 --- a/src/Sql/Ddl/Index/AbstractIndex.php +++ b/src/Sql/Ddl/Index/AbstractIndex.php @@ -1,12 +1,12 @@ setColumns($columns); + $this->setName($name); - $this->name = null === $name ? null : (string) $name; $this->lengths = $lengths; } /** - * * @return array of array|string should return an array in the format: * * array ( @@ -50,11 +50,11 @@ public function __construct($columns, $name = null, array $lengths = []) * ) * */ - public function getExpressionData() + public function getExpressionData() : array { $colCount = count($this->columns); $values = []; - $values[] = $this->name ?: ''; + $values[] = $this->name ?? ''; $newSpecTypes = [self::TYPE_IDENTIFIER]; $newSpecParts = []; diff --git a/src/Sql/Ddl/SqlInterface.php b/src/Sql/Ddl/SqlInterface.php index 0f8660f452..519a9690fa 100644 --- a/src/Sql/Ddl/SqlInterface.php +++ b/src/Sql/Ddl/SqlInterface.php @@ -1,12 +1,12 @@ 'DELETE FROM %1$s', - self::SPECIFICATION_WHERE => 'WHERE %1$s' + self::SPECIFICATION_WHERE => 'WHERE %1$s', ]; - /** - * @var string|TableIdentifier - */ + /** @var string|TableIdentifier */ protected $table = ''; - /** - * @var bool - */ + /** @var bool */ protected $emptyWhereProtection = true; - /** - * @var array - */ + /** @var array */ protected $set = []; - /** - * @var null|string|Where - */ - protected $where = null; + /** @var null|string|Where */ + protected $where; /** * Constructor * - * @param null|string|TableIdentifier $table + * @param null|string|TableIdentifier $table */ public function __construct($table = null) { if ($table) { $this->from($table); } + $this->where = new Where(); } /** * Create from statement * - * @param string|TableIdentifier $table - * @return self Provides a fluent interface + * @param string|TableIdentifier $table + * @return $this */ - public function from($table) + public function from($table) : self { $this->table = $table; + return $this; } - /** - * @param null $key - * - * @return mixed - */ - public function getRawState($key = null) + public function getRawState(?string $key = null) { $rawState = [ 'emptyWhereProtection' => $this->emptyWhereProtection, - 'table' => $this->table, - 'set' => $this->set, - 'where' => $this->where + 'table' => $this->table, + 'set' => $this->set, + 'where' => $this->where, ]; + return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } /** * Create where clause * - * @param Where|\Closure|string|array $predicate - * @param string $combination One of the OP_* constants from Predicate\PredicateSet - * - * @return self Provides a fluent interface + * @param Where|\Closure|string|array $predicate + * @param string $combination One of the OP_* constants from Predicate\PredicateSet + * @return $this */ - public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) + public function where($predicate, string $combination = PredicateSet::OP_AND) : self { if ($predicate instanceof Where) { $this->where = $predicate; @@ -113,60 +105,36 @@ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) return $this; } - /** - * @param PlatformInterface $platform - * @param DriverInterface|null $driver - * @param ParameterContainer|null $parameterContainer - * - * @return string - */ protected function processDelete( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : string { return sprintf( $this->specifications[static::SPECIFICATION_DELETE], $this->resolveTable($this->table, $platform, $driver, $parameterContainer) ); } - /** - * @param PlatformInterface $platform - * @param DriverInterface|null $driver - * @param ParameterContainer|null $parameterContainer - * - * @return null|string - */ protected function processWhere( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { - if ($this->where->count() == 0) { - return; + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?string { + if ($this->where->count() === 0) { + return null; } return sprintf( $this->specifications[static::SPECIFICATION_WHERE], - $this->processExpression($this->where, $platform, $driver, $parameterContainer, 'where') + $this->processExpression($this->where, $platform, $driver, $parameterContainer, self::SPECIFICATION_WHERE) ); } - /** - * Property overloading - * - * Overloads "where" only. - * - * @param string $name - * - * @return Where|null - */ - public function __get($name) + public function __get(string $name) : ?Where { - switch (strtolower($name)) { - case 'where': - return $this->where; + if (strtolower($name) === self::SPECIFICATION_WHERE) { + return $this->where; } } } diff --git a/src/Sql/Exception/ExceptionInterface.php b/src/Sql/Exception/ExceptionInterface.php index 138f7e4d62..d29824c57f 100644 --- a/src/Sql/Exception/ExceptionInterface.php +++ b/src/Sql/Exception/ExceptionInterface.php @@ -1,12 +1,12 @@ setExpression($expression); @@ -61,46 +63,39 @@ public function __construct($expression = '', $parameters = null, array $types = } } - /** - * @param $expression - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ - public function setExpression($expression) + public function setExpression(string $expression) : self { - if (! is_string($expression) || $expression == '') { + if (! is_string($expression) || $expression === '') { throw new Exception\InvalidArgumentException('Supplied expression must be a string.'); } + $this->expression = $expression; + return $this; } - /** - * @return string - */ - public function getExpression() + public function getExpression() : string { return $this->expression; } /** * @param $parameters - * @return self Provides a fluent interface + * @return $this * @throws Exception\InvalidArgumentException */ - public function setParameters($parameters) + public function setParameters($parameters) : self { if (! is_scalar($parameters) && ! is_array($parameters)) { throw new Exception\InvalidArgumentException('Expression parameters must be a scalar or array.'); } + $this->parameters = $parameters; + return $this; } - /** - * @return array - */ - public function getParameters() + public function getParameters() : array { return $this->parameters; } @@ -108,11 +103,12 @@ public function getParameters() /** * @deprecated * @param array $types - * @return self Provides a fluent interface + * @return $this */ - public function setTypes(array $types) + public function setTypes(array $types) : self { $this->types = $types; + return $this; } @@ -120,22 +116,18 @@ public function setTypes(array $types) * @deprecated * @return array */ - public function getTypes() + public function getTypes() : array { return $this->types; } - /** - * @return array - * @throws Exception\RuntimeException - */ - public function getExpressionData() + public function getExpressionData() : array { - $parameters = (is_scalar($this->parameters)) ? [$this->parameters] : $this->parameters; + $parameters = is_scalar($this->parameters) ? [$this->parameters] : $this->parameters; $parametersCount = count($parameters); - $expression = str_replace('%', '%%', $this->expression); + $expression = str_replace('%', '%%', $this->expression); - if ($parametersCount == 0) { + if ($parametersCount === 0) { return [ str_ireplace(self::PLACEHOLDER, '', $expression) ]; @@ -155,10 +147,11 @@ public function getExpressionData() foreach ($parameters as $parameter) { list($values[], $types[]) = $this->normalizeArgument($parameter, self::TYPE_VALUE); } + return [[ $expression, $values, - $types + $types, ]]; } } diff --git a/src/Sql/ExpressionInterface.php b/src/Sql/ExpressionInterface.php index 282b408683..1d19164338 100644 --- a/src/Sql/ExpressionInterface.php +++ b/src/Sql/ExpressionInterface.php @@ -1,24 +1,23 @@ 'INSERT INTO %1$s (%2$s) VALUES (%3$s)', self::SPECIFICATION_SELECT => 'INSERT INTO %1$s %2$s %3$s', ]; - /** - * @var string|TableIdentifier - */ - protected $table = null; - protected $columns = []; + /** @var string|TableIdentifier */ + protected $table; + protected $columns = []; - /** - * @var array|Select - */ - protected $select = null; + /** @var array|Select */ + protected $select; /** * Constructor * - * @param null|string|TableIdentifier $table + * @param null|string|TableIdentifier $table */ public function __construct($table = null) { @@ -61,44 +65,42 @@ public function __construct($table = null) /** * Create INTO clause * - * @param string|TableIdentifier $table - * @return self Provides a fluent interface + * @param string|TableIdentifier $table + * @return $this */ - public function into($table) + public function into($table) : self { $this->table = $table; + return $this; } - /** - * Specify columns - * - * @param array $columns - * @return self Provides a fluent interface - */ - public function columns(array $columns) + public function columns(array $columns) : self { $this->columns = array_flip($columns); + return $this; } /** * Specify values to insert * - * @param array|Select $values - * @param string $flag one of VALUES_MERGE or VALUES_SET; defaults to VALUES_SET - * @return self Provides a fluent interface + * @param array|Select $values + * @param string $flag one of VALUES_MERGE or VALUES_SET; defaults to VALUES_SET + * @return $this * @throws Exception\InvalidArgumentException */ - public function values($values, $flag = self::VALUES_SET) + public function values($values, string $flag = self::VALUES_SET) : self { if ($values instanceof Select) { - if ($flag == self::VALUES_MERGE) { + if ($flag === self::VALUES_MERGE) { throw new Exception\InvalidArgumentException( 'A Zend\Db\Sql\Select instance cannot be provided with the merge flag' ); } + $this->select = $values; + return $this; } @@ -108,15 +110,15 @@ public function values($values, $flag = self::VALUES_SET) ); } - if ($this->select && $flag == self::VALUES_MERGE) { + if ($this->select && $flag === self::VALUES_MERGE) { throw new Exception\InvalidArgumentException( 'An array of values cannot be provided with the merge flag when a Zend\Db\Sql\Select instance already ' . 'exists as the value source' ); } - if ($flag == self::VALUES_SET) { - $this->columns = $this->isAssocativeArray($values) + if ($flag === self::VALUES_SET) { + $this->columns = $this->isAssociativeArray($values) ? $values : array_combine(array_keys($this->columns), array_values($values)); } else { @@ -124,6 +126,7 @@ public function values($values, $flag = self::VALUES_SET) $this->columns[$column] = $value; } } + return $this; } @@ -135,42 +138,31 @@ public function values($values, $flag = self::VALUES_SET) * @param array $array * @return bool */ - private function isAssocativeArray(array $array) + private function isAssociativeArray(array $array) : bool { return array_keys($array) !== range(0, count($array) - 1); } - /** - * Create INTO SELECT clause - * - * @param Select $select - * @return self - */ - public function select(Select $select) + public function select(Select $select) : self { return $this->values($select); } - /** - * Get raw state - * - * @param string $key - * @return mixed - */ - public function getRawState($key = null) + public function getRawState(?string $key = null) { $rawState = [ - 'table' => $this->table, + 'table' => $this->table, 'columns' => array_keys($this->columns), - 'values' => array_values($this->columns) + 'values' => array_values($this->columns), ]; + return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } protected function processInsert( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->select) { return; @@ -191,6 +183,7 @@ protected function processInsert( if ($driver instanceof Pdo) { $column = 'c_' . $i++; } + $values[] = $driver->formatParameterName($column); $parameterContainer->offsetSet($column, $value); } else { @@ -202,6 +195,7 @@ protected function processInsert( ); } } + return sprintf( $this->specifications[static::SPECIFICATION_INSERT], $this->resolveTable($this->table, $platform, $driver, $parameterContainer), @@ -212,8 +206,8 @@ protected function processInsert( protected function processSelect( PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if (! $this->select) { return; @@ -226,36 +220,18 @@ protected function processSelect( return sprintf( $this->specifications[static::SPECIFICATION_SELECT], $this->resolveTable($this->table, $platform, $driver, $parameterContainer), - $columns ? "($columns)" : "", + $columns ? "($columns)" : '', $selectSql ); } - /** - * Overloading: variable setting - * - * Proxies to values, using VALUES_MERGE strategy - * - * @param string $name - * @param mixed $value - * @return self Provides a fluent interface - */ - public function __set($name, $value) + public function __set(string $name, $value) { $this->columns[$name] = $value; return $this; } - /** - * Overloading: variable unset - * - * Proxies to values and columns - * - * @param string $name - * @throws Exception\InvalidArgumentException - * @return void - */ - public function __unset($name) + public function __unset(string $name) { if (! array_key_exists($name, $this->columns)) { throw new Exception\InvalidArgumentException( @@ -266,35 +242,19 @@ public function __unset($name) unset($this->columns[$name]); } - /** - * Overloading: variable isset - * - * Proxies to columns; does a column of that name exist? - * - * @param string $name - * @return bool - */ - public function __isset($name) + public function __isset(string $name) { return array_key_exists($name, $this->columns); } - /** - * Overloading: variable retrieval - * - * Retrieves value by column name - * - * @param string $name - * @throws Exception\InvalidArgumentException - * @return mixed - */ - public function __get($name) + public function __get(string $name) { if (! array_key_exists($name, $this->columns)) { throw new Exception\InvalidArgumentException( 'The key ' . $name . ' was not found in this objects column list' ); } + return $this->columns[$name]; } } diff --git a/src/Sql/Join.php b/src/Sql/Join.php index 12e8599663..8f6ad332bb 100644 --- a/src/Sql/Join.php +++ b/src/Sql/Join.php @@ -1,14 +1,22 @@ position = 0; } - /** - * Rewind iterator. - */ - public function rewind() - { - $this->position = 0; - } - - /** - * Return current join specification. - * - * @return array - */ - public function current() + public function current() : array { return $this->joins[$this->position]; } - /** - * Return the current iterator index. - * - * @return int - */ - public function key() + public function key() : int { return $this->position; } - /** - * Advance to the next JOIN specification. - */ - public function next() + public function next() : void { ++$this->position; } - /** - * Is the iterator at a valid position? - * - * @return bool - */ - public function valid() + public function valid() : bool { return isset($this->joins[$this->position]); } - /** - * @return array - */ - public function getJoins() + public function getJoins() : array { return $this->joins; } @@ -115,10 +83,10 @@ public function getJoins() * of column names, or (a) specification(s) such as SQL_STAR representing * the columns to join. * @param string $type The JOIN type to use; see the JOIN_* constants. - * @return self Provides a fluent interface + * @return $this * @throws Exception\InvalidArgumentException for invalid $name values. */ - public function join($name, $on, $columns = [Select::SQL_STAR], $type = Join::JOIN_INNER) + public function join($name, $on, $columns = [Select::SQL_STAR], string $type = Join::JOIN_INNER) : self { if (is_array($name) && (! is_string(key($name)) || count($name) !== 1)) { throw new Exception\InvalidArgumentException( @@ -134,29 +102,20 @@ public function join($name, $on, $columns = [Select::SQL_STAR], $type = Join::JO 'name' => $name, 'on' => $on, 'columns' => $columns, - 'type' => $type ? $type : Join::JOIN_INNER + 'type' => $type ? $type : self::JOIN_INNER, ]; return $this; } - /** - * Reset to an empty list of JOIN specifications. - * - * @return self Provides a fluent interface - */ - public function reset() + public function reset() : self { $this->joins = []; + return $this; } - /** - * Get count of attached predicates - * - * @return int - */ - public function count() + public function count() : int { return count($this->joins); } diff --git a/src/Sql/Literal.php b/src/Sql/Literal.php index a04b0f16de..042c67ac8c 100644 --- a/src/Sql/Literal.php +++ b/src/Sql/Literal.php @@ -1,56 +1,43 @@ literal = $literal; } - /** - * @param string $literal - * @return self Provides a fluent interface - */ - public function setLiteral($literal) + public function setLiteral(string $literal) : self { $this->literal = $literal; + return $this; } - /** - * @return string - */ - public function getLiteral() + public function getLiteral() : string { return $this->literal; } - /** - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { return [[ str_replace('%', '%%', $this->literal), [], - [] + [], ]]; } } diff --git a/src/Sql/Platform/AbstractPlatform.php b/src/Sql/Platform/AbstractPlatform.php index 6d0b040bc6..7efc815f40 100644 --- a/src/Sql/Platform/AbstractPlatform.php +++ b/src/Sql/Platform/AbstractPlatform.php @@ -1,36 +1,29 @@ subject = $subject; @@ -38,13 +31,7 @@ public function setSubject($subject) return $this; } - /** - * @param string $type - * @param PlatformDecoratorInterface $decorator - * - * @return void - */ - public function setTypeDecorator($type, PlatformDecoratorInterface $decorator) + public function setTypeDecorator(string $type, PlatformDecoratorInterface $decorator) : void { $this->decorators[$type] = $decorator; } @@ -67,22 +54,17 @@ public function getTypeDecorator($subject) } /** - * @return array|PlatformDecoratorInterface[] + * @return array|PlatformDecoratorInterface */ public function getDecorators() { return $this->decorators; } - /** - * {@inheritDoc} - * - * @throws Exception\RuntimeException - */ public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) { if (! $this->subject instanceof PreparableSqlInterface) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'The subject does not appear to implement Zend\Db\Sql\PreparableSqlInterface, thus calling ' . 'prepareStatement() has no effect' ); @@ -93,15 +75,10 @@ public function prepareStatement(AdapterInterface $adapter, StatementContainerIn return $statementContainer; } - /** - * {@inheritDoc} - * - * @throws Exception\RuntimeException - */ - public function getSqlString(PlatformInterface $adapterPlatform = null) + public function getSqlString(?PlatformInterface $adapterPlatform = null) : string { if (! $this->subject instanceof SqlInterface) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'The subject does not appear to implement Zend\Db\Sql\SqlInterface, thus calling ' . 'prepareStatement() has no effect' ); diff --git a/src/Sql/Platform/IbmDb2/IbmDb2.php b/src/Sql/Platform/IbmDb2/IbmDb2.php index 35ddc2c768..5e4b31c914 100644 --- a/src/Sql/Platform/IbmDb2/IbmDb2.php +++ b/src/Sql/Platform/IbmDb2/IbmDb2.php @@ -1,23 +1,21 @@ setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); + $this->setTypeDecorator(Select::class, $selectDecorator ?? new SelectDecorator()); } } diff --git a/src/Sql/Platform/IbmDb2/SelectDecorator.php b/src/Sql/Platform/IbmDb2/SelectDecorator.php index f1988c09b5..7a57be6f52 100644 --- a/src/Sql/Platform/IbmDb2/SelectDecorator.php +++ b/src/Sql/Platform/IbmDb2/SelectDecorator.php @@ -1,12 +1,12 @@ isSelectContainDistinct; } - /** - * @param boolean $isSelectContainDistinct - */ - public function setIsSelectContainDistinct($isSelectContainDistinct) + public function setIsSelectContainDistinct(bool $isSelectContainDistinct) : void { $this->isSelectContainDistinct = $isSelectContainDistinct; } @@ -57,33 +50,25 @@ public function setSubject($select) $this->subject = $select; } - /** - * @return bool - */ - public function getSupportsLimitOffset() + public function getSupportsLimitOffset() : bool { return $this->supportsLimitOffset; } - /** - * @param bool $supportsLimitOffset - */ - public function setSupportsLimitOffset($supportsLimitOffset) + public function setSupportsLimitOffset(bool $supportsLimitOffset) : void { $this->supportsLimitOffset = $supportsLimitOffset; } - /** - * @see Select::renderTable - */ - protected function renderTable($table, $alias = null) + protected function renderTable(string $table, ?string $alias = null) : string { return $table . ' ' . $alias; } - protected function localizeVariables() + protected function localizeVariables() : void { parent::localizeVariables(); + // set specifications unset($this->specifications[self::LIMIT]); unset($this->specifications[self::OFFSET]); @@ -91,20 +76,13 @@ protected function localizeVariables() $this->specifications['LIMITOFFSET'] = null; } - /** - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * @param array $sqls - * @param array $parameters - */ protected function processLimitOffset( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null, - &$sqls, - &$parameters - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null, + array &$sqls, + array &$parameters + ) : void { if ($this->limit === null && $this->offset === null) { return; } @@ -112,26 +90,30 @@ protected function processLimitOffset( if ($this->supportsLimitOffset) { // Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET $limit = (int) $this->limit; + if (! $limit) { return; } $offset = (int) $this->offset; + if ($offset) { - array_push($sqls, sprintf("LIMIT %s OFFSET %s", $limit, $offset)); + $sqls[] = sprintf('LIMIT %s OFFSET %s', $limit, $offset); return; } - array_push($sqls, sprintf("LIMIT %s", $limit)); + $sqls[] = sprintf('LIMIT %s', $limit); + return; } $selectParameters = $parameters[self::SELECT]; $starSuffix = $platform->getIdentifierSeparator() . self::SQL_STAR; + foreach ($selectParameters[0] as $i => $columnParameters) { - if ($columnParameters[0] == self::SQL_STAR - || (isset($columnParameters[1]) && $columnParameters[1] == self::SQL_STAR) + if ($columnParameters[0] === self::SQL_STAR + || (isset($columnParameters[1]) && $columnParameters[1] === self::SQL_STAR) || strpos($columnParameters[0], $starSuffix) ) { $selectParameters[0] = [[self::SQL_STAR]]; @@ -156,16 +138,16 @@ protected function processLimitOffset( if ($parameterContainer) { // create bottom part of query, with offset and limit using row_number - $limitParamName = $driver->formatParameterName('limit'); - $offsetParamName = $driver->formatParameterName('offset'); + $limitParamName = $driver->formatParameterName('limit'); + $offsetParamName = $driver->formatParameterName('offset'); - array_push($sqls, sprintf( - // @codingStandardsIgnoreStart - ") AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %s AND %s", + $sqls[] = sprintf( + // @codingStandardsIgnoreStart + ') AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %s AND %s', // @codingStandardsIgnoreEnd $offsetParamName, $limitParamName - )); + ); if ((int) $this->offset > 0) { $parameterContainer->offsetSet('offset', (int) $this->offset + 1); @@ -181,17 +163,18 @@ protected function processLimitOffset( $offset = (int) $this->offset; } - array_push($sqls, sprintf( - // @codingStandardsIgnoreStart - ") AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %d AND %d", + $sqls[] = sprintf( + // @codingStandardsIgnoreStart + ') AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %d AND %d', // @codingStandardsIgnoreEnd $offset, (int) $this->limit + (int) $this->offset - )); + ); } if (isset($sqls[self::ORDER])) { $orderBy = $sqls[self::ORDER]; + unset($sqls[self::ORDER]); } else { $orderBy = ''; diff --git a/src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php b/src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php index ea3176532d..b392a679b6 100644 --- a/src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php +++ b/src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php @@ -1,28 +1,33 @@ 0, 'zerofill' => 1, @@ -38,9 +43,9 @@ class AlterTableDecorator extends AlterTable implements PlatformDecoratorInterfa /** * @param AlterTable $subject - * @return self Provides a fluent interface + * @return $this */ - public function setSubject($subject) + public function setSubject($subject) : self { $this->subject = $subject; @@ -51,7 +56,7 @@ public function setSubject($subject) * @param string $sql * @return array */ - protected function getSqlInsertOffsets($sql) + protected function getSqlInsertOffsets(string $sql) : array { $sqlLength = strlen($sql); $insertStart = []; @@ -81,11 +86,7 @@ protected function getSqlInsertOffsets($sql) return $insertStart; } - /** - * @param PlatformInterface $adapterPlatform - * @return array - */ - protected function processAddColumns(PlatformInterface $adapterPlatform = null) + protected function processAddColumns(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; @@ -106,11 +107,11 @@ protected function processAddColumns(PlatformInterface $adapterPlatform = null) switch ($this->normalizeColumnOption($coName)) { case 'unsigned': $insert = ' UNSIGNED'; - $j = 0; + $j = 0; break; case 'zerofill': $insert = ' ZEROFILL'; - $j = 0; + $j = 0; break; case 'identity': case 'serial': @@ -120,43 +121,43 @@ protected function processAddColumns(PlatformInterface $adapterPlatform = null) break; case 'comment': $insert = ' COMMENT ' . $adapterPlatform->quoteValue($coValue); - $j = 2; + $j = 2; break; case 'columnformat': case 'format': $insert = ' COLUMN_FORMAT ' . strtoupper($coValue); - $j = 2; + $j = 2; break; case 'storage': $insert = ' STORAGE ' . strtoupper($coValue); - $j = 2; + $j = 2; break; case 'after': $insert = ' AFTER ' . $adapterPlatform->quoteIdentifier($coValue); - $j = 2; + $j = 2; } if ($insert) { - $j = isset($j) ? $j : 0; - $sql = substr_replace($sql, $insert, $insertStart[$j], 0); + $j = isset($j) ? $j : 0; + $sql = substr_replace($sql, $insert, $insertStart[$j], 0); $insertStartCount = count($insertStart); + for (; $j < $insertStartCount; ++$j) { $insertStart[$j] += strlen($insert); } } } + $sqls[$i] = $sql; } + return [$sqls]; } - /** - * @param PlatformInterface $adapterPlatform - * @return array - */ - protected function processChangeColumns(PlatformInterface $adapterPlatform = null) + protected function processChangeColumns(?PlatformInterface $adapterPlatform = null) : array { $sqls = []; + foreach ($this->changeColumns as $name => $column) { $sql = $this->processExpression($column, $adapterPlatform); $insertStart = $this->getSqlInsertOffsets($sql); @@ -210,6 +211,7 @@ protected function processChangeColumns(PlatformInterface $adapterPlatform = nul } } } + $sqls[] = [ $adapterPlatform->quoteIdentifier($name), $sql @@ -219,24 +221,12 @@ protected function processChangeColumns(PlatformInterface $adapterPlatform = nul return [$sqls]; } - /** - * @param string $name - * - * @return string - */ - private function normalizeColumnOption($name) + private function normalizeColumnOption(string $name) : string { return strtolower(str_replace(['-', '_', ' '], '', $name)); } - /** - * - * @param string $columnA - * @param string $columnB - * - * @return int - */ - private function compareColumnOptions($columnA, $columnB) + private function compareColumnOptions(string $columnA, string $columnB) : int { $columnA = $this->normalizeColumnOption($columnA); $columnA = isset($this->columnOptionSortOrder[$columnA]) diff --git a/src/Sql/Platform/Mysql/Ddl/CreateTableDecorator.php b/src/Sql/Platform/Mysql/Ddl/CreateTableDecorator.php index fa8815a9a1..2acea2b82c 100644 --- a/src/Sql/Platform/Mysql/Ddl/CreateTableDecorator.php +++ b/src/Sql/Platform/Mysql/Ddl/CreateTableDecorator.php @@ -1,28 +1,32 @@ 0, 'zerofill' => 1, @@ -37,21 +41,16 @@ class CreateTableDecorator extends CreateTable implements PlatformDecoratorInter /** * @param CreateTable $subject - * - * @return self Provides a fluent interface + * @return $this */ - public function setSubject($subject) + public function setSubject($subject) : self { $this->subject = $subject; return $this; } - /** - * @param string $sql - * @return array - */ - protected function getSqlInsertOffsets($sql) + protected function getSqlInsertOffsets(string $sql) : array { $sqlLength = strlen($sql); $insertStart = []; @@ -81,10 +80,7 @@ protected function getSqlInsertOffsets($sql) return $insertStart; } - /** - * {@inheritDoc} - */ - protected function processColumns(PlatformInterface $platform = null) + protected function processColumns(?PlatformInterface $platform = null) { if (! $this->columns) { return; @@ -109,37 +105,38 @@ protected function processColumns(PlatformInterface $platform = null) switch ($this->normalizeColumnOption($coName)) { case 'unsigned': $insert = ' UNSIGNED'; - $j = 0; + $j = 0; break; case 'zerofill': $insert = ' ZEROFILL'; - $j = 0; + $j = 0; break; case 'identity': case 'serial': case 'autoincrement': $insert = ' AUTO_INCREMENT'; - $j = 1; + $j = 1; break; case 'comment': $insert = ' COMMENT ' . $platform->quoteValue($coValue); - $j = 2; + $j = 2; break; case 'columnformat': case 'format': $insert = ' COLUMN_FORMAT ' . strtoupper($coValue); - $j = 2; + $j = 2; break; case 'storage': $insert = ' STORAGE ' . strtoupper($coValue); - $j = 2; + $j = 2; break; } if ($insert) { - $j = isset($j) ? $j : 0; - $sql = substr_replace($sql, $insert, $insertStart[$j], 0); + $j = isset($j) ? $j : 0; + $sql = substr_replace($sql, $insert, $insertStart[$j], 0); $insertStartCount = count($insertStart); + for (; $j < $insertStartCount; ++$j) { $insertStart[$j] += strlen($insert); } @@ -152,32 +149,22 @@ protected function processColumns(PlatformInterface $platform = null) return [$sqls]; } - /** - * @param string $name - * - * @return string - */ - private function normalizeColumnOption($name) + private function normalizeColumnOption(string $name) : string { return strtolower(str_replace(['-', '_', ' '], '', $name)); } - /** - * - * @param string $columnA - * @param string $columnB - * - * @return int - */ - private function compareColumnOptions($columnA, $columnB) + private function compareColumnOptions(string $columnA, string $columnB) : int { $columnA = $this->normalizeColumnOption($columnA); $columnA = isset($this->columnOptionSortOrder[$columnA]) - ? $this->columnOptionSortOrder[$columnA] : count($this->columnOptionSortOrder); + ? $this->columnOptionSortOrder[$columnA] + : count($this->columnOptionSortOrder); $columnB = $this->normalizeColumnOption($columnB); $columnB = isset($this->columnOptionSortOrder[$columnB]) - ? $this->columnOptionSortOrder[$columnB] : count($this->columnOptionSortOrder); + ? $this->columnOptionSortOrder[$columnB] + : count($this->columnOptionSortOrder); return $columnA - $columnB; } diff --git a/src/Sql/Platform/Mysql/Mysql.php b/src/Sql/Platform/Mysql/Mysql.php index 3e1611b87f..321aa2229f 100644 --- a/src/Sql/Platform/Mysql/Mysql.php +++ b/src/Sql/Platform/Mysql/Mysql.php @@ -1,22 +1,27 @@ setTypeDecorator('Zend\Db\Sql\Select', new SelectDecorator()); - $this->setTypeDecorator('Zend\Db\Sql\Ddl\CreateTable', new Ddl\CreateTableDecorator()); - $this->setTypeDecorator('Zend\Db\Sql\Ddl\AlterTable', new Ddl\AlterTableDecorator()); + $this->setTypeDecorator(Select::class, new SelectDecorator()); + $this->setTypeDecorator(CreateTable::class, new CreateTableDecorator()); + $this->setTypeDecorator(AlterTable::class, new AlterTableDecorator()); } } diff --git a/src/Sql/Platform/Mysql/SelectDecorator.php b/src/Sql/Platform/Mysql/SelectDecorator.php index 3ef7655ce8..a703efa734 100644 --- a/src/Sql/Platform/Mysql/SelectDecorator.php +++ b/src/Sql/Platform/Mysql/SelectDecorator.php @@ -1,12 +1,12 @@ subject = $select; } - protected function localizeVariables() + protected function localizeVariables() : void { parent::localizeVariables(); if ($this->limit === null && $this->offset !== null) { @@ -39,19 +37,22 @@ protected function localizeVariables() } protected function processLimit( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->limit === null && $this->offset !== null) { return ['']; } + if ($this->limit === null) { return; } + if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); + return [$driver->formatParameterName($paramPrefix . 'limit')]; } @@ -59,9 +60,9 @@ protected function processLimit( } protected function processOffset( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->offset === null) { return; @@ -69,6 +70,7 @@ protected function processOffset( if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; $parameterContainer->offsetSet($paramPrefix . 'offset', $this->offset, ParameterContainer::TYPE_INTEGER); + return [$driver->formatParameterName($paramPrefix . 'offset')]; } diff --git a/src/Sql/Platform/Oracle/Oracle.php b/src/Sql/Platform/Oracle/Oracle.php index bf1dc31df5..6f0163e8b3 100644 --- a/src/Sql/Platform/Oracle/Oracle.php +++ b/src/Sql/Platform/Oracle/Oracle.php @@ -1,20 +1,21 @@ setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); + $this->setTypeDecorator(Select::class, $selectDecorator ?? new SelectDecorator()); } } diff --git a/src/Sql/Platform/Oracle/SelectDecorator.php b/src/Sql/Platform/Oracle/SelectDecorator.php index 9961b4dcf9..ae774778a8 100644 --- a/src/Sql/Platform/Oracle/SelectDecorator.php +++ b/src/Sql/Platform/Oracle/SelectDecorator.php @@ -1,12 +1,12 @@ subject = $select; } - /** - * @see \Zend\Db\Sql\Select::renderTable - */ - protected function renderTable($table, $alias = null) + protected function renderTable(string $table, ?string $alias = null) : string { return $table . ($alias ? ' ' . $alias : ''); } - protected function localizeVariables() + protected function localizeVariables() : void { parent::localizeVariables(); unset($this->specifications[self::LIMIT]); @@ -47,21 +45,13 @@ protected function localizeVariables() $this->specifications['LIMITOFFSET'] = null; } - /** - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * @param array $sqls - * @param array $parameters - * @return null - */ protected function processLimitOffset( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null, - &$sqls = [], - &$parameters = [] - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null, + array &$sqls = [], + array &$parameters = [] + ) : void { if ($this->limit === null && $this->offset === null) { return; } @@ -70,8 +60,8 @@ protected function processLimitOffset( $starSuffix = $platform->getIdentifierSeparator() . self::SQL_STAR; foreach ($selectParameters[0] as $i => $columnParameters) { - if ($columnParameters[0] == self::SQL_STAR - || (isset($columnParameters[1]) && $columnParameters[1] == self::SQL_STAR) + if ($columnParameters[0] === self::SQL_STAR + || (isset($columnParameters[1]) && $columnParameters[1] === self::SQL_STAR) || strpos($columnParameters[0], $starSuffix) ) { $selectParameters[0] = [[self::SQL_STAR]]; @@ -97,10 +87,7 @@ protected function processLimitOffset( $number = $this->processInfo['subselectCount'] ? $this->processInfo['subselectCount'] : ''; if ($this->limit === null) { - array_push( - $sqls, - ') b ) WHERE b_rownum > (:offset' . $number . ')' - ); + $sqls[] = ') b ) WHERE b_rownum > (:offset' . $number . ')'; $parameterContainer->offsetSet( 'offset' . $number, $this->offset, @@ -108,16 +95,13 @@ protected function processLimitOffset( ); } else { // create bottom part of query, with offset and limit using row_number - array_push( - $sqls, - ') b WHERE rownum <= (:offset' + $sqls[] = ') b WHERE rownum <= (:offset' . $number . '+:limit' . $number . ')) WHERE b_rownum >= (:offset' . $number - . ' + 1)' - ); + . ' + 1)'; $parameterContainer->offsetSet( 'offset' . $number, $this->offset, @@ -129,21 +113,19 @@ protected function processLimitOffset( $parameterContainer::TYPE_INTEGER ); } + $this->processInfo['subselectCount']++; } else { if ($this->limit === null) { - array_push($sqls, ') b ) WHERE b_rownum > (' . (int) $this->offset . ')'); + $sqls[] = ') b ) WHERE b_rownum > (' . (int) $this->offset . ')'; } else { - array_push( - $sqls, - ') b WHERE rownum <= (' + $sqls[] = ') b WHERE rownum <= (' . (int) $this->offset . '+' . (int) $this->limit . ')) WHERE b_rownum >= (' . (int) $this->offset - . ' + 1)' - ); + . ' + 1)'; } } diff --git a/src/Sql/Platform/Platform.php b/src/Sql/Platform/Platform.php index e702caac46..0d47d9abb4 100644 --- a/src/Sql/Platform/Platform.php +++ b/src/Sql/Platform/Platform.php @@ -1,42 +1,47 @@ defaultPlatform = $adapter->getPlatform(); - $mySqlPlatform = new Mysql\Mysql(); - $sqlServerPlatform = new SqlServer\SqlServer(); - $oraclePlatform = new Oracle\Oracle(); - $ibmDb2Platform = new IbmDb2\IbmDb2(); - $sqlitePlatform = new Sqlite\Sqlite(); + $mySqlPlatform = new Mysql(); + $sqlServerPlatform = new SqlServer(); + $oraclePlatform = new Oracle(); + $ibmDb2Platform = new IbmDb2(); + $sqlitePlatform = new Sqlite(); $this->decorators['mysql'] = $mySqlPlatform->getDecorators(); $this->decorators['sqlserver'] = $sqlServerPlatform->getDecorators(); @@ -45,13 +50,11 @@ public function __construct(AdapterInterface $adapter) $this->decorators['sqlite'] = $sqlitePlatform->getDecorators(); } - /** - * @param string $type - * @param PlatformDecoratorInterface $decorator - * @param AdapterInterface|PlatformInterface $adapterOrPlatform - */ - public function setTypeDecorator($type, PlatformDecoratorInterface $decorator, $adapterOrPlatform = null) - { + public function setTypeDecorator( + string $type, + PlatformDecoratorInterface $decorator, + $adapterOrPlatform = null + ) : void { $platformName = $this->resolvePlatformName($adapterOrPlatform); $this->decorators[$platformName][$type] = $decorator; } @@ -69,6 +72,7 @@ public function getTypeDecorator($subject, $adapterOrPlatform = null) foreach ($this->decorators[$platformName] as $type => $decorator) { if ($subject instanceof $type && is_a($decorator, $type, true)) { $decorator->setSubject($subject); + return $decorator; } } @@ -78,23 +82,19 @@ public function getTypeDecorator($subject, $adapterOrPlatform = null) } /** - * @return array|PlatformDecoratorInterface[] + * @return array|PlatformDecoratorInterface */ public function getDecorators() { $platformName = $this->resolvePlatformName($this->getDefaultPlatform()); + return $this->decorators[$platformName]; } - /** - * {@inheritDoc} - * - * @throws Exception\RuntimeException - */ public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) { if (! $this->subject instanceof PreparableSqlInterface) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'The subject does not appear to implement Zend\Db\Sql\PreparableSqlInterface, thus calling ' . 'prepareStatement() has no effect' ); @@ -105,15 +105,10 @@ public function prepareStatement(AdapterInterface $adapter, StatementContainerIn return $statementContainer; } - /** - * {@inheritDoc} - * - * @throws Exception\RuntimeException - */ - public function getSqlString(PlatformInterface $adapterPlatform = null) + public function getSqlString(?PlatformInterface $adapterPlatform = null) : string { if (! $this->subject instanceof SqlInterface) { - throw new Exception\RuntimeException( + throw new RuntimeException( 'The subject does not appear to implement Zend\Db\Sql\SqlInterface, thus calling ' . 'prepareStatement() has no effect' ); @@ -127,16 +122,15 @@ public function getSqlString(PlatformInterface $adapterPlatform = null) protected function resolvePlatformName($adapterOrPlatform) { $platformName = $this->resolvePlatform($adapterOrPlatform)->getName(); + return str_replace([' ', '_'], '', strtolower($platformName)); } /** * @param null|PlatformInterface|AdapterInterface $adapterOrPlatform - * * @return PlatformInterface - * - * @throws Exception\InvalidArgumentException + * @throws InvalidArgumentException */ - protected function resolvePlatform($adapterOrPlatform) + protected function resolvePlatform($adapterOrPlatform) : PlatformInterface { if (! $adapterOrPlatform) { return $this->getDefaultPlatform(); @@ -150,22 +144,21 @@ protected function resolvePlatform($adapterOrPlatform) return $adapterOrPlatform; } - throw new Exception\InvalidArgumentException(sprintf( + throw new InvalidArgumentException(sprintf( '$adapterOrPlatform should be null, %s, or %s', - 'Zend\Db\Adapter\AdapterInterface', - 'Zend\Db\Adapter\Platform\PlatformInterface' + AdapterInterface::class, + PlatformInterface::class )); } /** * @return PlatformInterface - * - * @throws Exception\RuntimeException + * @throws RuntimeException */ - protected function getDefaultPlatform() + protected function getDefaultPlatform() : PlatformInterface { if (! $this->defaultPlatform) { - throw new Exception\RuntimeException('$this->defaultPlatform was not set'); + throw new RuntimeException('$this->defaultPlatform was not set'); } return $this->defaultPlatform; diff --git a/src/Sql/Platform/PlatformDecoratorInterface.php b/src/Sql/Platform/PlatformDecoratorInterface.php index f06c4340d2..4bf490a6f7 100644 --- a/src/Sql/Platform/PlatformDecoratorInterface.php +++ b/src/Sql/Platform/PlatformDecoratorInterface.php @@ -1,19 +1,18 @@ subject = $subject; + return $this; } - /** - * @param PlatformInterface $adapterPlatform - * @return array - */ - protected function processTable(PlatformInterface $adapterPlatform = null) + protected function processTable(?PlatformInterface $adapterPlatform = null) : array { $table = ($this->isTemporary ? '#' : '') . ltrim($this->table, '#'); + return [ '', $adapterPlatform->quoteIdentifier($table), diff --git a/src/Sql/Platform/SqlServer/SelectDecorator.php b/src/Sql/Platform/SqlServer/SelectDecorator.php index 642d10b563..afd2adac74 100644 --- a/src/Sql/Platform/SqlServer/SelectDecorator.php +++ b/src/Sql/Platform/SqlServer/SelectDecorator.php @@ -1,12 +1,12 @@ subject = $select; } - protected function localizeVariables() + protected function localizeVariables() : void { parent::localizeVariables(); // set specifications @@ -40,21 +43,13 @@ protected function localizeVariables() $this->specifications['LIMITOFFSET'] = null; } - /** - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * @param $sqls - * @param $parameters - * @return null - */ protected function processLimitOffset( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null, + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null, &$sqls, &$parameters - ) { + ) : void { if ($this->limit === null && $this->offset === null) { return; } @@ -71,8 +66,8 @@ protected function processLimitOffset( $starSuffix = $platform->getIdentifierSeparator() . self::SQL_STAR; foreach ($selectParameters[0] as $i => $columnParameters) { - if ($columnParameters[0] == self::SQL_STAR - || (isset($columnParameters[1]) && $columnParameters[1] == self::SQL_STAR) + if ($columnParameters[0] === self::SQL_STAR + || (isset($columnParameters[1]) && $columnParameters[1] === self::SQL_STAR) || strpos($columnParameters[0], $starSuffix) ) { $selectParameters[0] = [[self::SQL_STAR]]; @@ -96,17 +91,17 @@ protected function processLimitOffset( $offsetParamName = $driver->formatParameterName('offset'); $offsetForSumParamName = $driver->formatParameterName('offsetForSum'); // @codingStandardsIgnoreStart - array_push($sqls, ') AS [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION] WHERE [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION].[__ZEND_ROW_NUMBER] BETWEEN ' - . $offsetParamName . '+1 AND ' . $limitParamName . '+' . $offsetForSumParamName); + $sqls[] = ') AS [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION] WHERE [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION].[__ZEND_ROW_NUMBER] BETWEEN ' + . $offsetParamName . '+1 AND ' . $limitParamName . '+' . $offsetForSumParamName; // @codingStandardsIgnoreEnd $parameterContainer->offsetSet('offset', $this->offset); $parameterContainer->offsetSet('limit', $this->limit); $parameterContainer->offsetSetReference('offsetForSum', 'offset'); } else { // @codingStandardsIgnoreStart - array_push($sqls, ') AS [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION] WHERE [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION].[__ZEND_ROW_NUMBER] BETWEEN ' + $sqls[] = ') AS [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION] WHERE [ZEND_SQL_SERVER_LIMIT_OFFSET_EMULATION].[__ZEND_ROW_NUMBER] BETWEEN ' . (int) $this->offset . '+1 AND ' - . (int) $this->limit . '+' . (int) $this->offset); + . (int) $this->limit . '+' . (int) $this->offset; // @codingStandardsIgnoreEnd } diff --git a/src/Sql/Platform/SqlServer/SqlServer.php b/src/Sql/Platform/SqlServer/SqlServer.php index 0dc383d0be..94539f201d 100644 --- a/src/Sql/Platform/SqlServer/SqlServer.php +++ b/src/Sql/Platform/SqlServer/SqlServer.php @@ -1,21 +1,24 @@ setTypeDecorator('Zend\Db\Sql\Select', ($selectDecorator) ?: new SelectDecorator()); - $this->setTypeDecorator('Zend\Db\Sql\Ddl\CreateTable', new Ddl\CreateTableDecorator()); + $this->setTypeDecorator(Select::class, $selectDecorator ?? new SelectDecorator()); + $this->setTypeDecorator(CreateTable::class, new CreateTableDecorator()); } } diff --git a/src/Sql/Platform/Sqlite/SelectDecorator.php b/src/Sql/Platform/Sqlite/SelectDecorator.php index a1b3d6c508..9a1a5c88c6 100644 --- a/src/Sql/Platform/Sqlite/SelectDecorator.php +++ b/src/Sql/Platform/Sqlite/SelectDecorator.php @@ -1,12 +1,12 @@ subject = $select; return $this; } - /** - * {@inheritDoc} - */ - protected function localizeVariables() + protected function localizeVariables() : void { parent::localizeVariables(); $this->specifications[self::COMBINE] = '%1$s %2$s'; } - /** - * {@inheritDoc} - */ protected function processStatementStart( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { - return ''; + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : array { + return []; } protected function processLimit( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->limit === null && $this->offset !== null) { return ['']; } + if ($this->limit === null) { return; } + if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); + return [$driver->formatParameterName('limit')]; } @@ -76,9 +69,9 @@ protected function processLimit( } protected function processOffset( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->offset === null) { return; @@ -92,14 +85,11 @@ protected function processOffset( return [$this->offset]; } - /** - * {@inheritDoc} - */ protected function processStatementEnd( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { - return ''; + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : array { + return []; } } diff --git a/src/Sql/Platform/Sqlite/Sqlite.php b/src/Sql/Platform/Sqlite/Sqlite.php index 00712dded1..bdda4d5fa6 100644 --- a/src/Sql/Platform/Sqlite/Sqlite.php +++ b/src/Sql/Platform/Sqlite/Sqlite.php @@ -1,25 +1,21 @@ setTypeDecorator('Zend\Db\Sql\Select', new SelectDecorator()); + $this->setTypeDecorator(Select::class, new SelectDecorator()); } } diff --git a/src/Sql/Predicate/Between.php b/src/Sql/Predicate/Between.php index 4b1545eb08..efc0cb4f2c 100644 --- a/src/Sql/Predicate/Between.php +++ b/src/Sql/Predicate/Between.php @@ -1,12 +1,12 @@ setIdentifier($identifier); } + if ($minValue !== null) { $this->setMinValue($minValue); } + if ($maxValue !== null) { $this->setMaxValue($maxValue); } } - /** - * Set identifier for comparison - * - * @param string $identifier - * @return self Provides a fluent interface - */ - public function setIdentifier($identifier) + public function setIdentifier(string $identifier) : self { $this->identifier = $identifier; + return $this; } - /** - * Get identifier of comparison - * - * @return null|string - */ - public function getIdentifier() + public function getIdentifier() : ?string { return $this->identifier; } @@ -63,12 +55,13 @@ public function getIdentifier() /** * Set minimum boundary for comparison * - * @param int|float|string $minValue - * @return self Provides a fluent interface + * @param int|float|string $minValue + * @return $this */ - public function setMinValue($minValue) + public function setMinValue($minValue) : self { $this->minValue = $minValue; + return $this; } @@ -85,12 +78,13 @@ public function getMinValue() /** * Set maximum boundary for comparison * - * @param int|float|string $maxValue - * @return self Provides a fluent interface + * @param int|float|string $maxValue + * @return $this */ - public function setMaxValue($maxValue) + public function setMaxValue($maxValue) : self { $this->maxValue = $maxValue; + return $this; } @@ -104,34 +98,19 @@ public function getMaxValue() return $this->maxValue; } - /** - * Set specification string to use in forming SQL predicate - * - * @param string $specification - * @return self Provides a fluent interface - */ - public function setSpecification($specification) + public function setSpecification(string $specification) : self { $this->specification = $specification; + return $this; } - /** - * Get specification string to use in forming SQL predicate - * - * @return string - */ - public function getSpecification() + public function getSpecification() : string { return $this->specification; } - /** - * Return "where" parts - * - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); list($values[], $types[]) = $this->normalizeArgument($this->minValue, self::TYPE_VALUE); diff --git a/src/Sql/Predicate/Expression.php b/src/Sql/Predicate/Expression.php index e9b5363f24..5694c4aebe 100644 --- a/src/Sql/Predicate/Expression.php +++ b/src/Sql/Predicate/Expression.php @@ -1,25 +1,22 @@ setExpression($expression); diff --git a/src/Sql/Predicate/In.php b/src/Sql/Predicate/In.php index 0c62189cf0..a312d7cb2a 100644 --- a/src/Sql/Predicate/In.php +++ b/src/Sql/Predicate/In.php @@ -1,17 +1,23 @@ setIdentifier($identifier); } + if ($valueSet !== null) { $this->setValueSet($valueSet); } @@ -41,10 +48,10 @@ public function __construct($identifier = null, $valueSet = null) /** * Set identifier for comparison * - * @param string|array $identifier - * @return self Provides a fluent interface + * @param string|array $identifier + * @return $this */ - public function setIdentifier($identifier) + public function setIdentifier($identifier) : self { $this->identifier = $identifier; @@ -64,17 +71,19 @@ public function getIdentifier() /** * Set set of values for IN comparison * - * @param array|Select $valueSet - * @return self Provides a fluent interface + * @param array|Select $valueSet + * @return $this + * * @throws Exception\InvalidArgumentException */ - public function setValueSet($valueSet) + public function setValueSet($valueSet) : self { if (! is_array($valueSet) && ! $valueSet instanceof Select) { throw new Exception\InvalidArgumentException( '$valueSet must be either an array or a Zend\Db\Sql\Select object, ' . gettype($valueSet) . ' given' ); } + $this->valueSet = $valueSet; return $this; @@ -82,7 +91,6 @@ public function setValueSet($valueSet) /** * Gets set of values in IN comparison - * * @return array|Select */ public function getValueSet() @@ -90,12 +98,7 @@ public function getValueSet() return $this->valueSet; } - /** - * Return array of parts for where statement - * - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $identifier = $this->getIdentifier(); $values = $this->getValueSet(); diff --git a/src/Sql/Predicate/IsNotNull.php b/src/Sql/Predicate/IsNotNull.php index e42c9aa5b5..ebbe99269c 100644 --- a/src/Sql/Predicate/IsNotNull.php +++ b/src/Sql/Predicate/IsNotNull.php @@ -1,12 +1,12 @@ setIdentifier($identifier); } } - /** - * Set identifier for comparison - * - * @param string $identifier - * @return self Provides a fluent interface - */ - public function setIdentifier($identifier) + public function setIdentifier(string $identifier) : self { $this->identifier = $identifier; + return $this; } - /** - * Get identifier of comparison - * - * @return null|string - */ - public function getIdentifier() + public function getIdentifier() : ?string { return $this->identifier; } - /** - * Set specification string to use in forming SQL predicate - * - * @param string $specification - * @return self Provides a fluent interface - */ - public function setSpecification($specification) + public function setSpecification(string $specification) : self { $this->specification = $specification; + return $this; } - /** - * Get specification string to use in forming SQL predicate - * - * @return string - */ - public function getSpecification() + public function getSpecification() : string { return $this->specification; } - /** - * Get parts for where statement - * - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $identifier = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); return [[ diff --git a/src/Sql/Predicate/Like.php b/src/Sql/Predicate/Like.php index 2f1ffde79b..72905996e3 100644 --- a/src/Sql/Predicate/Like.php +++ b/src/Sql/Predicate/Like.php @@ -1,108 +1,79 @@ setIdentifier($identifier); } + if ($like) { $this->setLike($like); } } - /** - * @param string $identifier - * @return self Provides a fluent interface - */ - public function setIdentifier($identifier) + public function setIdentifier(string $identifier) : self { $this->identifier = $identifier; + return $this; } - /** - * @return string - */ - public function getIdentifier() + public function getIdentifier() : string { return $this->identifier; } - /** - * @param string $like - * @return self Provides a fluent interface - */ - public function setLike($like) + public function setLike(string $like) : self { $this->like = $like; + return $this; } - /** - * @return string - */ - public function getLike() + public function getLike() : string { return $this->like; } - /** - * @param string $specification - * @return self Provides a fluent interface - */ - public function setSpecification($specification) + public function setSpecification(string $specification) : self { $this->specification = $specification; + return $this; } - /** - * @return string - */ - public function getSpecification() + public function getSpecification() : string { return $this->specification; } - /** - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER); list($values[], $types[]) = $this->normalizeArgument($this->like, self::TYPE_VALUE); + return [ [ $this->specification, diff --git a/src/Sql/Predicate/Literal.php b/src/Sql/Predicate/Literal.php index d4b509fc93..fa18a887c8 100644 --- a/src/Sql/Predicate/Literal.php +++ b/src/Sql/Predicate/Literal.php @@ -1,12 +1,12 @@ '; - const OP_GT = '>'; + public const OPERATOR_GREATER_THAN = '>'; + public const OP_GT = '>'; - const OPERATOR_GREATER_THAN_OR_EQUAL_TO = '>='; - const OP_GTE = '>='; + public const OPERATOR_GREATER_THAN_OR_EQUAL_TO = '>='; + public const OP_GTE = '>='; - /** - * {@inheritDoc} - */ protected $allowedTypes = [ self::TYPE_IDENTIFIER, self::TYPE_VALUE, ]; - /** - * @var int|float|bool|string - */ + /** @var int|float|bool|string */ protected $left; - /** - * @var int|float|bool|string - */ + /** @var int|float|bool|string */ protected $right; - /** - * @var string - */ + /** @var string */ protected $leftType = self::TYPE_IDENTIFIER; - /** - * @var string - */ + /** @var string */ protected $rightType = self::TYPE_VALUE; - /** - * @var string - */ + /** @var string */ protected $operator = self::OPERATOR_EQUAL_TO; /** @@ -76,10 +66,10 @@ class Operator extends AbstractExpression implements PredicateInterface */ public function __construct( $left = null, - $operator = self::OPERATOR_EQUAL_TO, + string $operator = self::OPERATOR_EQUAL_TO, $right = null, - $leftType = self::TYPE_IDENTIFIER, - $rightType = self::TYPE_VALUE + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE ) { if ($left !== null) { $this->setLeft($left); @@ -105,11 +95,10 @@ public function __construct( /** * Set left side of operator * - * @param int|float|bool|string $left - * - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @return $this */ - public function setLeft($left) + public function setLeft($left) : self { $this->left = $left; @@ -123,7 +112,6 @@ public function setLeft($left) /** * Get left side of operator - * * @return int|float|bool|string */ public function getLeft() @@ -134,13 +122,11 @@ public function getLeft() /** * Set parameter type for left side of operator * - * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes} - * - * @return self Provides a fluent interface - * + * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes} + * @return $this * @throws Exception\InvalidArgumentException */ - public function setLeftType($type) + public function setLeftType($type) : self { if (! in_array($type, $this->allowedTypes)) { throw new Exception\InvalidArgumentException(sprintf( @@ -156,35 +142,19 @@ public function setLeftType($type) return $this; } - /** - * Get parameter type on left side of operator - * - * @return string - */ - public function getLeftType() + public function getLeftType() : string { return $this->leftType; } - /** - * Set operator string - * - * @param string $operator - * @return self Provides a fluent interface - */ - public function setOperator($operator) + public function setOperator(string $operator) : self { $this->operator = $operator; return $this; } - /** - * Get operator string - * - * @return string - */ - public function getOperator() + public function getOperator() : string { return $this->operator; } @@ -192,11 +162,10 @@ public function getOperator() /** * Set right side of operator * - * @param int|float|bool|string $right - * - * @return self Provides a fluent interface + * @param int|float|bool|string $right + * @return $this */ - public function setRight($right) + public function setRight($right) : self { $this->right = $right; @@ -210,7 +179,6 @@ public function setRight($right) /** * Get right side of operator - * * @return int|float|bool|string */ public function getRight() @@ -221,11 +189,12 @@ public function getRight() /** * Set parameter type for right side of operator * - * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param string $type TYPE_IDENTIFIER or TYPE_VALUE {@see allowedTypes} + * @return $this + * * @throws Exception\InvalidArgumentException */ - public function setRightType($type) + public function setRightType(string $type) : self { if (! in_array($type, $this->allowedTypes)) { throw new Exception\InvalidArgumentException(sprintf( @@ -241,22 +210,12 @@ public function setRightType($type) return $this; } - /** - * Get parameter type on right side of operator - * - * @return string - */ - public function getRightType() + public function getRightType() : string { return $this->rightType; } - /** - * Get predicate parts for where statement - * - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { list($values[], $types[]) = $this->normalizeArgument($this->left, $this->leftType); list($values[], $types[]) = $this->normalizeArgument($this->right, $this->rightType); @@ -264,7 +223,7 @@ public function getExpressionData() return [[ '%s ' . $this->operator . ' %s', $values, - $types + $types, ]]; } } diff --git a/src/Sql/Predicate/Predicate.php b/src/Sql/Predicate/Predicate.php index a7d901d7bf..ab61695d63 100644 --- a/src/Sql/Predicate/Predicate.php +++ b/src/Sql/Predicate/Predicate.php @@ -1,15 +1,19 @@ setUnnest($this); - $this->addPredicate($predicateSet, ($this->nextPredicateCombineOperator) ?: $this->defaultCombination); + + $this->addPredicate($predicateSet, $this->nextPredicateCombineOperator ?? $this->defaultCombination); $this->nextPredicateCombineOperator = null; + return $predicateSet; } - /** - * Indicate what predicate will be unnested - * - * @param Predicate $predicate - * @return void - */ - public function setUnnest(Predicate $predicate) + public function setUnnest(Predicate $predicate) : void { $this->unnest = $predicate; } - /** - * Indicate end of nested predicate - * - * @return Predicate - * @throws RuntimeException - */ - public function unnest() + public function unnest() : Predicate { if ($this->unnest === null) { throw new RuntimeException('Not nested'); } + $unnest = $this->unnest; $this->unnest = null; + return $unnest; } @@ -70,18 +61,23 @@ public function unnest() * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ - public function equalTo($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE) - { + public function equalTo( + $left, + $right, + string $leftType = self::TYPE_IDENTIFIER, + ?string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_EQUAL_TO, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -92,18 +88,23 @@ public function equalTo($left, $right, $leftType = self::TYPE_IDENTIFIER, $right * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ - public function notEqualTo($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE) - { + public function notEqualTo( + $left, + $right, + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_NOT_EQUAL_TO, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -114,18 +115,23 @@ public function notEqualTo($left, $right, $leftType = self::TYPE_IDENTIFIER, $ri * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ - public function lessThan($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE) - { + public function lessThan( + $left, + $right, + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_LESS_THAN, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -136,18 +142,23 @@ public function lessThan($left, $right, $leftType = self::TYPE_IDENTIFIER, $righ * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ - public function greaterThan($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE) - { + public function greaterThan( + $left, + $right, + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_GREATER_THAN, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -158,18 +169,23 @@ public function greaterThan($left, $right, $leftType = self::TYPE_IDENTIFIER, $r * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ - public function lessThanOrEqualTo($left, $right, $leftType = self::TYPE_IDENTIFIER, $rightType = self::TYPE_VALUE) - { + public function lessThanOrEqualTo( + $left, + $right, + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_LESS_THAN_OR_EQUAL_TO, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -180,22 +196,23 @@ public function lessThanOrEqualTo($left, $right, $leftType = self::TYPE_IDENTIFI * * Utilizes Operator predicate * - * @param int|float|bool|string $left - * @param int|float|bool|string $right - * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} - * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} - * @return self Provides a fluent interface + * @param int|float|bool|string $left + * @param int|float|bool|string $right + * @param string $leftType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_IDENTIFIER {@see allowedTypes} + * @param string $rightType TYPE_IDENTIFIER or TYPE_VALUE by default TYPE_VALUE {@see allowedTypes} + * @return $this */ public function greaterThanOrEqualTo( $left, $right, - $leftType = self::TYPE_IDENTIFIER, - $rightType = self::TYPE_VALUE - ) { + string $leftType = self::TYPE_IDENTIFIER, + string $rightType = self::TYPE_VALUE + ) : self { $this->addPredicate( new Operator($left, Operator::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $right, $leftType, $rightType), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -206,16 +223,17 @@ public function greaterThanOrEqualTo( * * Utilizes Like predicate * - * @param string|Expression $identifier - * @param string $like - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param string $like + * @return $this */ - public function like($identifier, $like) + public function like($identifier, string $like) : self { $this->addPredicate( new Like($identifier, $like), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -225,17 +243,19 @@ public function like($identifier, $like) * * Utilizes In predicate * - * @param string|Expression $identifier - * @param string $notLike - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param string $notLike + * @return $this */ - public function notLike($identifier, $notLike) + public function notLike($identifier, string $notLike) : self { $this->addPredicate( new NotLike($identifier, $notLike), - ($this->nextPredicateCombineOperator) ? : $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; + return $this; } @@ -244,14 +264,15 @@ public function notLike($identifier, $notLike) * * @param $expression * @param $parameters - * @return self Provides a fluent interface + * @return $this */ - public function expression($expression, $parameters = null) + public function expression($expression, $parameters = null) : self { $this->addPredicate( new Expression($expression, $parameters), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -262,10 +283,10 @@ public function expression($expression, $parameters = null) * * Literal predicate, for parameters, use expression() * - * @param string $literal - * @return self Provides a fluent interface + * @param string $literal + * @return $this */ - public function literal($literal) + public function literal(string $literal) : self { // process deprecated parameters from previous literal($literal, $parameters = null) signature if (func_num_args() >= 2) { @@ -280,8 +301,9 @@ public function literal($literal) $this->addPredicate( $predicate, - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -292,15 +314,16 @@ public function literal($literal) * * Utilizes IsNull predicate * - * @param string|Expression $identifier - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @return $this */ - public function isNull($identifier) + public function isNull($identifier) : self { $this->addPredicate( new IsNull($identifier), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -311,15 +334,16 @@ public function isNull($identifier) * * Utilizes IsNotNull predicate * - * @param string|Expression $identifier - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @return $this */ - public function isNotNull($identifier) + public function isNotNull($identifier) : self { $this->addPredicate( new IsNotNull($identifier), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -330,16 +354,17 @@ public function isNotNull($identifier) * * Utilizes In predicate * - * @param string|Expression $identifier - * @param array|\Zend\Db\Sql\Select $valueSet - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param array|Select|null $valueSet + * @return s$this */ - public function in($identifier, $valueSet = null) + public function in($identifier, $valueSet = null) : self { $this->addPredicate( new In($identifier, $valueSet), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -350,15 +375,15 @@ public function in($identifier, $valueSet = null) * * Utilizes NotIn predicate * - * @param string|Expression $identifier - * @param array|\Zend\Db\Sql\Select $valueSet - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param array|Select|null $valueSet + * @return $this */ - public function notIn($identifier, $valueSet = null) + public function notIn($identifier, $valueSet = null) : self { $this->addPredicate( new NotIn($identifier, $valueSet), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); $this->nextPredicateCombineOperator = null; @@ -370,17 +395,18 @@ public function notIn($identifier, $valueSet = null) * * Utilizes Between predicate * - * @param string|Expression $identifier - * @param int|float|string $minValue - * @param int|float|string $maxValue - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param int|float|string $minValue + * @param int|float|string $maxValue + * @return $this */ - public function between($identifier, $minValue, $maxValue) + public function between($identifier, $minValue, $maxValue) : self { $this->addPredicate( new Between($identifier, $minValue, $maxValue), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -391,17 +417,18 @@ public function between($identifier, $minValue, $maxValue) * * Utilizes NotBetween predicate * - * @param string|Expression $identifier - * @param int|float|string $minValue - * @param int|float|string $maxValue - * @return self Provides a fluent interface + * @param string|Expression $identifier + * @param int|float|string $minValue + * @param int|float|string $maxValue + * @return $this */ - public function notBetween($identifier, $minValue, $maxValue) + public function notBetween($identifier, $minValue, $maxValue) : self { $this->addPredicate( new NotBetween($identifier, $minValue, $maxValue), - ($this->nextPredicateCombineOperator) ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -414,15 +441,16 @@ public function notBetween($identifier, $minValue, $maxValue) * AND / OR combination operator, thus allowing generic predicates to be * used fluently within where chains as any other concrete predicate. * - * @param PredicateInterface $predicate - * @return self Provides a fluent interface + * @param PredicateInterface $predicate + * @return $this */ - public function predicate(PredicateInterface $predicate) + public function predicate(PredicateInterface $predicate) : self { $this->addPredicate( $predicate, - $this->nextPredicateCombineOperator ?: $this->defaultCombination + $this->nextPredicateCombineOperator ?? $this->defaultCombination ); + $this->nextPredicateCombineOperator = null; return $this; @@ -433,10 +461,10 @@ public function predicate(PredicateInterface $predicate) * * Overloads "or", "and", "nest", and "unnest" * - * @param string $name - * @return self Provides a fluent interface + * @param string $name + * @return mixed */ - public function __get($name) + public function __get(string $name) { switch (strtolower($name)) { case 'or': @@ -450,6 +478,7 @@ public function __get($name) case 'unnest': return $this->unnest(); } + return $this; } } diff --git a/src/Sql/Predicate/PredicateInterface.php b/src/Sql/Predicate/PredicateInterface.php index b8d6c84baf..47487e0c12 100644 --- a/src/Sql/Predicate/PredicateInterface.php +++ b/src/Sql/Predicate/PredicateInterface.php @@ -1,12 +1,12 @@ defaultCombination = $defaultCombination; + if ($predicates) { foreach ($predicates as $predicate) { $this->addPredicate($predicate); @@ -39,25 +42,20 @@ public function __construct(array $predicates = null, $defaultCombination = self } } - /** - * Add predicate to set - * - * @param PredicateInterface $predicate - * @param string $combination - * @return self Provides a fluent interface - */ - public function addPredicate(PredicateInterface $predicate, $combination = null) + public function addPredicate(PredicateInterface $predicate, ?string $combination = null) : self { if ($combination === null || ! in_array($combination, [self::OP_AND, self::OP_OR])) { $combination = $this->defaultCombination; } - if ($combination == self::OP_OR) { + if ($combination === self::OP_OR) { $this->orPredicate($predicate); + return $this; } $this->andPredicate($predicate); + return $this; } @@ -65,30 +63,38 @@ public function addPredicate(PredicateInterface $predicate, $combination = null) * Add predicates to set * * @param PredicateInterface|\Closure|string|array $predicates - * @param string $combination - * @return self Provides a fluent interface + * @param string $combination + * @return $this + * * @throws Exception\InvalidArgumentException */ - public function addPredicates($predicates, $combination = self::OP_AND) + public function addPredicates($predicates, string $combination = self::OP_AND) : self { if ($predicates === null) { throw new Exception\InvalidArgumentException('Predicate cannot be null'); } + if ($predicates instanceof PredicateInterface) { $this->addPredicate($predicates, $combination); + return $this; } + if ($predicates instanceof \Closure) { $predicates($this); + return $this; } + if (is_string($predicates)) { // String $predicate should be passed as an expression $predicates = (strpos($predicates, Expression::PLACEHOLDER) !== false) ? new Expression($predicates) : new Literal($predicates); $this->addPredicate($predicates, $combination); + return $this; } + if (is_array($predicates)) { foreach ($predicates as $pkey => $pvalue) { // loop through predicates @@ -120,65 +126,48 @@ public function addPredicates($predicates, $combination = self::OP_AND) $predicates = (strpos($pvalue, Expression::PLACEHOLDER) !== false) ? new Expression($pvalue) : new Literal($pvalue); } + $this->addPredicate($predicates, $combination); } } + return $this; } - /** - * Return the predicates - * - * @return PredicateInterface[] - */ - public function getPredicates() + public function getPredicates() : array { return $this->predicates; } - /** - * Add predicate using OR operator - * - * @param PredicateInterface $predicate - * @return self Provides a fluent interface - */ - public function orPredicate(PredicateInterface $predicate) + public function orPredicate(PredicateInterface $predicate) : self { $this->predicates[] = [self::OP_OR, $predicate]; + return $this; } - /** - * Add predicate using AND operator - * - * @param PredicateInterface $predicate - * @return self Provides a fluent interface - */ - public function andPredicate(PredicateInterface $predicate) + public function andPredicate(PredicateInterface $predicate) : self { $this->predicates[] = [self::OP_AND, $predicate]; + return $this; } - /** - * Get predicate parts for where statement - * - * @return array - */ - public function getExpressionData() + public function getExpressionData() : array { $parts = []; + for ($i = 0, $count = count($this->predicates); $i < $count; $i++) { /** @var $predicate PredicateInterface */ $predicate = $this->predicates[$i][1]; - if ($predicate instanceof PredicateSet) { + if ($predicate instanceof self) { $parts[] = '('; } $parts = array_merge($parts, $predicate->getExpressionData()); - if ($predicate instanceof PredicateSet) { + if ($predicate instanceof self) { $parts[] = ')'; } @@ -186,15 +175,11 @@ public function getExpressionData() $parts[] = sprintf(' %s ', $this->predicates[$i + 1][0]); } } + return $parts; } - /** - * Get count of attached predicates - * - * @return int - */ - public function count() + public function count() : int { return count($this->predicates); } diff --git a/src/Sql/PreparableSqlInterface.php b/src/Sql/PreparableSqlInterface.php index f937e07b7b..d93464a3d3 100644 --- a/src/Sql/PreparableSqlInterface.php +++ b/src/Sql/PreparableSqlInterface.php @@ -1,12 +1,12 @@ '%1$s', self::SELECT => [ @@ -84,19 +100,19 @@ class Select extends AbstractPreparableSql self::JOINS => [ '%1$s' => [ [3 => '%1$s JOIN %2$s ON %3$s', 'combinedby' => ' '] - ] + ], ], self::WHERE => 'WHERE %1$s', self::GROUP => [ 'GROUP BY %1$s' => [ [1 => '%1$s', 'combinedby' => ', '] - ] + ], ], self::HAVING => 'HAVING %1$s', self::ORDER => [ 'ORDER BY %1$s' => [ [1 => '%1$s', 2 => '%1$s %2$s', 'combinedby' => ', '] - ] + ], ], self::LIMIT => 'LIMIT %1$s', self::OFFSET => 'OFFSET %1$s', @@ -104,75 +120,49 @@ class Select extends AbstractPreparableSql self::COMBINE => '%1$s ( %2$s )', ]; - /** - * @var bool - */ + /** @var bool */ protected $tableReadOnly = false; - /** - * @var bool - */ + /** @var bool */ protected $prefixColumnsWithTable = true; - /** - * @var string|array|TableIdentifier - */ - protected $table = null; + /** @var string|array|TableIdentifier */ + protected $table; - /** - * @var null|string|Expression - */ - protected $quantifier = null; + /** @var null|string|Expression */ + protected $quantifier; - /** - * @var array - */ + /** @var array */ protected $columns = [self::SQL_STAR]; - /** - * @var null|Join - */ - protected $joins = null; + /** @var null|Join */ + protected $joins; - /** - * @var Where - */ - protected $where = null; + /** @var Where */ + protected $where; - /** - * @var array - */ + /** @var array */ protected $order = []; - /** - * @var null|array - */ - protected $group = null; + /** @var null|array */ + protected $group; - /** - * @var null|string|array - */ - protected $having = null; + /** @var null|string|array */ + protected $having; - /** - * @var int|null - */ - protected $limit = null; + /** @var int|null */ + protected $limit; - /** - * @var int|null - */ - protected $offset = null; + /** @var int|null */ + protected $offset; - /** - * @var array - */ + /** @var array */ protected $combine = []; /** * Constructor * - * @param null|string|array|TableIdentifier $table + * @param null|string|array|TableIdentifier $table */ public function __construct($table = null) { @@ -181,19 +171,19 @@ public function __construct($table = null) $this->tableReadOnly = true; } - $this->where = new Where; - $this->joins = new Join; + $this->where = new Where; + $this->joins = new Join; $this->having = new Having; } /** * Create from clause * - * @param string|array|TableIdentifier $table - * @return self Provides a fluent interface + * @param string|array|TableIdentifier $table + * @return $this * @throws Exception\InvalidArgumentException */ - public function from($table) + public function from($table) : self { if ($this->tableReadOnly) { throw new Exception\InvalidArgumentException( @@ -219,10 +209,10 @@ public function from($table) /** * @param string|Expression $quantifier DISTINCT|ALL - * @return self Provides a fluent interface + * @return $this * @throws Exception\InvalidArgumentException */ - public function quantifier($quantifier) + public function quantifier($quantifier) : self { if (! is_string($quantifier) && ! $quantifier instanceof ExpressionInterface) { throw new Exception\InvalidArgumentException( @@ -231,6 +221,7 @@ public function quantifier($quantifier) ); } $this->quantifier = $quantifier; + return $this; } @@ -248,28 +239,28 @@ public function quantifier($quantifier) * key string will be use as alias, * value can be string or Expression objects * - * @param array $columns - * @param bool $prefixColumnsWithTable - * @return self Provides a fluent interface + * @param array $columns + * @param bool $prefixColumnsWithTable + * @return $this */ - public function columns(array $columns, $prefixColumnsWithTable = true) + public function columns(array $columns, bool $prefixColumnsWithTable = true) : self { $this->columns = $columns; - $this->prefixColumnsWithTable = (bool) $prefixColumnsWithTable; + $this->prefixColumnsWithTable = $prefixColumnsWithTable; return $this; } /** * Create join clause * - * @param string|array|TableIdentifier $name - * @param string|Predicate\Expression $on - * @param string|array $columns - * @param string $type one of the JOIN_* constants - * @return self Provides a fluent interface + * @param string|array|TableIdentifier $name + * @param string|Predicate\Expression $on + * @param string|array $columns + * @param string $type one of the JOIN_* constants + * @return $this * @throws Exception\InvalidArgumentException */ - public function join($name, $on, $columns = self::SQL_STAR, $type = self::JOIN_INNER) + public function join($name, $on, $columns = self::SQL_STAR, string $type = self::JOIN_INNER) : self { $this->joins->join($name, $on, $columns, $type); @@ -279,12 +270,12 @@ public function join($name, $on, $columns = self::SQL_STAR, $type = self::JOIN_I /** * Create where clause * - * @param Where|\Closure|string|array|Predicate\PredicateInterface $predicate - * @param string $combination One of the OP_* constants from Predicate\PredicateSet - * @return self Provides a fluent interface + * @param Where|\Closure|string|array|PredicateInterface $predicate + * @param string $combination One of the OP_* constants from Predicate\PredicateSet + * @return $this * @throws Exception\InvalidArgumentException */ - public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) + public function where($predicate, string $combination = PredicateSet::OP_AND) : self { if ($predicate instanceof Where) { $this->where = $predicate; @@ -296,9 +287,9 @@ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) /** * @param mixed $group - * @return self Provides a fluent interface + * @return $this */ - public function group($group) + public function group($group) : self { if (is_array($group)) { foreach ($group as $o) { @@ -313,11 +304,11 @@ public function group($group) /** * Create having clause * - * @param Where|\Closure|string|array $predicate - * @param string $combination One of the OP_* constants from Predicate\PredicateSet - * @return self Provides a fluent interface + * @param Where|\Closure|string|array $predicate + * @param string $combination One of the OP_* constants from Predicate\PredicateSet + * @return $this */ - public function having($predicate, $combination = Predicate\PredicateSet::OP_AND) + public function having($predicate, string $combination = PredicateSet::OP_AND) : self { if ($predicate instanceof Having) { $this->having = $predicate; @@ -329,9 +320,9 @@ public function having($predicate, $combination = Predicate\PredicateSet::OP_AND /** * @param string|array|Expression $order - * @return self Provides a fluent interface + * @return $this Provides a fluent interface */ - public function order($order) + public function order($order) : self { if (is_string($order)) { if (strpos($order, ',') !== false) { @@ -352,72 +343,30 @@ public function order($order) return $this; } - /** - * @param int $limit - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ - public function limit($limit) + public function limit(int $limit) : self { - if (! is_numeric($limit)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects parameter to be numeric, "%s" given', - __METHOD__, - (is_object($limit) ? get_class($limit) : gettype($limit)) - )); - } - $this->limit = $limit; return $this; } - /** - * @param int $offset - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ - public function offset($offset) + public function offset(int $offset) : self { - if (! is_numeric($offset)) { - throw new Exception\InvalidArgumentException(sprintf( - '%s expects parameter to be numeric, "%s" given', - __METHOD__, - (is_object($offset) ? get_class($offset) : gettype($offset)) - )); - } - $this->offset = $offset; return $this; } - /** - * @param Select $select - * @param string $type - * @param string $modifier - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ - public function combine(Select $select, $type = self::COMBINE_UNION, $modifier = '') + public function combine(Select $select, string $type = self::COMBINE_UNION, string $modifier = '') : self { if ($this->combine !== []) { throw new Exception\InvalidArgumentException( 'This Select object is already combined and cannot be combined with multiple Selects objects' ); } - $this->combine = [ - 'select' => $select, - 'type' => $type, - 'modifier' => $modifier - ]; + $this->combine = compact(self::SELECT, 'type', self::MODIFIER); return $this; } - /** - * @param string $part - * @return self Provides a fluent interface - * @throws Exception\InvalidArgumentException - */ - public function reset($part) + public function reset(string $part) : self { switch ($part) { case self::TABLE: @@ -463,11 +412,11 @@ public function reset($part) } /** - * @param $index - * @param $specification - * @return self Provides a fluent interface + * @param string $index + * @param array|string $specification + * @return $this */ - public function setSpecification($index, $specification) + public function setSpecification(string $index, $specification) : self { if (! method_exists($this, 'process' . $index)) { throw new Exception\InvalidArgumentException('Not a valid specification name.'); @@ -476,7 +425,7 @@ public function setSpecification($index, $specification) return $this; } - public function getRawState($key = null) + public function getRawState(?string $key = null) { $rawState = [ self::TABLE => $this->table, @@ -489,57 +438,45 @@ public function getRawState($key = null) self::HAVING => $this->having, self::LIMIT => $this->limit, self::OFFSET => $this->offset, - self::COMBINE => $this->combine + self::COMBINE => $this->combine, ]; return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; } - /** - * Returns whether the table is read only or not. - * - * @return bool - */ - public function isTableReadOnly() + public function isTableReadOnly() : bool { return $this->tableReadOnly; } protected function processStatementStart( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?array { if ($this->combine !== []) { return ['(']; } } protected function processStatementEnd( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?array { if ($this->combine !== []) { return [')']; } } - /** - * Process the select part - * - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * @return null|array - */ protected function processSelect( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?array { $expr = 1; - list($table, $fromTable) = $this->resolveTable($this->table, $platform, $driver, $parameterContainer); + [$table, $fromTable] = $this->resolveTable($this->table, $platform, $driver, $parameterContainer); + // process table columns $columns = []; foreach ($this->columns as $columnIndexOrAs => $column) { @@ -559,18 +496,19 @@ protected function processSelect( $parameterContainer, (is_string($columnIndexOrAs) ? $columnIndexOrAs : 'column') ); + // process As portion if (is_string($columnIndexOrAs)) { $columnAs = $platform->quoteIdentifier($columnIndexOrAs); } elseif (stripos($columnName, ' as ') === false) { - $columnAs = (is_string($column)) ? $platform->quoteIdentifier($column) : 'Expression' . $expr++; + $columnAs = is_string($column) ? $platform->quoteIdentifier($column) : 'Expression' . $expr++; } - $columns[] = (isset($columnAs)) ? [$columnName, $columnAs] : [$columnName]; + $columns[] = isset($columnAs) ? [$columnName, $columnAs] : [$columnName]; } // process join columns foreach ($this->joins->getJoins() as $join) { - $joinName = (is_array($join['name'])) ? key($join['name']) : $join['name']; + $joinName = is_array($join['name']) ? key($join['name']) : $join['name']; $joinName = parent::resolveTable($joinName, $platform, $driver, $parameterContainer); foreach ($join['columns'] as $jKey => $jColumn) { @@ -606,44 +544,49 @@ protected function processSelect( if (! isset($table)) { return [$columns]; - } elseif (isset($quantifier)) { + } + + if (isset($quantifier)) { return [$quantifier, $columns, $table]; - } else { - return [$columns, $table]; } + + return [$columns, $table]; } protected function processJoins( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { return $this->processJoin($this->joins, $platform, $driver, $parameterContainer); } protected function processWhere( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { - if ($this->where->count() == 0) { + if ($this->where->count() === 0) { return; } + return [ - $this->processExpression($this->where, $platform, $driver, $parameterContainer, 'where') + $this->processExpression($this->where, $platform, $driver, $parameterContainer, self::WHERE) ]; } protected function processGroup( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->group === null) { return; } + // process table columns $groups = []; + foreach ($this->group as $column) { $groups[] = $this->resolveColumnValue( [ @@ -660,25 +603,25 @@ protected function processGroup( } protected function processHaving( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { - if ($this->having->count() == 0) { + if ($this->having->count() === 0) { return; } return [ - $this->processExpression($this->having, $platform, $driver, $parameterContainer, 'having') + $this->processExpression($this->having, $platform, $driver, $parameterContainer, self::HAVING) ]; } protected function processOrder( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null - ) { + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null + ) : ?array { if (empty($this->order)) { - return; + return null; } $orders = []; foreach ($this->order as $k => $v) { @@ -690,7 +633,7 @@ protected function processOrder( } if (is_int($k)) { if (strpos($v, ' ') !== false) { - list($k, $v) = preg_split('# #', $v, 2); + [$k, $v] = preg_split('# #', $v, 2); } else { $k = $v; $v = self::ORDER_ASCENDING; @@ -707,85 +650,71 @@ protected function processOrder( protected function processLimit( PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->limit === null) { return; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; - $parameterContainer->offsetSet($paramPrefix . 'limit', $this->limit, ParameterContainer::TYPE_INTEGER); - return [$driver->formatParameterName($paramPrefix . 'limit')]; + $parameterContainer->offsetSet($paramPrefix . self::LIMIT, $this->limit, ParameterContainer::TYPE_INTEGER); + return [$driver->formatParameterName($paramPrefix . self::LIMIT)]; } - return [$platform->quoteValue($this->limit)]; + return [$platform->quoteValue((string) $this->limit ?? '')]; } protected function processOffset( PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { if ($this->offset === null) { return; } if ($parameterContainer) { $paramPrefix = $this->processInfo['paramPrefix']; - $parameterContainer->offsetSet($paramPrefix . 'offset', $this->offset, ParameterContainer::TYPE_INTEGER); - return [$driver->formatParameterName($paramPrefix . 'offset')]; + $parameterContainer->offsetSet($paramPrefix . self::OFFSET, $this->offset, ParameterContainer::TYPE_INTEGER); + return [$driver->formatParameterName($paramPrefix . self::OFFSET)]; } - return [$platform->quoteValue($this->offset)]; + return [$platform->quoteValue((string) $this->offset ?? '')]; } protected function processCombine( PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { - if ($this->combine == []) { + if ($this->combine === []) { return; } $type = $this->combine['type']; - if ($this->combine['modifier']) { - $type .= ' ' . $this->combine['modifier']; + if ($this->combine[self::MODIFIER]) { + $type .= ' ' . $this->combine[self::MODIFIER]; } return [ strtoupper($type), - $this->processSubSelect($this->combine['select'], $platform, $driver, $parameterContainer), + $this->processSubSelect($this->combine[self::SELECT], $platform, $driver, $parameterContainer), ]; } - /** - * Variable overloading - * - * @param string $name - * @throws Exception\InvalidArgumentException - * @return mixed - */ - public function __get($name) + public function __get(string $name) { switch (strtolower($name)) { - case 'where': + case self::WHERE: return $this->where; - case 'having': + case self::HAVING: return $this->having; - case 'joins': + case self::JOINS: return $this->joins; default: throw new Exception\InvalidArgumentException('Not a valid magic property for this object'); } } - /** - * __clone - * - * Resets the where object each time the Select is cloned. - * - * @return void - */ public function __clone() { $this->where = clone $this->where; @@ -795,16 +724,16 @@ public function __clone() /** * @param string|TableIdentifier|Select $table - * @param PlatformInterface $platform - * @param DriverInterface $driver - * @param ParameterContainer $parameterContainer - * @return string + * @param PlatformInterface $platform + * @param DriverInterface $driver + * @param ParameterContainer $parameterContainer + * @return array */ protected function resolveTable( $table, - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { $alias = null; @@ -830,7 +759,7 @@ protected function resolveTable( return [ $table, - $fromTable + $fromTable, ]; } } diff --git a/src/Sql/Sql.php b/src/Sql/Sql.php index 2cc1428204..f8fb8a2803 100644 --- a/src/Sql/Sql.php +++ b/src/Sql/Sql.php @@ -1,62 +1,66 @@ adapter = $adapter; if ($table) { $this->setTable($table); } - $this->sqlPlatform = $sqlPlatform ?: new Platform\Platform($adapter); + $this->sqlPlatform = $sqlPlatform ?? new Platform\Platform($adapter); } - /** - * @return null|\Zend\Db\Adapter\AdapterInterface - */ - public function getAdapter() + public function getAdapter() : ?AdapterInterface { return $this->adapter; } - public function hasTable() + public function hasTable() : bool { return ($this->table !== null); } /** * @param string|array|TableIdentifier $table - * @return self Provides a fluent interface + * @return $this * @throws Exception\InvalidArgumentException */ - public function setTable($table) + public function setTable($table) : self { if (is_string($table) || is_array($table) || $table instanceof TableIdentifier) { $this->table = $table; @@ -73,12 +77,16 @@ public function getTable() return $this->table; } - public function getSqlPlatform() + public function getSqlPlatform() : Platform\Platform { return $this->sqlPlatform; } - public function select($table = null) + /** + * @param null|string|array|TableIdentifier $table + * @return Select + */ + public function select($table = null) : Select { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -86,10 +94,14 @@ public function select($table = null) $this->table )); } - return new Select(($table) ?: $this->table); + return new Select($table ?? $this->table); } - public function insert($table = null) + /** + * @param null|string|TableIdentifier $table + * @return Insert + */ + public function insert($table = null) : Insert { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -97,10 +109,14 @@ public function insert($table = null) $this->table )); } - return new Insert(($table) ?: $this->table); + return new Insert($table ?? $this->table); } - public function update($table = null) + /** + * @param null|string|TableIdentifier $table + * @return Update + */ + public function update($table = null) : Update { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -108,10 +124,14 @@ public function update($table = null) $this->table )); } - return new Update(($table) ?: $this->table); + return new Update($table ?? $this->table); } - public function delete($table = null) + /** + * @param null|string|TableIdentifier $table + * @return Delete + */ + public function delete($table = null) : Delete { if ($this->table !== null && $table !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -119,23 +139,16 @@ public function delete($table = null) $this->table )); } - return new Delete(($table) ?: $this->table); + return new Delete($table ?? $this->table); } - /** - * @param PreparableSqlInterface $sqlObject - * @param StatementInterface $statement - * @param AdapterInterface $adapter - * - * @return StatementInterface - */ public function prepareStatementForSqlObject( PreparableSqlInterface $sqlObject, - StatementInterface $statement = null, - AdapterInterface $adapter = null - ) { - $adapter = $adapter ?: $this->adapter; - $statement = $statement ?: $adapter->getDriver()->createStatement(); + ?StatementInterface $statement = null, + ?AdapterInterface $adapter = null + ) : StatementInterface { + $adapter = $adapter ?? $this->adapter; + $statement = $statement ?? $adapter->getDriver()->createStatement(); return $this->sqlPlatform->setSubject($sqlObject)->prepareStatement($adapter, $statement); } @@ -145,26 +158,17 @@ public function prepareStatementForSqlObject( * * @param SqlInterface $sqlObject * @param PlatformInterface|null $platform - * * @return string * * @deprecated Deprecated in 2.4. Use buildSqlString() instead */ - public function getSqlStringForSqlObject(SqlInterface $sqlObject, PlatformInterface $platform = null) + public function getSqlStringForSqlObject(SqlInterface $sqlObject, ?PlatformInterface $platform = null) : string { - $platform = ($platform) ?: $this->adapter->getPlatform(); + $platform = $platform ?? $this->adapter->getPlatform(); return $this->sqlPlatform->setSubject($sqlObject)->getSqlString($platform); } - /** - * @param SqlInterface $sqlObject - * @param AdapterInterface $adapter - * - * @return string - * - * @throws Exception\InvalidArgumentException - */ - public function buildSqlString(SqlInterface $sqlObject, AdapterInterface $adapter = null) + public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null) : string { return $this ->sqlPlatform diff --git a/src/Sql/SqlInterface.php b/src/Sql/SqlInterface.php index 551a458c98..5831f98fb8 100644 --- a/src/Sql/SqlInterface.php +++ b/src/Sql/SqlInterface.php @@ -1,24 +1,17 @@ table = (string) $table; + $this->table = $table; if ('' === $this->table) { throw new Exception\InvalidArgumentException('$table must be a valid table name, empty string given'); @@ -52,7 +49,7 @@ public function __construct($table, $schema = null) )); } - $this->schema = (string) $schema; + $this->schema = $schema; if ('' === $this->schema) { throw new Exception\InvalidArgumentException( @@ -64,36 +61,28 @@ public function __construct($table, $schema = null) /** * @param string $table - * * @deprecated please use the constructor and build a new {@see TableIdentifier} instead */ - public function setTable($table) + public function setTable(string $table) : void { $this->table = $table; } - /** - * @return string - */ - public function getTable() + public function getTable() : string { return $this->table; } - /** - * @return bool - */ - public function hasSchema() + public function hasSchema() : bool { return ($this->schema !== null); } /** - * @param $schema - * + * @param null|string $schema * @deprecated please use the constructor and build a new {@see TableIdentifier} instead */ - public function setSchema($schema) + public function setSchema(?string $schema) : void { $this->schema = $schema; } @@ -101,12 +90,12 @@ public function setSchema($schema) /** * @return null|string */ - public function getSchema() + public function getSchema() : ?string { return $this->schema; } - public function getTableAndSchema() + public function getTableAndSchema() : array { return [$this->table, $this->schema]; } diff --git a/src/Sql/Update.php b/src/Sql/Update.php index 09563cf528..ca4b2b92dc 100644 --- a/src/Sql/Update.php +++ b/src/Sql/Update.php @@ -1,22 +1,26 @@ 'UPDATE %1$s', self::SPECIFICATION_JOIN => [ '%1$s' => [ - [3 => '%1$s JOIN %2$s ON %3$s', 'combinedby' => ' '] + [3 => '%1$s JOIN %2$s ON %3$s', 'combinedby' => ' '], ] ], self::SPECIFICATION_SET => 'SET %1$s', self::SPECIFICATION_WHERE => 'WHERE %1$s', ]; - /** - * @var string|TableIdentifier - */ + /** @var string|TableIdentifier */ protected $table = ''; - /** - * @var bool - */ + /** @var bool */ protected $emptyWhereProtection = true; - /** - * @var PriorityList - */ + /** @var PriorityList */ protected $set; - /** - * @var string|Where - */ - protected $where = null; + /** @var string|Where */ + protected $where; - /** - * @var null|Join - */ - protected $joins = null; + /** @var null|Join */ + protected $joins; /** * Constructor * - * @param null|string|TableIdentifier $table + * @param null|string|TableIdentifier $table */ public function __construct($table = null) { @@ -81,6 +75,7 @@ public function __construct($table = null) } $this->where = new Where(); $this->joins = new Join(); + $this->set = new PriorityList(); $this->set->isLIFO(false); } @@ -88,10 +83,10 @@ public function __construct($table = null) /** * Specify table for statement * - * @param string|TableIdentifier $table - * @return self Provides a fluent interface + * @param string|TableIdentifier $table + * @return $this */ - public function table($table) + public function table($table) : self { $this->table = $table; return $this; @@ -100,18 +95,18 @@ public function table($table) /** * Set key/value pairs to update * - * @param array $values Associative array of key values - * @param string $flag One of the VALUES_* constants - * @return self Provides a fluent interface + * @param array $values Associative array of key values + * @param string $flag One of the VALUES_* constants + * @return $this * @throws Exception\InvalidArgumentException */ - public function set(array $values, $flag = self::VALUES_SET) + public function set(array $values, string $flag = self::VALUES_SET) : self { if ($values === null) { throw new Exception\InvalidArgumentException('set() expects an array of values'); } - if ($flag == self::VALUES_SET) { + if ($flag === self::VALUES_SET) { $this->set->clear(); } $priority = is_numeric($flag) ? $flag : 0; @@ -127,12 +122,12 @@ public function set(array $values, $flag = self::VALUES_SET) /** * Create where clause * - * @param Where|\Closure|string|array $predicate - * @param string $combination One of the OP_* constants from Predicate\PredicateSet - * @return self Provides a fluent interface + * @param Where|\Closure|string|array $predicate + * @param string $combination One of the OP_* constants from Predicate\PredicateSet + * @return $this * @throws Exception\InvalidArgumentException */ - public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) + public function where($predicate, string $combination = Predicate\PredicateSet::OP_AND) : self { if ($predicate instanceof Where) { $this->where = $predicate; @@ -145,20 +140,20 @@ public function where($predicate, $combination = Predicate\PredicateSet::OP_AND) /** * Create join clause * - * @param string|array $name - * @param string $on - * @param string $type one of the JOIN_* constants - * @return self Provides a fluent interface + * @param string|array $name + * @param string $on + * @param string $type one of the JOIN_* constants + * @return $this * @throws Exception\InvalidArgumentException */ - public function join($name, $on, $type = Join::JOIN_INNER) + public function join($name, string $on, string $type = Join::JOIN_INNER) : self { $this->joins->join($name, $on, [], $type); return $this; } - public function getRawState($key = null) + public function getRawState(?string $key = null) { $rawState = [ 'emptyWhereProtection' => $this->emptyWhereProtection, @@ -171,9 +166,9 @@ public function getRawState($key = null) } protected function processUpdate( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { return sprintf( $this->specifications[static::SPECIFICATION_UPDATE], @@ -182,9 +177,9 @@ protected function processUpdate( } protected function processSet( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { $setSql = []; $i = 0; @@ -226,11 +221,11 @@ protected function processSet( } protected function processWhere( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { - if ($this->where->count() == 0) { + if ($this->where->count() === 0) { return; } return sprintf( @@ -240,9 +235,9 @@ protected function processWhere( } protected function processJoins( - PlatformInterface $platform, - DriverInterface $driver = null, - ParameterContainer $parameterContainer = null + PlatformInterface $platform, + ?DriverInterface $driver = null, + ?ParameterContainer $parameterContainer = null ) { return $this->processJoin($this->joins, $platform, $driver, $parameterContainer); } @@ -252,26 +247,19 @@ protected function processJoins( * * Proxies to "where" only * - * @param string $name + * @param string $name * @return mixed */ - public function __get($name) + public function __get(string $name) { - if (strtolower($name) == 'where') { + if (strtolower($name) === 'where') { return $this->where; } } - /** - * __clone - * - * Resets the where object each time the Update is cloned. - * - * @return void - */ public function __clone() { $this->where = clone $this->where; - $this->set = clone $this->set; + $this->set = clone $this->set; } } diff --git a/src/Sql/Where.php b/src/Sql/Where.php index 68166d5256..6be0074da8 100644 --- a/src/Sql/Where.php +++ b/src/Sql/Where.php @@ -1,12 +1,12 @@