Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate ParserResult to PHP 8 syntax #10448

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions lib/Doctrine/ORM/Query/ParserResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string|int, list<int>>
*/
private $_parameterMappings = [];
private array $_parameterMappings = [];

/**
* Initializes a new instance of the <tt>ParserResult</tt> class.
Expand All @@ -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;
}
Expand All @@ -105,7 +98,7 @@ public function addParameterMapping($dqlPosition, $sqlPosition)
*
* @psalm-return array<int|string, list<int>> The parameter mappings.
*/
public function getParameterMappings()
public function getParameterMappings(): array
{
return $this->_parameterMappings;
}
Expand All @@ -118,7 +111,7 @@ public function getParameterMappings()
* @return int[] The positions of the corresponding SQL parameters.
* @psalm-return list<int>
*/
public function getSqlParameterPositions($dqlPosition)
public function getSqlParameterPositions(string|int $dqlPosition): array
{
return $this->_parameterMappings[$dqlPosition];
}
Expand Down
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1313,11 +1313,6 @@
<code>$AST instanceof AST\SelectStatement</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="lib/Doctrine/ORM/Query/ParserResult.php">
<PropertyNotSetInConstructor occurrences="1">
<code>$_sqlExecutor</code>
</PropertyNotSetInConstructor>
</file>
<file src="lib/Doctrine/ORM/Query/QueryExpressionVisitor.php">
<InvalidReturnStatement occurrences="1">
<code>new ArrayCollection($this-&gt;parameters)</code>
Expand Down
13 changes: 11 additions & 2 deletions tests/Doctrine/Tests/ORM/Query/ParserResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand Down