Skip to content

Fix wrong type assertion and annotation#3543

Closed
nicolas-grekas wants to merge 2 commits intodoctrine:masterfrom
nicolas-grekas:assert-pdo
Closed

Fix wrong type assertion and annotation#3543
nicolas-grekas wants to merge 2 commits intodoctrine:masterfrom
nicolas-grekas:assert-pdo

Conversation

@nicolas-grekas
Copy link
Copy Markdown
Member

Q A
Type bug
BC Break no
Fixed issues -

This issue introduced in #3443 breaks Symfony's test suite.

@jwage
Copy link
Copy Markdown
Member

jwage commented May 9, 2019

@morozov and I were just talking about this issue in #3487 and how we plan to improve this in 3.0. This is good for master.

@jwage jwage added the BC Fix label May 9, 2019
@jwage
Copy link
Copy Markdown
Member

jwage commented May 9, 2019

Hmm, does phpstan not like that?

nicolas-grekas added a commit to symfony/symfony that referenced this pull request May 9, 2019
This PR was merged into the 4.3 branch.

Discussion
----------

[Doctrine\Bridge] fix tests

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Once merged, one issue will remain, which will be fixed by doctrine/dbal#3543

Commits
-------

10da231 [Doctrine\Bridge] fix tests
* @param string $cacheKey
* @param string $realKey
* @param int $lifetime
* @param ResultStatement|PDOStatement $stmt
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add an assert statement to the constructor to check for this, but apparently PHPStan thinks it's redundant. The code in question would be

assert($stmt instanceof ResultStatement || $stmt instanceof PDOStatement);

Do we want to add it and silence the error or leave it as is?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed and the type hint added back, isn't it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree here, it looks like we're missing an assert on the caller and not here.

@alcaeus alcaeus requested review from jwage, lcobucci and morozov May 10, 2019 08:52
@alcaeus
Copy link
Copy Markdown
Member

alcaeus commented May 10, 2019

I've fixed the PHPStan failures that occurred due to allowing PDO classes in Connection. Unfortunately that means adding a bunch of assert calls as for example fetchAll will happily return false depending on the error handling mechanism. I don't think it's feasible to check for false and throw exceptions; at least not in the context of this PR.

* @param int $lifetime
*/
public function __construct(ResultStatement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
public function __construct($stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even when passing the PDO instance we modify the statement class, so I'd say we shouldn't remove this type hint here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan was complaining about it, that's why I had to modify it.

Copy link
Copy Markdown
Member

@lcobucci lcobucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we're doing unnecessary things here and that we should try an alternative approach (wrapping). I'm working on it atm and we can compare the two PRs...

* @param string $cacheKey
* @param string $realKey
* @param int $lifetime
* @param ResultStatement|PDOStatement $stmt
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree here, it looks like we're missing an assert on the caller and not here.

$conn = DriverManager::getConnection([
'pdo' => new PDO('sqlite::memory:'),
]);
$pdo = new PDO('sqlite::memory:');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we need more tests covering the basic DBAL features for when an instance is used?

@lcobucci lcobucci mentioned this pull request May 10, 2019
Tobion added a commit to symfony/symfony that referenced this pull request May 11, 2019
This PR was merged into the 4.3 branch.

Discussion
----------

[Messenger] Fix doctrine tests

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | not needed

Not sure why, but when passing in `pdo`, the Doctrine `Connection::_conn` property is a PDO instance and isn't wrapped. In the master branch of `doctrine/dbal`, they now check for this an throw an exception.

@nicolas-grekas has a PR in Doctrine for this (doctrine/dbal#3543), but I don't see any reason we shouldn't just avoid the `pdo` option entirely.

Commits
-------

a7cf3f9 Fixing tests - passing pdo is not wrapped for some reason in dbal
$this->_conn->quote($keyName)
));
$indexArray = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$stmt = $this->_conn->executeQuery(sprintf(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$stmt = $this->_conn->executeQuery(sprintf(
$stmt = $this->_conn->executeQuery(sprintf(

Because of the newline above this, I assume that the spaces aren't needed here.

* Executes an SQL statement, returning a result set as a Statement object.
*
* @return \Doctrine\DBAL\Driver\Statement
* @return DriverStatement|PDOStatement
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Interface containing query() only expects a return type of Doctrine\DBAL\Driver\Statement. Should PDOStatement be added to the interface too?

@morozov
Copy link
Copy Markdown
Member

morozov commented May 23, 2019

Closing as no longer relevant.

@morozov morozov closed this May 23, 2019
@morozov morozov self-assigned this Jun 7, 2019
@morozov morozov added the PDO label Jun 7, 2019
@morozov morozov removed their request for review October 23, 2020 17:01
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants