diff --git a/lib/Doctrine/ORM/Query/ParserResult.php b/lib/Doctrine/ORM/Query/ParserResult.php index fc513e438d2..ad0c1c1f7b3 100644 --- a/lib/Doctrine/ORM/Query/ParserResult.php +++ b/lib/Doctrine/ORM/Query/ParserResult.php @@ -5,6 +5,9 @@ namespace Doctrine\ORM\Query; use Doctrine\ORM\Query\Exec\AbstractSqlExecutor; +use LogicException; + +use function sprintf; /** * Encapsulates the resulting components from a DQL query parsing process that @@ -16,24 +19,20 @@ class ParserResult { /** * The SQL executor used for executing the SQL. - * - * @var AbstractSqlExecutor */ - private $_sqlExecutor; + private AbstractSqlExecutor|null $_sqlExecutor = null; /** * The ResultSetMapping that describes how to map the SQL result set. - * - * @var ResultSetMapping */ - private $_resultSetMapping; + private ResultSetMapping $_resultSetMapping; /** * The mappings of DQL parameter names/positions to SQL parameter positions. * * @psalm-var array> */ - private $_parameterMappings = []; + private array $_parameterMappings = []; /** * Initializes a new instance of the ParserResult class. @@ -49,53 +48,47 @@ public function __construct() * * @return ResultSetMapping The result set mapping of the parsed query */ - public function getResultSetMapping() + public function getResultSetMapping(): ResultSetMapping { return $this->_resultSetMapping; } /** * Sets the ResultSetMapping of the parsed query. - * - * @return void */ - public function setResultSetMapping(ResultSetMapping $rsm) + public function setResultSetMapping(ResultSetMapping $rsm): void { $this->_resultSetMapping = $rsm; } /** * Sets the SQL executor that should be used for this ParserResult. - * - * @param AbstractSqlExecutor $executor - * - * @return void */ - public function setSqlExecutor($executor) + public function setSqlExecutor(AbstractSqlExecutor $executor): void { $this->_sqlExecutor = $executor; } /** * Gets the SQL executor used by this ParserResult. - * - * @return AbstractSqlExecutor */ - public function getSqlExecutor() + public function getSqlExecutor(): AbstractSqlExecutor { + if ($this->_sqlExecutor === null) { + throw new LogicException(sprintf( + 'Executor not set yet. Call %s::setSqlExecutor() first.', + self::class, + )); + } + return $this->_sqlExecutor; } /** * Adds a DQL to SQL parameter mapping. One DQL parameter name/position can map to * several SQL parameter positions. - * - * @param string|int $dqlPosition - * @param int $sqlPosition - * - * @return void */ - public function addParameterMapping($dqlPosition, $sqlPosition) + public function addParameterMapping(string|int $dqlPosition, int $sqlPosition): void { $this->_parameterMappings[$dqlPosition][] = $sqlPosition; } @@ -105,7 +98,7 @@ public function addParameterMapping($dqlPosition, $sqlPosition) * * @psalm-return array> The parameter mappings. */ - public function getParameterMappings() + public function getParameterMappings(): array { return $this->_parameterMappings; } @@ -118,7 +111,7 @@ public function getParameterMappings() * @return int[] The positions of the corresponding SQL parameters. * @psalm-return list */ - public function getSqlParameterPositions($dqlPosition) + public function getSqlParameterPositions(string|int $dqlPosition): array { return $this->_parameterMappings[$dqlPosition]; } diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 31906e3082c..48fc01770ae 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1313,11 +1313,6 @@ $AST instanceof AST\SelectStatement - - - $_sqlExecutor - - new ArrayCollection($this->parameters) diff --git a/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php b/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php index f1bf6ea8e71..af9831bf77a 100644 --- a/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\Exec\AbstractSqlExecutor; use Doctrine\ORM\Query\ParserResult; use Doctrine\ORM\Query\ResultSetMapping; +use LogicException; use PHPUnit\Framework\TestCase; class ParserResultTest extends TestCase @@ -24,10 +25,18 @@ public function testGetRsm(): void self::assertInstanceOf(ResultSetMapping::class, $this->parserResult->getResultSetMapping()); } - public function testSetGetSqlExecutor(): void + public function testItThrowsWhenAttemptingToAccessTheExecutorBeforeItIsSet(): void { - self::assertNull($this->parserResult->getSqlExecutor()); + $this->expectException(LogicException::class); + $this->expectExceptionMessage( + 'Executor not set yet. Call Doctrine\ORM\Query\ParserResult::setSqlExecutor() first.', + ); + + $this->parserResult->getSqlExecutor(); + } + public function testSetGetSqlExecutor(): void + { $executor = $this->getMockForAbstractClass(AbstractSqlExecutor::class); $this->parserResult->setSqlExecutor($executor); self::assertSame($executor, $this->parserResult->getSqlExecutor());