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: initialize typed properties correctly in ExternalImage::build() #2818
Conversation
If we do it like that (probably a better approach, as I mentioned in the considerations), then we
This is why I propose not to initialize with |
@jrathert Thanks for fixing this! I agree with your approach. The only thing that I would so differently is not initializing the properties but rather use the null null coalescing operator when returning values. You can see it right here : #2825 Sorry to push another PR, It was faster than commenting code and I didn't find an easy way to PR on your fork 🙂 Feel free to report changes if needed. |
@nlemoine - no problem tweaking my patch into a separate PR, but:
I am strongly against this. Not only it is a bit a questionable approach: first we make sure (in some places) by default arrays to set the values to empty strings - just to in the very last moment of access replace that empty string with But the most important and critical thing with your approach now: What if a user explicitly sets So either we do it right from the very beginning (allow null properties and initialize them that way and only return |
Are you referring to Lines 150 to 156 in 38a9940
This was changed in my PR to
We should avoid making assumptions on what users might do, it's WordPress 😉
I think you're mistaken by the null coalescing operator. If a user sets an empty string, Depending on what you're doing, you don't necessarily need to initialize your properties. |
@nlemoine - sorry, my bad, I made a few stupid mistakes here. Wasn't the best idea to comment while commuting and not having convenient access to the code. And to top even that: I really wasn't sure about the null coalescing operator's behavior and tried it before commenting, but the test I did was simply not correct / misleading.... 😬 So fine with it, sorry again. |
Thanks to you both for figuring out the correct path to this. I just approved the PR in #2825. @nlemoine I think we’ll wait until we fixed all the testing bugs before merging this in.
@nlemoine We usually do it this way: create a new PR based on the commits of the contributor and make the PR not against the PR, but against the main branch. This worked fine in the past.
@jrathert No worries, and don’t call them stupid mistakes. That’s why we have PRs and discussions. I also learn like a looot of new things almost every time I try to tackle an issue. It’s amazing that you already dived deep into the project even though you don’t have that much experience with PHP. Your help is invaluable and we truly appreciate it 🙌. Thanks! |
Just an additional note: as soon as we merge #2825, this PR will be marked as merged automatically. |
No need to be sorry @jrathert ;) Thanks for your contribution! |
Related:
Issue
The (protected) typed properties of
ExternalImage
,$alt_text
and$caption
, were not initialized properly in the staticbuild()
method, causing errors when accessing them later (must not be accessed before initialization
). The error only occurred in PHP >= 7.4, when this behavior (not initializing typed properties) was introduced.Solution
The calling method (
Timber::get_external_image()
) provides empty strings as default arguments foralt
andcaption
when calling the build() method. Also, within the build method, there was a default for at least thealt
parameter. But as these default parameters were empty strings, they were not used to initialize the properties properly. I did two things:caption
has a default value (empty string) defined withinbuild()
Impact
No negative impact expected, as typed properties did not even exist before PHP 7.4, so that version is required for Timber 2.0.
Usage Changes
None
Considerations
One could argue that null values would be even more appropriate as default initialization values for the respective properties, if they are not specifically defined by the user. That would allow to separate between "there is no alt value" and "there is an alt value, the empty string".
Testing
I enhanced the unit test for the
ExternalImage
class to cover the case where a user does NOT specify an alt-text or a caption when creating theExternalImage
withTimber::get_external-image()
. These test cases fail for the original code but succeed when the patch is applied.