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

Add PHP type declarations #1314

Merged
merged 3 commits into from
Apr 8, 2023
Merged

Add PHP type declarations #1314

merged 3 commits into from
Apr 8, 2023

Conversation

herrvigg
Copy link
Collaborator

@herrvigg herrvigg commented Apr 8, 2023

PHP type declarations make the code clearer and catch some bugs due to wrong calls not handled properly.
PHP7.1 is interesting for nullable args and void return values.
See PHP type declarations.

This patch covers all of the qTranslate code.
Though it doesn't enforce strict checks yet, this has little effect with array and strings.
These changes might break some parts in specific situations so it should be tested by developers in master.
Some types are left unspecified, as mixed types require PHP 8.0 and some WP/plugin APIs are unclear.

@herrvigg herrvigg added the core Core functionalities, including the admin section label Apr 8, 2023
@herrvigg herrvigg requested a review from spleen1981 April 8, 2023 12:23
@herrvigg
Copy link
Collaborator Author

herrvigg commented Apr 8, 2023

@spleen1981 I put you as reviewer, but you don't need to go through all of the code as it's quite massive. Maybe take a more precise look at modules / slugs, but it's mostly about testing. If some type is wrong it will trigger some error at runtime.
After that we'll leave it for test in master for a few days.

@herrvigg herrvigg force-pushed the php-types branch 2 times, most recently from a7bfe28 to 3f65023 Compare April 8, 2023 13:18
@herrvigg
Copy link
Collaborator Author

herrvigg commented Apr 8, 2023

So, I've tested the main functionalities and it holds well. Let's merge and see now in master, we'll fix it if needed.
With the 3 sub-commits it might be easier to check back later if needed.

@herrvigg herrvigg merged commit 3da3cab into master Apr 8, 2023
@herrvigg herrvigg deleted the php-types branch April 8, 2023 20:05
herrvigg added a commit that referenced this pull request Apr 8, 2023
PHP type declarations make the code clearer and catch some bugs due to wrong calls or usage.
PHP7.1 is interesting for nullable args and void return values.
This patch doesn't enforce strict checks yet, but this has little effect with array and strings.
These changes might break some parts in specific situations so it should be tested by developers in master.
Some types are left unspecified, as mixed types require PHP 8.0 and some WP/plugin APIs are unclear.
herrvigg added a commit that referenced this pull request Apr 8, 2023
Fix wrong type in `qtranxf_adjust_config_files`.
Complete missing trivial types.
Improve robustness of `rest_request_after_callbacks` with type check.
herrvigg added a commit that referenced this pull request Apr 10, 2023
Remove arg type declaration as this function must also accept `object`.
herrvigg added a commit that referenced this pull request Apr 10, 2023
herrvigg added a commit that referenced this pull request Apr 10, 2023
The WordPress hook documentation is not very precise, giving WP_Term
but it can be other than this in some cases.
herrvigg added a commit that referenced this pull request Apr 15, 2023
The main purpose is to filter `redirect_canonical` (WP hook).
The WP documentation is not precise regarding the types so we have
to relax the types check. Check first if the redirect URL is false.

The second parameter is the requested URL but it is ignored.
See #1320, wrong usage with Yoast. The second parameter has a
different meaning with Yoast. Removing the types will work but the
Yoast hook should be rewritten with a different function.

Rewrite the function to handle the default language in convertURL.
herrvigg added a commit that referenced this pull request Apr 15, 2023
See also #1320. It is too risky to specify these types for now.
@herrvigg
Copy link
Collaborator Author

Strong type checks with PHP is the way to go because it helps to debug by finding wrong usage in the API calls. The drawback is that is it can create many regressions because it's impossible to test all the combination with integrations and hooks.

It's unfortunate the WP documentation is not accurate and their APIs are so messy with many mixed types often undocumented 😕 Let's try to stick to safe usage but it's difficult to judge where to set the boundary. The checks in core functions should be kept. But I may revert type checks that are risky, typically when plugged with hooks.

herrvigg added a commit that referenced this pull request Apr 15, 2023
Enabling too strict type checks can create regressions with hooks
that are not well documented.

Revert the type checks for hook callbacks that could be risky.
Copy link
Contributor

@spleen1981 spleen1981 left a comment

Choose a reason for hiding this comment

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

No issues as far as I could test (mainly slugs)

@herrvigg
Copy link
Collaborator Author

Great. I reverted/fixed quite a few things related to hooks with WP / other plugins, but slugs is mostly internal so we should be able to control the types better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core functionalities, including the admin section
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants