diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index d94b3ef..742c01b 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -47,7 +47,7 @@ jobs: strategy: matrix: php-version: - - "7.2" + - "7.4" services: mysql: @@ -81,7 +81,7 @@ jobs: run: "composer update --no-interaction --no-progress --no-suggest --prefer-lowest" - name: "Run PHPUnit" - run: "vendor/bin/phpunit -c phpunit.mysql8.xml --coverage-clover=coverage.xml" + run: "vendor/bin/phpunit --no-coverage" phpunit-code-coverage: name: "PHPUnit with Code Coverage" @@ -90,7 +90,7 @@ jobs: strategy: matrix: php-version: - - "7.2" + - "7.4" services: mysql: @@ -137,9 +137,7 @@ jobs: strategy: matrix: php-version: - - "7.1" - - "7.3" - - "7.4" + - "8.0" services: mysql: @@ -161,7 +159,6 @@ jobs: with: php-version: "${{ matrix.php-version }}" extensions: "" - coverage: "pcov" - name: "Cache dependencies installed with composer" uses: "actions/cache@v1" @@ -174,4 +171,4 @@ jobs: run: "composer install --no-interaction --no-progress --no-suggest" - name: "Run PHPUnit" - run: "vendor/bin/phpunit" + run: "vendor/bin/phpunit --no-coverage" diff --git a/composer.json b/composer.json index 8853c89..dbd4a3c 100644 --- a/composer.json +++ b/composer.json @@ -13,21 +13,21 @@ } ], "require": { - "php": ">=7.1.0", + "php": ">=7.4.0 || ^8.0", "mouf/utils.common.conditioninterface": "~2.0", "mouf/utils.value.value-interface": "~1.0", "mouf/utils.common.paginable-interface": "~1.0", "mouf/utils.common.sortable-interface": "~1.0", "mouf/schema-analyzer": "~1.0", "twig/twig": "^2.11 || ^3", - "greenlion/php-sql-parser": "^4.1.2", + "greenlion/php-sql-parser": "^4.3", "doctrine/cache": "^1.5" }, "require-dev": { - "phpunit/phpunit": "^7.5.10", + "phpunit/phpunit": "^9.5", "satooshi/php-coveralls": "~1.0", "doctrine/dbal": "~2.5", - "phpstan/phpstan": "^0.11.7" + "phpstan/phpstan": "^0.12.82" }, "suggest": { "doctrine/dbal": "To support more databases than just MySQL and to use MagicJoin feature", @@ -67,6 +67,6 @@ "minimum-stability": "dev", "prefer-stable": true, "scripts": { - "phpstan": "vendor/bin/phpstan analyse -c phpstan.neon src" + "phpstan": "vendor/bin/phpstan analyse -c phpstan.neon" } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..d8d1d0b --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,197 @@ +parameters: + ignoreErrors: + - + message: "#^Method Mouf\\\\Database\\\\QueryWriter\\\\QueryResult\\:\\:val\\(\\) should return array but returns Mouf\\\\Database\\\\QueryWriter\\\\ResultSet\\.$#" + count: 1 + path: src/Mouf/Database/QueryWriter/QueryResult.php + + - + message: "#^Method Mouf\\\\Database\\\\QueryWriter\\\\Utils\\\\DbHelper\\:\\:getAll\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/Mouf/Database/QueryWriter/Utils/DbHelper.php + + - + message: "#^Parameter \\#1 \\$rightOperand of method SQLParser\\\\Node\\\\AbstractInListOperator\\:\\:refactorParameterToExpression\\(\\) expects SQLParser\\\\Node\\\\NodeInterface, array\\\\|SQLParser\\\\Node\\\\NodeInterface\\|string given\\.$#" + count: 1 + path: src/SQLParser/Node/AbstractInListOperator.php + + - + message: "#^Cannot access offset 0 on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\.$#" + count: 1 + path: src/SQLParser/Node/AbstractInListOperator.php + + - + message: "#^Method SQLParser\\\\Node\\\\AbstractTwoOperandsOperator\\:\\:setLeftOperand\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/AbstractTwoOperandsOperator.php + + - + message: "#^Method SQLParser\\\\Node\\\\AbstractTwoOperandsOperator\\:\\:setRightOperand\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/AbstractTwoOperandsOperator.php + + - + message: "#^Cannot call method walk\\(\\) on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\|string\\.$#" + count: 2 + path: src/SQLParser/Node/AbstractTwoOperandsOperator.php + + - + message: "#^Method SQLParser\\\\Node\\\\Between\\:\\:setLeftOperand\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/Between.php + + - + message: "#^Method SQLParser\\\\Node\\\\Between\\:\\:setMinValueOperand\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/Between.php + + - + message: "#^Cannot call method walk\\(\\) on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\|string\\.$#" + count: 1 + path: src/SQLParser/Node/Between.php + + - + message: "#^Cannot call method walk\\(\\) on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\|string\\.$#" + count: 1 + path: src/SQLParser/Node/CaseOperation.php + + - + message: "#^Cannot access offset mixed on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\.$#" + count: 2 + path: src/SQLParser/Node/Expression.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:toObject\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Parameter \\#1 \\$refClause of method SQLParser\\\\Node\\\\SubQuery\\:\\:setRefClause\\(\\) expects array\\, array\\\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:buildFromSubtree\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:buildFromSubtree\\(\\) has parameter \\$subTree with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:mapArrayToNodeObjectList\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Property SQLParser\\\\Node\\\\NodeFactory\\:\\:\\$PRECEDENCE has no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Property SQLParser\\\\Node\\\\NodeFactory\\:\\:\\$OPERATOR_TO_CLASS has no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, array\\\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Parameter \\#2 \\.\\.\\.\\$args of function array_merge expects array, array\\\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:array_map_deep\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:array_map_deep\\(\\) has parameter \\$array with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Method SQLParser\\\\Node\\\\NodeFactory\\:\\:array_map_deep\\(\\) has parameter \\$callback with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/NodeFactory.php + + - + message: "#^Property SQLParser\\\\Node\\\\Operator\\:\\:\\$value has no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/Operator.php + + - + message: "#^Method SQLParser\\\\Node\\\\Operator\\:\\:getValue\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/Operator.php + + - + message: "#^Method SQLParser\\\\Node\\\\Operator\\:\\:setValue\\(\\) has no return typehint specified\\.$#" + count: 1 + path: src/SQLParser/Node/Operator.php + + - + message: "#^Strict comparison using \\=\\=\\= between mixed and null will always evaluate to false\\.$#" + count: 1 + path: src/SQLParser/Node/Parameter.php + + - + message: "#^Argument of an invalid type array\\\\|SQLParser\\\\Node\\\\NodeInterface supplied for foreach, only iterables are supported\\.$#" + count: 1 + path: src/SQLParser/Node/SimpleFunction.php + + - + message: "#^Cannot access offset mixed on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\.$#" + count: 2 + path: src/SQLParser/Node/SimpleFunction.php + + - + message: "#^Cannot access offset mixed on array\\\\|SQLParser\\\\Node\\\\NodeInterface\\.$#" + count: 2 + path: src/SQLParser/Node/Table.php + + - + message: "#^Elseif condition is always true\\.$#" + count: 1 + path: src/SQLParser/Node/Table.php + + - + message: "#^Property SQLParser\\\\Node\\\\Table\\:\\:\\$refClause \\(array\\\\|SQLParser\\\\Node\\\\NodeInterface\\) does not accept null\\.$#" + count: 1 + path: src/SQLParser/Node/Table.php + + - + message: "#^Method SQLParser\\\\Query\\\\Select\\:\\:overwriteInstanceDescriptor\\(\\) has parameter \\$name with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Query/Select.php + + - + message: "#^Method SQLParser\\\\Query\\\\Select\\:\\:walkChildren\\(\\) has parameter \\$children with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Query/Select.php + + - + message: "#^Argument of an invalid type array\\\\|SQLParser\\\\Node\\\\NodeInterface supplied for foreach, only iterables are supported\\.$#" + count: 1 + path: src/SQLParser/Query/StatementFactory.php + + - + message: "#^Cannot access offset mixed on \\(array\\&nonEmpty\\)\\|SQLParser\\\\Node\\\\NodeInterface\\.$#" + count: 1 + path: src/SQLParser/Query/StatementFactory.php + + - + message: "#^Parameter \\#1 \\$columns of method SQLParser\\\\Query\\\\Select\\:\\:setColumns\\(\\) expects array\\, array\\\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#" + count: 1 + path: src/SQLParser/Query/StatementFactory.php + + - + message: "#^Method SQLParser\\\\Query\\\\Union\\:\\:overwriteInstanceDescriptor\\(\\) has parameter \\$name with no typehint specified\\.$#" + count: 1 + path: src/SQLParser/Query/Union.php + diff --git a/phpstan.neon b/phpstan.neon index 588a228..c85f7c3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,14 @@ +includes: + - phpstan-baseline.neon + parameters: level: 7 + paths: + - src inferPrivatePropertyTypeFromConstructor: true + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false ignoreErrors: - "#Mouf\\\\MoufManager#" - "#Mouf\\\\MoufInstanceDescriptor#" - - '#Method Mouf\\Database\\QueryWriter\\Utils\\FindParametersService::findParameters\(\) should return array but returns array#' \ No newline at end of file + - '#Method Mouf\\Database\\QueryWriter\\Utils\\FindParametersService::findParameters\(\) should return array but returns array#' diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 95a50ce..61da686 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -12,7 +12,8 @@ > - + + diff --git a/src/Mouf/Database/MagicQuery.php b/src/Mouf/Database/MagicQuery.php index c9158ef..7dbdb8e 100644 --- a/src/Mouf/Database/MagicQuery.php +++ b/src/Mouf/Database/MagicQuery.php @@ -2,6 +2,7 @@ namespace Mouf\Database; +use Doctrine\Common\Cache\Cache; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform; @@ -34,21 +35,20 @@ class MagicQuery { private $connection; + /** @var Cache */ private $cache; + /** @var SchemaAnalyzer */ private $schemaAnalyzer; - /** - * @var AbstractPlatform - */ + /** @var AbstractPlatform */ private $platform; - /** - * @var Environment - */ + /** @var Environment */ private $twigEnvironment; + /** @var bool */ private $enableTwig = false; /** * @param \Doctrine\DBAL\Connection $connection - * @param \Doctrine\Common\Cache\Cache $cache + * @param Cache $cache * @param SchemaAnalyzer $schemaAnalyzer (optional). If not set, it is initialized from the connection. */ public function __construct($connection = null, $cache = null, SchemaAnalyzer $schemaAnalyzer = null) @@ -205,11 +205,9 @@ public function toSql(NodeInterface $sqlNode, array $parameters = array(), bool /** * Scans the SQL statement and replaces the "magicjoin" part with the correct joins. * - * @param NodeInterface $select - * * @throws MagicQueryMissingConnectionException */ - private function magicJoin(NodeInterface $select) + private function magicJoin(NodeInterface $select): void { // Let's find if this is a MagicJoin query. $magicJoinDetector = new DetectMagicJoinSelectVisitor(); @@ -227,12 +225,8 @@ private function magicJoin(NodeInterface $select) /** * For one given MagicJoin select, let's apply MagicJoin. - * - * @param MagicJoinSelect $magicJoinSelect - * - * @return Select */ - private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect) + private function magicJoinOnOneQuery(MagicJoinSelect $magicJoinSelect): void { $tableSearchNodeTraverser = new NodeTraverser(); $detectTableVisitor = new DetectTablesVisitor($magicJoinSelect->getMainTable()); @@ -316,7 +310,7 @@ private function getSchemaAnalyzer() return $this->schemaAnalyzer; } - private function getConnectionUniqueId(Connection $connection) + private function getConnectionUniqueId(Connection $connection): string { return hash('md4', $connection->getHost().'-'.$connection->getPort().'-'.$connection->getDatabase().'-'.$connection->getDriver()->getName()); } diff --git a/src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php b/src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php index 55cf0c5..589e7e1 100644 --- a/src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php +++ b/src/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactory.php @@ -12,9 +12,10 @@ */ class SqlTwigEnvironmentFactory { + /** @var Environment|null */ private static $twig; - public static function getTwigEnvironment() + public static function getTwigEnvironment(): Environment { if (self::$twig) { return self::$twig; @@ -45,7 +46,7 @@ public static function getTwigEnvironment() return $twig; } - private static function getCacheDirectory() + private static function getCacheDirectory(): string { // If we are running on a Unix environment, let's prepend the cache with the user id of the PHP process. // This way, we can avoid rights conflicts. diff --git a/src/Mouf/Database/MagicQuery/Twig/StringLoader.php b/src/Mouf/Database/MagicQuery/Twig/StringLoader.php index 2f80793..7e22074 100644 --- a/src/Mouf/Database/MagicQuery/Twig/StringLoader.php +++ b/src/Mouf/Database/MagicQuery/Twig/StringLoader.php @@ -19,13 +19,8 @@ class StringLoader implements LoaderInterface { /** * {@inheritdoc} - */ - public function getSource($name) - { - return $name; - } - /** - * {@inheritdoc} + * + * @param string $name */ public function getCacheKey($name): string { @@ -33,6 +28,9 @@ public function getCacheKey($name): string } /** * {@inheritdoc} + * + * @param string $name + * @param int $time */ public function isFresh($name, $time): bool { diff --git a/src/Mouf/Database/QueryWriter/QueryResult.php b/src/Mouf/Database/QueryWriter/QueryResult.php index 4b44f6b..d0287b3 100644 --- a/src/Mouf/Database/QueryWriter/QueryResult.php +++ b/src/Mouf/Database/QueryWriter/QueryResult.php @@ -44,8 +44,10 @@ class QueryResult implements ArrayValueInterface, PaginableInterface, SortableIn */ private $parameters = array(); - private $limit; + /** @var int|null */ private $offset; + /** @var int|null */ + private $limit; /** * @param Select $select @@ -62,7 +64,7 @@ public function __construct(Select $select, Connection $connection) * * @param array|array|ArrayValueInterface $parameters */ - public function setParameters($parameters) + public function setParameters($parameters): void { $this->parameters = $parameters; } @@ -98,7 +100,7 @@ public function toSql() * @param int $limit * @param int $offset */ - public function paginate($limit, $offset = 0) + public function paginate($limit, $offset = 0): void { $this->limit = $limit; $this->offset = $offset; @@ -107,7 +109,7 @@ public function paginate($limit, $offset = 0) /* (non-PHPdoc) * @see \Mouf\Utils\Common\SortableInterface::sort() */ - public function sort($key, $direction = SortableInterface::ASC) + public function sort($key, $direction = SortableInterface::ASC): void { $result = $this->findColumnByKey($key); if ($result != null) { diff --git a/src/Mouf/Database/QueryWriter/ResultSet.php b/src/Mouf/Database/QueryWriter/ResultSet.php index fbb8212..5cffdec 100644 --- a/src/Mouf/Database/QueryWriter/ResultSet.php +++ b/src/Mouf/Database/QueryWriter/ResultSet.php @@ -2,7 +2,8 @@ namespace Mouf\Database\QueryWriter; -use Doctrine\DBAL\Statement; +use Doctrine\DBAL\FetchMode; +use Doctrine\DBAL\Driver\Statement; /** * Wraps the results of a PDOStatement. @@ -11,20 +12,20 @@ */ class ResultSet implements \Iterator { - /** - * @var \PDOStatement|Statement - */ + /** @var Statement */ private $statement; - private $castToClass; + /** @var int */ private $key = 0; + /** @var array|false */ private $result; + /** @var bool */ private $fetched = false; + /** @var int */ private $rewindCalls = 0; - public function __construct($statement, $castToClass = '') + public function __construct(Statement $statement) { $this->statement = $statement; - $this->castToClass = $castToClass; } public function rewind() @@ -35,6 +36,9 @@ public function rewind() } } + /** + * @return array|false + */ public function current() { if (!$this->fetched) { @@ -44,6 +48,9 @@ public function current() return $this->result; } + /** + * @return int + */ public function key() { return $this->key; @@ -56,9 +63,9 @@ public function next() $this->fetch(); } - private function fetch() + private function fetch(): void { - $this->result = $this->statement->fetch(\PDO::FETCH_ASSOC); + $this->result = $this->statement->fetch(FetchMode::ASSOCIATIVE); $this->fetched = true; } diff --git a/src/Mouf/Database/QueryWriter/Utils/DbHelper.php b/src/Mouf/Database/QueryWriter/Utils/DbHelper.php index 5cb2c75..bab502a 100644 --- a/src/Mouf/Database/QueryWriter/Utils/DbHelper.php +++ b/src/Mouf/Database/QueryWriter/Utils/DbHelper.php @@ -2,14 +2,15 @@ namespace Mouf\Database\QueryWriter\Utils; +use Doctrine\DBAL\Connection; use Mouf\MoufManager; class DbHelper { - public static function getAll($sql, $offset, $limit) + public static function getAll(string $sql, ?int $offset, ?int $limit) { + /** @var Connection $dbalConnection */ $dbalConnection = MoufManager::getMoufManager()->get('dbalConnection'); - /* @var $dbalConnection \Doctrine\DBAL\Connection */ $sql .= self::getFromLimitString($offset, $limit); $statement = $dbalConnection->executeQuery($sql); $results = $statement->fetchAll(); @@ -23,15 +24,13 @@ public static function getAll($sql, $offset, $limit) return $array; } - public static function getFromLimitString($from = null, $limit = null) + public static function getFromLimitString(?int $from = null, ?int $limit = null): string { if ($limit !== null) { - $limitInt = (int) $limit; - $queryStr = ' LIMIT '.$limitInt; + $queryStr = ' LIMIT '.$limit; if ($from !== null) { - $fromInt = (int) $from; - $queryStr .= ' OFFSET '.$fromInt; + $queryStr .= ' OFFSET '.$from; } return $queryStr; diff --git a/src/Mouf/Database/QueryWriter/Utils/FindParametersService.php b/src/Mouf/Database/QueryWriter/Utils/FindParametersService.php index 2ddb0ee..628251c 100644 --- a/src/Mouf/Database/QueryWriter/Utils/FindParametersService.php +++ b/src/Mouf/Database/QueryWriter/Utils/FindParametersService.php @@ -25,7 +25,7 @@ public static function findParameters(MoufInstanceDescriptor $instanceDescriptor * @param MoufInstanceDescriptor $instanceDescriptor * @param array $visitedInstances The list of names of visited instances. */ - private static function recursiveFindParameters(MoufInstanceDescriptor $instanceDescriptor, array $visitedInstances = array(), array $foundParameters = array()) + private static function recursiveFindParameters(MoufInstanceDescriptor $instanceDescriptor, array $visitedInstances = array(), array $foundParameters = array()): array { if (isset($visitedInstances[$instanceDescriptor->getIdentifierName()])) { return array($visitedInstances, $foundParameters); diff --git a/src/SQLParser/Node/AbstractInListOperator.php b/src/SQLParser/Node/AbstractInListOperator.php index 69a2949..6519bab 100644 --- a/src/SQLParser/Node/AbstractInListOperator.php +++ b/src/SQLParser/Node/AbstractInListOperator.php @@ -12,7 +12,7 @@ */ abstract class AbstractInListOperator extends AbstractTwoOperandsOperator { - protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true) + protected function getSql(array $parameters, AbstractPlatform $platform, int $indent = 0, int $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): string { $rightOperand = $this->getRightOperand(); diff --git a/src/SQLParser/Node/AbstractManyInstancesOperator.php b/src/SQLParser/Node/AbstractManyInstancesOperator.php index bcc1bdc..2827426 100644 --- a/src/SQLParser/Node/AbstractManyInstancesOperator.php +++ b/src/SQLParser/Node/AbstractManyInstancesOperator.php @@ -15,9 +15,10 @@ */ abstract class AbstractManyInstancesOperator implements NodeInterface { + /** @var array */ private $operands; - public function getOperands() + public function getOperands(): array { return $this->operands; } @@ -30,7 +31,7 @@ public function getOperands() * * @param array $operands */ - public function setOperands($operands) + public function setOperands($operands): void { if (!is_array($operands)) { $operands = array($operands); @@ -108,5 +109,5 @@ public function walk(VisitorInterface $visitor) * * @return string */ - abstract protected function getOperatorSymbol(); + abstract protected function getOperatorSymbol(): string; } diff --git a/src/SQLParser/Node/AbstractTwoOperandsOperator.php b/src/SQLParser/Node/AbstractTwoOperandsOperator.php index 390c08c..890f3ad 100644 --- a/src/SQLParser/Node/AbstractTwoOperandsOperator.php +++ b/src/SQLParser/Node/AbstractTwoOperandsOperator.php @@ -18,8 +18,12 @@ abstract class AbstractTwoOperandsOperator implements NodeInterface { use ConditionTrait; + /** @var NodeInterface|NodeInterface[]|string */ private $leftOperand; + /** + * @return NodeInterface|NodeInterface[]|string + */ public function getLeftOperand() { return $this->leftOperand; @@ -37,8 +41,12 @@ public function setLeftOperand($leftOperand) $this->leftOperand = $leftOperand; } + /** @var NodeInterface|NodeInterface[]|string */ private $rightOperand; + /** + * @return NodeInterface|NodeInterface[]|string + */ public function getRightOperand() { return $this->rightOperand; @@ -122,7 +130,7 @@ public function toSql(array $parameters, AbstractPlatform $platform, int $indent return $sql; } - protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true) + protected function getSql(array $parameters, AbstractPlatform $platform, int $indent = 0, int $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): string { $sql = NodeFactory::toSql($this->leftOperand, $platform, $parameters, ' ', false, $indent, $conditionsMode, $extrapolateParameters); $sql .= ' '.$this->getOperatorSymbol().' '; @@ -165,5 +173,5 @@ public function walk(VisitorInterface $visitor) /** * Returns the symbol for this operator. */ - abstract protected function getOperatorSymbol(); + abstract protected function getOperatorSymbol(): string; } diff --git a/src/SQLParser/Node/AggregateFunction.php b/src/SQLParser/Node/AggregateFunction.php index 248b484..2c675a6 100644 --- a/src/SQLParser/Node/AggregateFunction.php +++ b/src/SQLParser/Node/AggregateFunction.php @@ -45,6 +45,7 @@ */ class AggregateFunction implements NodeInterface { + /** @var string */ private $functionName; /** @@ -64,13 +65,17 @@ public function getFunctionName() * * @param string $functionName */ - public function setFunctionName($functionName) + public function setFunctionName($functionName): void { $this->functionName = $functionName; } + /** @var NodeInterface[] */ private $subTree; + /** + * @return NodeInterface[] + */ public function getSubTree() { return $this->subTree; @@ -83,13 +88,17 @@ public function getSubTree() * * @param array $subTree */ - public function setSubTree($subTree) + public function setSubTree($subTree): void { $this->subTree = $subTree; } + /** @var string */ private $alias; + /** + * @return string + */ public function getAlias() { return $this->alias; @@ -98,15 +107,19 @@ public function getAlias() /** * Sets the alias. * - * @param string $alias + * @param string|array $alias */ - public function setAlias($alias) + public function setAlias($alias): void { - $this->alias = $alias; + $this->alias = is_array($alias) ? $alias['name'] : $alias; } + /** @var string */ private $direction; + /** + * @return string + */ public function getDirection() { return $this->direction; @@ -119,7 +132,7 @@ public function getDirection() * * @param string $direction */ - public function setDirection($direction) + public function setDirection($direction): void { $this->direction = $direction; } @@ -160,10 +173,7 @@ public function toSql(array $parameters, AbstractPlatform $platform, int $indent $sql .= $subTreeSql; $sql .= ')'; if ($this->alias) { - // defensive fix: - $alias = is_array($this->alias) ? $this->alias['name'] : $this->alias; - - $sql .= ' AS '.$alias; + $sql .= ' AS '.$this->alias; } if ($this->direction) { $sql .= ' '.$this->direction; diff --git a/src/SQLParser/Node/AndOp.php b/src/SQLParser/Node/AndOp.php index 67d4cdc..3d375ad 100644 --- a/src/SQLParser/Node/AndOp.php +++ b/src/SQLParser/Node/AndOp.php @@ -44,7 +44,7 @@ class AndOp extends AbstractManyInstancesOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'AND'; } diff --git a/src/SQLParser/Node/Between.php b/src/SQLParser/Node/Between.php index d19e0a2..88c6886 100644 --- a/src/SQLParser/Node/Between.php +++ b/src/SQLParser/Node/Between.php @@ -17,8 +17,12 @@ */ class Between implements NodeInterface { + /** @var NodeInterface|NodeInterface[]|string */ private $leftOperand; + /** + * @return NodeInterface|NodeInterface[]|string + */ public function getLeftOperand() { return $this->leftOperand; @@ -73,7 +77,7 @@ public function getMaxValueOperand() /** * @param NodeInterface $maxValueOperand */ - public function setMaxValueOperand(NodeInterface $maxValueOperand) + public function setMaxValueOperand(NodeInterface $maxValueOperand): void { $this->maxValueOperand = $maxValueOperand; } @@ -88,9 +92,9 @@ public function setMaxValueOperand(NodeInterface $maxValueOperand) * * @Important IfSet * - * @param ConditionInterface $minValueCondition + * @param ConditionInterface|null $minValueCondition */ - public function setMinValueCondition(ConditionInterface $minValueCondition = null) + public function setMinValueCondition(ConditionInterface $minValueCondition = null): void { $this->minValueCondition = $minValueCondition; } @@ -105,9 +109,9 @@ public function setMinValueCondition(ConditionInterface $minValueCondition = nul * * @Important IfSet * - * @param ConditionInterface $maxValueCondition + * @param ConditionInterface|null $maxValueCondition */ - public function setMaxValueCondition(ConditionInterface $maxValueCondition = null) + public function setMaxValueCondition(ConditionInterface $maxValueCondition = null): void { $this->maxValueCondition = $maxValueCondition; } diff --git a/src/SQLParser/Node/BitwiseAnd.php b/src/SQLParser/Node/BitwiseAnd.php index 49d437b..1d052ec 100644 --- a/src/SQLParser/Node/BitwiseAnd.php +++ b/src/SQLParser/Node/BitwiseAnd.php @@ -14,7 +14,7 @@ class BitwiseAnd extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '&'; } diff --git a/src/SQLParser/Node/BitwiseOr.php b/src/SQLParser/Node/BitwiseOr.php index 8133fdd..8d3f3f1 100644 --- a/src/SQLParser/Node/BitwiseOr.php +++ b/src/SQLParser/Node/BitwiseOr.php @@ -14,7 +14,7 @@ class BitwiseOr extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '|'; } diff --git a/src/SQLParser/Node/BitwiseXor.php b/src/SQLParser/Node/BitwiseXor.php index 8ebc1c4..ba7f3b9 100644 --- a/src/SQLParser/Node/BitwiseXor.php +++ b/src/SQLParser/Node/BitwiseXor.php @@ -14,7 +14,7 @@ class BitwiseXor extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '^'; } diff --git a/src/SQLParser/Node/CaseOperation.php b/src/SQLParser/Node/CaseOperation.php index 8d4923d..5165d0f 100644 --- a/src/SQLParser/Node/CaseOperation.php +++ b/src/SQLParser/Node/CaseOperation.php @@ -15,8 +15,12 @@ */ class CaseOperation implements NodeInterface { + /** @var NodeInterface|NodeInterface[]|string */ private $operation; + /** + * @return NodeInterface|NodeInterface[]|string + */ public function getOperation() { return $this->operation; @@ -29,7 +33,7 @@ public function getOperation() * * @param NodeInterface|NodeInterface[]|string $operation */ - public function setOperation($operation) + public function setOperation($operation): void { $this->operation = $operation; } diff --git a/src/SQLParser/Node/ColRef.php b/src/SQLParser/Node/ColRef.php index cf76ce9..2fea15a 100644 --- a/src/SQLParser/Node/ColRef.php +++ b/src/SQLParser/Node/ColRef.php @@ -44,6 +44,7 @@ */ class ColRef implements NodeInterface { + /** @var mixed */ private $database; /** @@ -61,11 +62,12 @@ public function getDatabase() * * @param mixed $database */ - public function setDatabase($database) + public function setDatabase($database): void { $this->database = $database; } + /** @var string */ private $table; /** @@ -85,11 +87,12 @@ public function getTable() * * @param string $table */ - public function setTable($table) + public function setTable($table): void { $this->table = $table; } + /** @var string */ private $column; /** @@ -109,11 +112,12 @@ public function getColumn() * * @param string $column */ - public function setColumn($column) + public function setColumn($column): void { $this->column = $column; } + /** @var string */ private $alias; /** @@ -133,11 +137,12 @@ public function getAlias() * * @param string $alias */ - public function setAlias($alias) + public function setAlias($alias): void { $this->alias = $alias; } + /** @var string */ private $direction; /** @@ -157,7 +162,7 @@ public function getDirection() * * @param string $direction */ - public function setDirection($direction) + public function setDirection($direction): void { $this->direction = $direction; } diff --git a/src/SQLParser/Node/ConstNode.php b/src/SQLParser/Node/ConstNode.php index 9f97b0c..0b2231f 100644 --- a/src/SQLParser/Node/ConstNode.php +++ b/src/SQLParser/Node/ConstNode.php @@ -44,9 +44,14 @@ */ class ConstNode implements NodeInterface { + /** @var string */ private $value; + /** @var bool */ private $isString = true; + /** + * @return string + */ public function getValue() { return $this->value; @@ -59,13 +64,13 @@ public function getValue() * * @param string $value */ - public function setValue($value) + public function setValue($value): void { $this->value = $value; } /** - * @return mixed + * @return bool */ public function getIsString() { @@ -73,9 +78,9 @@ public function getIsString() } /** - * @param mixed $isString + * @param bool $isString */ - public function setIsString($isString) + public function setIsString($isString): void { $this->isString = $isString; } diff --git a/src/SQLParser/Node/Different.php b/src/SQLParser/Node/Different.php index 23ce118..56a4600 100644 --- a/src/SQLParser/Node/Different.php +++ b/src/SQLParser/Node/Different.php @@ -16,12 +16,12 @@ class Different extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '<>'; } - protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true) + protected function getSql(array $parameters, AbstractPlatform $platform, int $indent = 0, int $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): string { $rightOperand = $this->getRightOperand(); if ($rightOperand instanceof Parameter && !isset($parameters[$rightOperand->getName()])) { diff --git a/src/SQLParser/Node/Div.php b/src/SQLParser/Node/Div.php index 5b369c6..a76ea1d 100644 --- a/src/SQLParser/Node/Div.php +++ b/src/SQLParser/Node/Div.php @@ -14,7 +14,7 @@ class Div extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'DIV'; } diff --git a/src/SQLParser/Node/Divide.php b/src/SQLParser/Node/Divide.php index d6dd340..6760295 100644 --- a/src/SQLParser/Node/Divide.php +++ b/src/SQLParser/Node/Divide.php @@ -14,7 +14,7 @@ class Divide extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '/'; } diff --git a/src/SQLParser/Node/ElseOperation.php b/src/SQLParser/Node/ElseOperation.php index 40407d0..5441d32 100644 --- a/src/SQLParser/Node/ElseOperation.php +++ b/src/SQLParser/Node/ElseOperation.php @@ -14,7 +14,7 @@ class ElseOperation extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'ELSE'; } diff --git a/src/SQLParser/Node/Equal.php b/src/SQLParser/Node/Equal.php index 5af09b0..6e53db9 100644 --- a/src/SQLParser/Node/Equal.php +++ b/src/SQLParser/Node/Equal.php @@ -14,12 +14,12 @@ class Equal extends AbstractTwoOperandsOperator /** * Returns the symbol for this operator. */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '='; } - protected function getSql(array $parameters, AbstractPlatform $platform, $indent = 0, $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true) + protected function getSql(array $parameters, AbstractPlatform $platform, int $indent = 0, int $conditionsMode = self::CONDITION_APPLY, bool $extrapolateParameters = true): string { $rightOperand = $this->getRightOperand(); if ($rightOperand instanceof Parameter && !isset($parameters[$rightOperand->getName()])) { diff --git a/src/SQLParser/Node/Expression.php b/src/SQLParser/Node/Expression.php index 5a4761d..887c4bc 100644 --- a/src/SQLParser/Node/Expression.php +++ b/src/SQLParser/Node/Expression.php @@ -46,6 +46,7 @@ */ class Expression implements NodeInterface, BypassableInterface { + /** @var string */ private $baseExpression; /** @@ -63,13 +64,17 @@ public function getBaseExpression() * * @param string $baseExpression */ - public function setBaseExpression($baseExpression) + public function setBaseExpression($baseExpression): void { $this->baseExpression = $baseExpression; } + /** @var NodeInterface[]|NodeInterface */ private $subTree; + /** + * @return NodeInterface|NodeInterface[] + */ public function getSubTree() { return $this->subTree; @@ -82,14 +87,17 @@ public function getSubTree() * * @param array|NodeInterface $subTree */ - public function setSubTree($subTree) + public function setSubTree($subTree): void { - $this->subTree = $subTree; - $this->subTree = NodeFactory::simplify($this->subTree); + $this->subTree = NodeFactory::simplify($subTree); } + /** @var string */ private $alias; + /** + * @return string + */ public function getAlias() { return $this->alias; @@ -102,13 +110,17 @@ public function getAlias() * * @param string $alias */ - public function setAlias($alias) + public function setAlias($alias): void { $this->alias = $alias; } + /** @var string */ private $direction; + /** + * @return string + */ public function getDiretion() { return $this->direction; @@ -121,11 +133,12 @@ public function getDiretion() * * @param string $direction */ - public function setDirection($direction) + public function setDirection($direction): void { $this->direction = $direction; } + /** @var bool */ private $brackets = false; /** @@ -145,11 +158,12 @@ public function hasBrackets() * * @param bool $brackets */ - public function setBrackets($brackets) + public function setBrackets($brackets): void { $this->brackets = $brackets; } + /** @var string */ private $delimiter = ' '; /** @@ -165,7 +179,7 @@ public function getDelimiter() * * @param string $delimiter */ - public function setDelimiter($delimiter) + public function setDelimiter($delimiter): void { $this->delimiter = $delimiter; } diff --git a/src/SQLParser/Node/Greater.php b/src/SQLParser/Node/Greater.php index 1347bf4..6ad8b20 100644 --- a/src/SQLParser/Node/Greater.php +++ b/src/SQLParser/Node/Greater.php @@ -14,7 +14,7 @@ class Greater extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '>'; } diff --git a/src/SQLParser/Node/GreaterOrEqual.php b/src/SQLParser/Node/GreaterOrEqual.php index e9bcbdd..9f9484b 100644 --- a/src/SQLParser/Node/GreaterOrEqual.php +++ b/src/SQLParser/Node/GreaterOrEqual.php @@ -14,7 +14,7 @@ class GreaterOrEqual extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '>='; } diff --git a/src/SQLParser/Node/In.php b/src/SQLParser/Node/In.php index f8c3448..4c90592 100644 --- a/src/SQLParser/Node/In.php +++ b/src/SQLParser/Node/In.php @@ -16,7 +16,7 @@ class In extends AbstractInListOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'IN'; } diff --git a/src/SQLParser/Node/Is.php b/src/SQLParser/Node/Is.php index 0a4557c..6d3d43d 100644 --- a/src/SQLParser/Node/Is.php +++ b/src/SQLParser/Node/Is.php @@ -14,7 +14,7 @@ class Is extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'IS'; } diff --git a/src/SQLParser/Node/IsNot.php b/src/SQLParser/Node/IsNot.php index 4e02e90..2f245e7 100644 --- a/src/SQLParser/Node/IsNot.php +++ b/src/SQLParser/Node/IsNot.php @@ -14,7 +14,7 @@ class IsNot extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'IS NOT'; } diff --git a/src/SQLParser/Node/Less.php b/src/SQLParser/Node/Less.php index 492bbcc..54ee1a1 100644 --- a/src/SQLParser/Node/Less.php +++ b/src/SQLParser/Node/Less.php @@ -14,7 +14,7 @@ class Less extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '<'; } diff --git a/src/SQLParser/Node/LessOrEqual.php b/src/SQLParser/Node/LessOrEqual.php index 26379fd..e555a5b 100644 --- a/src/SQLParser/Node/LessOrEqual.php +++ b/src/SQLParser/Node/LessOrEqual.php @@ -14,7 +14,7 @@ class LessOrEqual extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '<='; } diff --git a/src/SQLParser/Node/Like.php b/src/SQLParser/Node/Like.php index b892aaa..f000cd7 100644 --- a/src/SQLParser/Node/Like.php +++ b/src/SQLParser/Node/Like.php @@ -14,7 +14,7 @@ class Like extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'LIKE'; } diff --git a/src/SQLParser/Node/LimitNode.php b/src/SQLParser/Node/LimitNode.php index 1a70261..c262dc6 100644 --- a/src/SQLParser/Node/LimitNode.php +++ b/src/SQLParser/Node/LimitNode.php @@ -44,8 +44,12 @@ */ class LimitNode implements NodeInterface { + /** @var string */ private $value; + /** + * @return string + */ public function getValue() { return $this->value; @@ -58,7 +62,7 @@ public function getValue() * * @param string $value */ - public function setValue($value) + public function setValue($value): void { $this->value = $value; } diff --git a/src/SQLParser/Node/Minus.php b/src/SQLParser/Node/Minus.php index f178fa3..623bb85 100644 --- a/src/SQLParser/Node/Minus.php +++ b/src/SQLParser/Node/Minus.php @@ -14,7 +14,7 @@ class Minus extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '-'; } diff --git a/src/SQLParser/Node/Modulo.php b/src/SQLParser/Node/Modulo.php index 91f74a3..3683038 100644 --- a/src/SQLParser/Node/Modulo.php +++ b/src/SQLParser/Node/Modulo.php @@ -14,7 +14,7 @@ class Modulo extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '%'; } diff --git a/src/SQLParser/Node/Multiply.php b/src/SQLParser/Node/Multiply.php index 6698f47..4ca2a75 100644 --- a/src/SQLParser/Node/Multiply.php +++ b/src/SQLParser/Node/Multiply.php @@ -14,7 +14,7 @@ class Multiply extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '*'; } diff --git a/src/SQLParser/Node/NodeFactory.php b/src/SQLParser/Node/NodeFactory.php index 8384326..63d8965 100644 --- a/src/SQLParser/Node/NodeFactory.php +++ b/src/SQLParser/Node/NodeFactory.php @@ -556,6 +556,8 @@ public static function mapArrayToNodeObjectList(array $items) * Takes an array of nodes (including operators) and try to build a tree from it. * * @param NodeInterface[]|NodeInterface $nodes + * + * @return NodeInterface[]|NodeInterface */ public static function simplify($nodes) { @@ -571,7 +573,8 @@ public static function simplify($nodes) // Let's transform "NOT" + "IN" into "NOT IN" $newNodes = array(); - for ($i = 0; $i < count($nodes); ++$i) { + $nodesCount = count($nodes); + for ($i = 0; $i < $nodesCount; ++$i) { $node = $nodes[$i]; if ($node instanceof Operator && isset($nodes[$i + 1]) && $nodes[$i + 1] instanceof Operator && strtoupper($node->getValue()) == 'IS' && strtoupper($nodes[$i + 1]->getValue()) == 'NOT') { @@ -599,7 +602,8 @@ public static function simplify($nodes) // Handle AGAINST function. Without this patch params will be placed after AGAINST() and not inside the brackets $newNodes = array(); - for ($i = 0; $i < count($nodes); ++$i) { + $nodesCount = count($nodes); + for ($i = 0; $i < $nodesCount; ++$i) { $node = $nodes[$i]; if ($node instanceof SimpleFunction && $node->getBaseExpression() === 'AGAINST' && isset($nodes[$i + 1])) { $node->setSubTree($nodes[$i + 1]); @@ -648,7 +652,7 @@ public static function simplify($nodes) if ($operand instanceof Expression) { if (empty($operand->getBaseExpression())) { $subTree = $operand->getSubTree(); - if (count($subTree) == 1) { + if (count($subTree) === 1) { $newNodes = array_merge($newNodes, self::simplify($subTree)); } else { $newNodes[] = $operand; diff --git a/src/SQLParser/Node/NotIn.php b/src/SQLParser/Node/NotIn.php index 103af1b..36b98b6 100644 --- a/src/SQLParser/Node/NotIn.php +++ b/src/SQLParser/Node/NotIn.php @@ -14,7 +14,7 @@ class NotIn extends AbstractInListOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'NOT IN'; } diff --git a/src/SQLParser/Node/NotLike.php b/src/SQLParser/Node/NotLike.php index 5258399..7c5749f 100644 --- a/src/SQLParser/Node/NotLike.php +++ b/src/SQLParser/Node/NotLike.php @@ -14,7 +14,7 @@ class NotLike extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'NOT LIKE'; } diff --git a/src/SQLParser/Node/NullCompatibleEqual.php b/src/SQLParser/Node/NullCompatibleEqual.php index f49f803..61ac1bb 100644 --- a/src/SQLParser/Node/NullCompatibleEqual.php +++ b/src/SQLParser/Node/NullCompatibleEqual.php @@ -14,7 +14,7 @@ class NullCompatibleEqual extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '<=>'; } diff --git a/src/SQLParser/Node/Operation.php b/src/SQLParser/Node/Operation.php index a9aa43b..fe86789 100644 --- a/src/SQLParser/Node/Operation.php +++ b/src/SQLParser/Node/Operation.php @@ -39,9 +39,13 @@ */ class Operation extends AbstractManyInstancesOperator { + /** @var string */ private $operator; - public function getOperatorSymbol() + /** + * @return string + */ + public function getOperatorSymbol(): string { return $this->operator; } @@ -51,7 +55,7 @@ public function getOperatorSymbol() * * @param string $operator */ - public function setOperatorSymbol($operator) + public function setOperatorSymbol($operator): void { $this->operator = $operator; } diff --git a/src/SQLParser/Node/OrOp.php b/src/SQLParser/Node/OrOp.php index 6885eac..9595e12 100644 --- a/src/SQLParser/Node/OrOp.php +++ b/src/SQLParser/Node/OrOp.php @@ -44,7 +44,7 @@ class OrOp extends AbstractManyInstancesOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'OR'; } diff --git a/src/SQLParser/Node/Parameter.php b/src/SQLParser/Node/Parameter.php index 40186d3..4ced389 100644 --- a/src/SQLParser/Node/Parameter.php +++ b/src/SQLParser/Node/Parameter.php @@ -44,7 +44,9 @@ */ class Parameter implements NodeInterface, BypassableInterface { + /** @var string */ protected $name; + /** @var bool */ protected $discardedOnNull = true; /** @@ -65,7 +67,7 @@ public function getName() * * @param string $name */ - public function setName($name) + public function setName($name): void { if (strrpos($name, '!') === strlen($name) - 1) { $this->name = substr($name, 0, strlen($name) - 1); @@ -102,7 +104,7 @@ public function getAutoPrepend() * * @param string $autoPrepend */ - public function setAutoPrepend($autoPrepend) + public function setAutoPrepend($autoPrepend): self { $this->autoPrepend = $autoPrepend; @@ -125,7 +127,7 @@ public function getAutoAppend() * * @param string $autoAppend */ - public function setAutoAppend($autoAppend) + public function setAutoAppend($autoAppend): self { $this->autoAppend = $autoAppend; diff --git a/src/SQLParser/Node/Plus.php b/src/SQLParser/Node/Plus.php index 0a5b8a5..5636645 100644 --- a/src/SQLParser/Node/Plus.php +++ b/src/SQLParser/Node/Plus.php @@ -14,7 +14,7 @@ class Plus extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '+'; } diff --git a/src/SQLParser/Node/Regexp.php b/src/SQLParser/Node/Regexp.php index 2cf84ad..f10a369 100644 --- a/src/SQLParser/Node/Regexp.php +++ b/src/SQLParser/Node/Regexp.php @@ -14,7 +14,7 @@ class Regexp extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'REGEXP'; } diff --git a/src/SQLParser/Node/Reserved.php b/src/SQLParser/Node/Reserved.php index 8a39f23..dfb671a 100644 --- a/src/SQLParser/Node/Reserved.php +++ b/src/SQLParser/Node/Reserved.php @@ -44,6 +44,7 @@ */ class Reserved implements NodeInterface { + /** @var string */ private $baseExpression; /** @@ -61,11 +62,12 @@ public function getBaseExpression() * * @param string $baseExpression */ - public function setBaseExpression($baseExpression) + public function setBaseExpression($baseExpression): void { $this->baseExpression = $baseExpression; } + /** @var bool */ private $brackets = false; /** @@ -85,7 +87,7 @@ public function hasBrackets() * * @param bool $brackets */ - public function setBrackets($brackets) + public function setBrackets($brackets): void { $this->brackets = $brackets; } diff --git a/src/SQLParser/Node/ShiftLeft.php b/src/SQLParser/Node/ShiftLeft.php index 0d56b9c..3df1c14 100644 --- a/src/SQLParser/Node/ShiftLeft.php +++ b/src/SQLParser/Node/ShiftLeft.php @@ -14,7 +14,7 @@ class ShiftLeft extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '<<'; } diff --git a/src/SQLParser/Node/ShiftRight.php b/src/SQLParser/Node/ShiftRight.php index e6f0eb9..71cc6d6 100644 --- a/src/SQLParser/Node/ShiftRight.php +++ b/src/SQLParser/Node/ShiftRight.php @@ -14,7 +14,7 @@ class ShiftRight extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return '>>'; } diff --git a/src/SQLParser/Node/SimpleFunction.php b/src/SQLParser/Node/SimpleFunction.php index 58568f2..5b40c89 100644 --- a/src/SQLParser/Node/SimpleFunction.php +++ b/src/SQLParser/Node/SimpleFunction.php @@ -45,6 +45,7 @@ */ class SimpleFunction implements NodeInterface { + /** @var string */ private $baseExpression; /** @@ -62,13 +63,17 @@ public function getBaseExpression() * * @param string $baseExpression */ - public function setBaseExpression($baseExpression) + public function setBaseExpression($baseExpression): void { $this->baseExpression = $baseExpression; } + /** @var NodeInterface[]|NodeInterface */ private $subTree; + /** + * @return NodeInterface|NodeInterface[] + */ public function getSubTree() { return $this->subTree; @@ -81,14 +86,17 @@ public function getSubTree() * * @param array|NodeInterface $subTree */ - public function setSubTree($subTree) + public function setSubTree($subTree): void { - $this->subTree = $subTree; - $this->subTree = NodeFactory::simplify($this->subTree); + $this->subTree = NodeFactory::simplify($subTree); } + /** @var string */ private $alias; + /** + * @return string + */ public function getAlias() { return $this->alias; @@ -101,13 +109,17 @@ public function getAlias() * * @param string $alias */ - public function setAlias($alias) + public function setAlias($alias): void { $this->alias = $alias; } + /** @var string */ private $direction; + /** + * @return string + */ public function getDiretion() { return $this->direction; @@ -120,11 +132,12 @@ public function getDiretion() * * @param string $direction */ - public function setDirection($direction) + public function setDirection($direction): void { $this->direction = $direction; } + /** @var bool */ private $brackets = false; /** diff --git a/src/SQLParser/Node/SubQuery.php b/src/SQLParser/Node/SubQuery.php index 327637b..c5aa494 100644 --- a/src/SQLParser/Node/SubQuery.php +++ b/src/SQLParser/Node/SubQuery.php @@ -47,12 +47,13 @@ */ class SubQuery implements NodeInterface { + /** @var Select|Union|NodeInterface */ private $subQuery; /** * Returns the list of subQuery statements. * - * @return Select|Union + * @return Select|Union|NodeInterface */ public function getSubQuery() { @@ -64,11 +65,12 @@ public function getSubQuery() * * @param Select|Union $subQuery */ - public function setSubQuery($subQuery) + public function setSubQuery($subQuery): void { $this->subQuery = $subQuery; } + /** @var string */ private $alias; /** @@ -86,11 +88,12 @@ public function getAlias() * * @param string $alias */ - public function setAlias($alias) + public function setAlias($alias): void { $this->alias = $alias; } + /** @var string */ private $joinType; /** @@ -108,11 +111,12 @@ public function getJoinType() * * @param string $joinType */ - public function setJoinType($joinType) + public function setJoinType($joinType): void { $this->joinType = $joinType; } + /** @var NodeInterface[] */ private $refClause; /** @@ -130,7 +134,7 @@ public function getRefClause() * * @param NodeInterface[] $refClause */ - public function setRefClause($refClause) + public function setRefClause($refClause): void { $this->refClause = $refClause; } diff --git a/src/SQLParser/Node/Table.php b/src/SQLParser/Node/Table.php index 38498cd..8d31021 100644 --- a/src/SQLParser/Node/Table.php +++ b/src/SQLParser/Node/Table.php @@ -45,6 +45,7 @@ */ class Table implements NodeInterface { + /** @var mixed */ private $database; /** @@ -62,11 +63,12 @@ public function getDatabase() * * @param mixed $database */ - public function setDatabase($database) + public function setDatabase($database): void { $this->database = $database; } + /** @var string */ private $table; /** @@ -86,11 +88,12 @@ public function getTable() * * @param string $table */ - public function setTable($table) + public function setTable($table): void { $this->table = $table; } + /** @var string */ private $alias; /** @@ -110,11 +113,12 @@ public function getAlias() * * @param string $alias */ - public function setAlias($alias) + public function setAlias($alias): void { $this->alias = $alias; } + /** @var string */ private $joinType; /** @@ -134,11 +138,12 @@ public function getJoinType() * * @param string $joinType */ - public function setJoinType($joinType) + public function setJoinType($joinType): void { $this->joinType = $joinType; } + /** @var NodeInterface[]|NodeInterface */ private $refClause; /** @@ -158,7 +163,7 @@ public function getRefClause() * * @param NodeInterface[]|NodeInterface $refClause */ - public function setRefClause($refClause) + public function setRefClause($refClause): void { $this->refClause = $refClause; } diff --git a/src/SQLParser/Node/Then.php b/src/SQLParser/Node/Then.php index 8d4b066..7d23de3 100644 --- a/src/SQLParser/Node/Then.php +++ b/src/SQLParser/Node/Then.php @@ -14,7 +14,7 @@ class Then extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'THEN'; } diff --git a/src/SQLParser/Node/Traverser/CompositeVisitor.php b/src/SQLParser/Node/Traverser/CompositeVisitor.php index d26ea95..123f06d 100644 --- a/src/SQLParser/Node/Traverser/CompositeVisitor.php +++ b/src/SQLParser/Node/Traverser/CompositeVisitor.php @@ -24,7 +24,7 @@ class CompositeVisitor implements VisitorInterface * * @param VisitorInterface $visitor */ - public function addVisitor(VisitorInterface $visitor) + public function addVisitor(VisitorInterface $visitor): void { $this->visitors[] = $visitor; array_unshift($this->reverseVisitors, $visitor); @@ -57,12 +57,7 @@ public function enterNode(NodeInterface $node) } /** - * Called on every node when the traverser leaves the node. - * The leaveNode() method can return a changed node, or null if nothing is changed. - * The leaveNode() method can also return the value NodeTraverser::REMOVE_NODE, - * which instructs the traverser to remove the current node. - * - * @param NodeInterface $node + * {@inheritDoc} */ public function leaveNode(NodeInterface $node) { diff --git a/src/SQLParser/Node/Traverser/DetectMagicJoinSelectVisitor.php b/src/SQLParser/Node/Traverser/DetectMagicJoinSelectVisitor.php index 06459b6..c397270 100644 --- a/src/SQLParser/Node/Traverser/DetectMagicJoinSelectVisitor.php +++ b/src/SQLParser/Node/Traverser/DetectMagicJoinSelectVisitor.php @@ -11,15 +11,17 @@ */ class DetectMagicJoinSelectVisitor implements VisitorInterface { + /** @var Select|null */ private $lastVisitedSelect; + /** @var array */ private $magicJoinSelects = array(); /** * Removes all detected magic join selects. * Useful for reusing the visitor instance on another node traversal. */ - public function resetVisitor() + public function resetVisitor(): void { $this->magicJoinSelects = array(); $this->lastVisitedSelect = null; @@ -61,14 +63,10 @@ public function enterNode(NodeInterface $node) } /** - * Called on every node when the traverser leaves the node. - * The leaveNode() method can return a changed node, or null if nothing is changed. - * The leaveNode() method can also return the value NodeTraverser::REMOVE_NODE, - * which instructs the traverser to remove the current node. - * - * @param NodeInterface $node + * {@inheritDoc} */ public function leaveNode(NodeInterface $node) { + return null; } } diff --git a/src/SQLParser/Node/Traverser/DetectTablesVisitor.php b/src/SQLParser/Node/Traverser/DetectTablesVisitor.php index 0dbc5e9..a8fbd20 100644 --- a/src/SQLParser/Node/Traverser/DetectTablesVisitor.php +++ b/src/SQLParser/Node/Traverser/DetectTablesVisitor.php @@ -12,8 +12,10 @@ */ class DetectTablesVisitor implements VisitorInterface { + /** @var bool */ private $isSelectVisited = false; + /** @var array */ private $tables = array(); private $defaultTable; @@ -22,7 +24,7 @@ class DetectTablesVisitor implements VisitorInterface * Removes all detected magic join selects. * Useful for reusing the visitor instance on another node traversal. */ - public function resetVisitor() + public function resetVisitor(): void { $this->tables = array(); $this->isSelectVisited = false; @@ -75,15 +77,10 @@ public function enterNode(NodeInterface $node) } /** - * Called on every node when the traverser leaves the node. - * The leaveNode() method can return a changed node, or null if nothing is changed. - * The leaveNode() method can also return the value NodeTraverser::REMOVE_NODE, - * which instructs the traverser to remove the current node. - * - * @param NodeInterface $node + * {@inheritDoc} */ public function leaveNode(NodeInterface $node) { - return; + return null; } } diff --git a/src/SQLParser/Node/Traverser/NodeTraverser.php b/src/SQLParser/Node/Traverser/NodeTraverser.php index 4654341..624547c 100644 --- a/src/SQLParser/Node/Traverser/NodeTraverser.php +++ b/src/SQLParser/Node/Traverser/NodeTraverser.php @@ -24,7 +24,7 @@ public function __construct() * * @param VisitorInterface $visitor */ - public function addVisitor(VisitorInterface $visitor) + public function addVisitor(VisitorInterface $visitor): void { $this->visitor->addVisitor($visitor); } @@ -34,7 +34,7 @@ public function addVisitor(VisitorInterface $visitor) * * @param NodeInterface $node */ - public function walk(NodeInterface $node) + public function walk(NodeInterface $node): void { $node->walk($this->visitor); } diff --git a/src/SQLParser/Node/Traverser/VisitorInterface.php b/src/SQLParser/Node/Traverser/VisitorInterface.php index 81ae6c3..e654d48 100644 --- a/src/SQLParser/Node/Traverser/VisitorInterface.php +++ b/src/SQLParser/Node/Traverser/VisitorInterface.php @@ -29,6 +29,8 @@ public function enterNode(NodeInterface $node); * which instructs the traverser to remove the current node. * * @param NodeInterface $node + * + * @return NodeInterface|string|null */ public function leaveNode(NodeInterface $node); } diff --git a/src/SQLParser/Node/WhenConditions.php b/src/SQLParser/Node/WhenConditions.php index 8b7c422..11bb62b 100644 --- a/src/SQLParser/Node/WhenConditions.php +++ b/src/SQLParser/Node/WhenConditions.php @@ -11,8 +11,12 @@ */ class WhenConditions extends AbstractManyInstancesOperator { + /** @var NodeInterface|NodeInterface[]|string */ private $value; + /** + * @return NodeInterface|NodeInterface[]|string + */ public function getValue() { return $this->value; @@ -25,7 +29,7 @@ public function getValue() * * @param NodeInterface|NodeInterface[]|string $value */ - public function setValue($value) + public function setValue($value): void { $this->value = $value; } @@ -64,7 +68,7 @@ public function toSql(array $parameters, AbstractPlatform $platform, int $indent * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'WHEN'; } diff --git a/src/SQLParser/Node/XorOp.php b/src/SQLParser/Node/XorOp.php index 26f98ed..4533152 100644 --- a/src/SQLParser/Node/XorOp.php +++ b/src/SQLParser/Node/XorOp.php @@ -14,7 +14,7 @@ class XorOp extends AbstractTwoOperandsOperator * * @return string */ - protected function getOperatorSymbol() + protected function getOperatorSymbol(): string { return 'XOR'; } diff --git a/src/SQLParser/Query/Select.php b/src/SQLParser/Query/Select.php index d737b15..e7b6079 100644 --- a/src/SQLParser/Query/Select.php +++ b/src/SQLParser/Query/Select.php @@ -54,6 +54,7 @@ */ class Select implements StatementInterface, NodeInterface { + /** @var bool */ private $distinct; /** @@ -71,11 +72,12 @@ public function getDistinct() * * @param bool $distinct */ - public function setDistinct($distinct) + public function setDistinct($distinct): void { $this->distinct = $distinct; } + /** @var NodeInterface[] */ private $columns; /** @@ -93,11 +95,12 @@ public function getColumns() * * @param NodeInterface[] $columns */ - public function setColumns($columns) + public function setColumns($columns): void { $this->columns = $columns; } + /** @var NodeInterface[] */ private $from; /** @@ -115,17 +118,18 @@ public function getFrom() * * @param NodeInterface[] $from */ - public function setFrom($from) + public function setFrom($from): void { $this->from = $from; } + /** @var NodeInterface[]|NodeInterface */ private $where; /** * Returns the list of where statements. * - * @return NodeInterface[] + * @return NodeInterface[]|NodeInterface */ public function getWhere() { @@ -137,17 +141,18 @@ public function getWhere() * * @param NodeInterface[]|NodeInterface $where */ - public function setWhere($where) + public function setWhere($where): void { $this->where = $where; } + /** @var NodeInterface[]|NodeInterface */ private $group; /** * Returns the list of group statements. * - * @return NodeInterface[] + * @return NodeInterface[]|NodeInterface */ public function getGroup() { @@ -159,11 +164,12 @@ public function getGroup() * * @param NodeInterface[]|NodeInterface $group */ - public function setGroup($group) + public function setGroup($group): void { $this->group = $group; } + /** @var NodeInterface[]|NodeInterface */ private $having; /** @@ -181,11 +187,12 @@ public function getHaving() * * @param NodeInterface[]|NodeInterface $having */ - public function setHaving($having) + public function setHaving($having): void { $this->having = $having; } + /** @var NodeInterface[]|NodeInterface */ private $order; /** @@ -203,11 +210,12 @@ public function getOrder() * * @param NodeInterface[]|NodeInterface $order */ - public function setOrder($order) + public function setOrder($order): void { $this->order = $order; } + /** @var string[] */ private $options; /** @@ -225,11 +233,12 @@ public function getOptions() * * @param string[] $options */ - public function setOptions($options) + public function setOptions($options): void { $this->options = $options; } + /** @var NodeInterface */ private $limit; /** @@ -243,11 +252,12 @@ public function getLimit() /** * @param NodeInterface $limit */ - public function setLimit($limit) + public function setLimit($limit): void { $this->limit = $limit; } + /** @var NodeInterface */ private $offset; /** @@ -261,7 +271,7 @@ public function getOffset() /** * @param NodeInterface $offset */ - public function setOffset($offset) + public function setOffset($offset): void { $this->offset = $offset; } @@ -435,7 +445,7 @@ public function walk(VisitorInterface $visitor) return $visitor->leaveNode($node); } - private function walkChildren(&$children, VisitorInterface $visitor) + private function walkChildren(&$children, VisitorInterface $visitor): void { if ($children) { if (is_array($children)) { diff --git a/src/SQLParser/Query/StatementFactory.php b/src/SQLParser/Query/StatementFactory.php index a3cb5ee..e5cebaf 100644 --- a/src/SQLParser/Query/StatementFactory.php +++ b/src/SQLParser/Query/StatementFactory.php @@ -2,6 +2,7 @@ namespace SQLParser\Query; +use Mouf\Database\MagicQueryException; use SQLParser\Node\NodeFactory; use SQLParser\Node\Operator; use SQLParser\Node\Reserved; @@ -13,6 +14,10 @@ */ class StatementFactory { + /** + * @return Select|Union + * @throws MagicQueryException + */ public static function toObject(array $desc) { if (isset($desc['SELECT'])) { @@ -88,6 +93,7 @@ public static function toObject(array $desc) return $select; } elseif (isset($desc['UNION'])) { + /** @var Select[] $selects */ $selects = array_map([self::class, 'toObject'], $desc['UNION']); return new Union($selects); diff --git a/src/SQLParser/Query/Union.php b/src/SQLParser/Query/Union.php index 762e2a1..255ecf3 100644 --- a/src/SQLParser/Query/Union.php +++ b/src/SQLParser/Query/Union.php @@ -105,10 +105,10 @@ public function walk(VisitorInterface $visitor) } /** - * @param (Select|null)[] $children + * @param array $children * @param VisitorInterface $visitor */ - private function walkChildren(array &$children, VisitorInterface $visitor) + private function walkChildren(array &$children, VisitorInterface $visitor): void { if ($children) { foreach ($children as $key => $operand) { diff --git a/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php b/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php index 742daec..7d2fdc5 100644 --- a/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php +++ b/tests/Mouf/Database/MagicQuery/Twig/SqlTwigEnvironmentFactoryTest.php @@ -4,12 +4,10 @@ use Mouf\Database\MagicQuery\Twig\SqlTwigEnvironmentFactory; use PHPUnit\Framework\TestCase; +use Twig\Error\RuntimeError; class SqlTwigEnvironmentFactoryTest extends TestCase { - /** - * - */ public function testIf() { $twig = SqlTwigEnvironmentFactory::getTwigEnvironment(); @@ -17,12 +15,10 @@ public function testIf() $this->assertEquals('SELECT * FROM toto WHERE id = :id', $sql); } - /** - * @expectedException \Twig\Error\RuntimeError - */ public function testException() { $twig = SqlTwigEnvironmentFactory::getTwigEnvironment(); + $this->expectException(RuntimeError::class); $twig->render('SELECT * FROM toto WHERE id = {{ id }}', ['id' => 'hello']); } } diff --git a/tests/Mouf/Database/MagicQueryTest.php b/tests/Mouf/Database/MagicQueryTest.php index 4fd9b3e..47e2d43 100644 --- a/tests/Mouf/Database/MagicQueryTest.php +++ b/tests/Mouf/Database/MagicQueryTest.php @@ -210,33 +210,29 @@ public function testStandardSelect() $this->assertEquals($sql, self::simplifySql($magicQuery->build($sql))); } - /** - * @expectedException \Mouf\Database\MagicQueryException - */ public function testInNullException() { $magicQuery = new MagicQuery(); $sql = 'SELECT * FROM users WHERE status IN :statuses!'; + $this->expectException(MagicQueryException::class); $magicQuery->build($sql, ['statuses' => NULL]); } - /** - * @expectedException \Mouf\Database\MagicQueryException - */ public function testInvalidSql() { $magicQuery = new MagicQuery(); $sql = 'SELECT * FROM users WHERE date_end => :startDate'; + $this->expectException(MagicQueryException::class); $this->assertEquals('SELECT * FROM users WHERE date_end => \'2014-06-06\'', self::simplifySql($magicQuery->build($sql, ['startDate' => '2014-06-06']))); } public function testWithCache() { - global $db_url; $connectionParams = array( 'user' => $GLOBALS['db_username'], 'password' => $GLOBALS['db_password'], 'host' => $GLOBALS['db_host'], + 'port' => $GLOBALS['db_port'], 'driver' => $GLOBALS['db_driver'], ); $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams); @@ -252,14 +248,12 @@ public function testWithCache() $this->assertEquals($sql, self::simplifySql($magicQuery->build($sql))); } - /** - * @expectedException \Mouf\Database\MagicQueryParserException - */ public function testParseError() { $magicQuery = new MagicQuery(); $sql = ''; + $this->expectException(MagicQueryParserException::class); $magicQuery->build($sql); } @@ -392,20 +386,15 @@ public function testMatchAgainst() $this->assertEquals($expectedSql, self::simplifySql($magicQuery->build($sql, $params))); } - /** - * @expectedException \Mouf\Database\MagicQueryMissingConnectionException - */ public function testMisconfiguration() { $magicQuery = new MagicQuery(); $sql = "SELECT role.* FROM magicjoin(role) WHERE right.label = 'my_right'"; + $this->expectException(MagicQueryMissingConnectionException::class); $magicQuery->build($sql); } - /** - * - */ public function testTwig() { $magicQuery = new MagicQuery();