Upgrade to PHP 8.2
See https://github.com/sybrew/the-seo-framework/issues/98 and #608.
We expect TSF v5.0 to be released late 2024-- that's when most sites have hopefully upgraded to a secure PHP version–that'd be 8.2. See https://www.php.net/supported-versions.php.
Since the jump from PHP 8.0 to 8.2 is perceived as minor, we expect that most PHP 8.X users will be on 8.2 by the time we upgrade from 7.4.
Features we'd want to utilize
PHP 8.0
-
Named arguments
- Makes code much more readable.
-
Construction promotion
- Instantly shows that construction arguments become class properties. This should improve readability and reduce code footprint.
- However, I have never seen this used before (probably because PHP 8 isn't in widespread use), so I'm not sure if future coders will understand this.
-
Match expression
- Condenses return-based/value-assigning switch-statements.
-
Nullsafe operator
- Reduces the need for chaining with
isset()/method_exists()/etc., condensing code.
- Reduces the need for chaining with
-
str_contains()/str_starts_with()/str_ends_with()
- Less
false/0 !== strpos()/strrpos(), more readable code.
- Less
PHP 8.1
-
Named arguments after unpacking
- Makes code a little more readable.
-
Fibers
- This is interesting. We can utilize this for generation by suspending coroutines when one value is reached. However, this is quite challenging to make logical, and this feature is probably intended for remote requests.
-
array_is_list()
- Condenses API functions that rely on lists, or allow both lists (sequential) and associative arrays.
-
Readonly properties
- This will make me not have to mark everything "@private".
PHP 8.2
-
Readonly classes
- Same as readonly properties, but then simpler.
-
Constant in traits
- Another good reason to start embracing traits and actual OOP.
Conclusion
The tiny list of features in 8.2 does not justify an upgrade unless we can reasonably assume most sites are upgraded.
Proposed version
Ref: https://wordpress.org/about/stats/
We'll upgrade to a version that at least 66% of sites support at our scheduled release. We previously aimed at 85%. However, since WordPress reports data from inactive sites and prevents users from updating plugins with unsupported required PHP versions, I see no issue in upgrading earlier.
TSF v5.0 will have an estimated 6-month development cycle. Hopefully, we can get started somewhere early or even before 2024. Extension Manager's upgrade will follow soon after that.
I also find enums (added in PHP 8.1) quite useful.
I simulated the Fiber API here; it's where we could benefit by upgrading, where we need not test for also valid() and next() -- but only resume() (now current()) and isTerminated() (now false):
https://github.com/sybrew/the-seo-framework/blob/e44d72af7fc6a71c79de59600ed259a015c5e022/inc/classes/meta/factory/image.class.php#L612-L639
We have various functions that could benefit from array_is_list():
https://github.com/sybrew/the-seo-framework/blob/e44d72af7fc6a71c79de59600ed259a015c5e022/inc/functions/utils.php#L70-L72
This could be condensed using array unpacking by string (...dimensions,[ etc, ]):
https://github.com/sybrew/the-seo-framework/blob/e44d72af7fc6a71c79de59600ed259a015c5e022/inc/classes/meta/factory/image.class.php#L678-L685
We're already using str_contains() and str_starts_with() because WordPress provides polyfills.
Some properties could benefit from being readonly -- for now, I marked those as "private" or blocked access altogether by requiring a functional interface. Bypassing the function overhead could speed things up and make the code more readable, especially since the nullsafe operator is also available.
About enums... this requires a new way of thinking about objects -- I'm not sure if it's backportable. I'll look into this one a bit more once we can actually embrace it.
WordPress 6.5+ has a polyfill for array_is_list().