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

fix: handle deprecation notices in PHP 8.1 #50

Merged
merged 3 commits into from
Dec 8, 2022
Merged

fix: handle deprecation notices in PHP 8.1 #50

merged 3 commits into from
Dec 8, 2022

Conversation

mwguerra
Copy link
Contributor

@mwguerra mwguerra commented Dec 1, 2022

Hi, everyone! This pull request solves deprecation notices and untreated errors, new after upgrading to PHP 8.1.

The PDOEngine class extends from the PHP PDO class, but with no indication of the return code or a signature mismatch in some methods. Starting in PHP 8.1 not being explicit on the return types (or returning anything different from the parent method) leads to a deprecation notice and untreated errors.

There were four methods to adjust. Three of them were easy fixes/enhancements.

  • commit and rollBack were expected to return a boolean but there was no return at all
  • beginTransaction just needed to be adjusted for a default return value (also expected to be a boolean)

The fourth method is query. The way query was implemented, changed a lot the original PDO query response behavior. It gets the job done but also introduces new challenges to make the code compliant with the original signature/return type, leading to a deprecation warning.

The original PDO query always returns a PDOStatement object, or false on any failure. The current PDOEngine, as implemented, could return (from my initial analysis) undefined, true, false, PDOStatement objects and even integers.

For this reason, I believe it’s a fair decision to use the #[\ReturnTypeWillChange] annotation in this case. In the near future, I suggest someone should review and refactor the codebase. There will be some breaking changes, but I believe it's an important step towards PHP 8.3 (that I believe should be launched by the end of next year) and even a future PHP 9, where I also believe the #[\ReturnTypeWillChange]
will be retired.

After those changes, all tests passed.

Solves #27
Solves #49

I hope my analysis and this PR help the project in any way.
Best regards and Merry Christmas!

Copy link
Owner

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Unfortunately, the addition of return types won't work here without breaking compatibility with PHP 5.6. WP has solved this in other places by having alternate classes that are loaded based on the PHP version, but that won't work for our single file drop-in here because the whole file needs to be syntactically valid for the runtime.

src/db.php Outdated
*/
public function beginTransaction()
public function beginTransaction(): bool
Copy link
Owner

Choose a reason for hiding this comment

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

Unfortunately, we can't use return types due to compatibility with PHP 5.6.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely right. The solution will be to use the ReturnTypeWillChange annotation on all those methods. Jut sent a new update. Thanks for your reply!

Comment on lines 2433 to +2437
}

$this->has_active_transaction = $this->pdo->beginTransaction();

return $this->has_active_transaction;
Copy link
Owner

Choose a reason for hiding this comment

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

This part seems a bit unrelated but is an improvement 😄

@mwguerra mwguerra requested a review from aaemnnosttv December 5, 2022 17:28
Copy link
Owner

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Thanks @mwguerra – tests are all passing now, just one last thing.

@@ -1319,9 +1319,9 @@ private function prepare_directory()
* @param int $mode
* @param array $fetch_mode_args
*
* @return mixed according to the query type
Copy link
Owner

Choose a reason for hiding this comment

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

Let's keep the @return doc block annotations since we aren't able to replace them with return types 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you!

@@ -2425,39 +2425,45 @@ private function get_sqlite_version()
* Method to call PDO::beginTransaction().
*
* @see PDO::beginTransaction()
* @return boolean
Copy link
Owner

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thank you!

@mwguerra mwguerra requested a review from aaemnnosttv December 5, 2022 17:58
Copy link
Owner

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@aaemnnosttv aaemnnosttv merged commit 6a7780f into aaemnnosttv:master Dec 8, 2022
@dingo-d dingo-d mentioned this pull request Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants