WordPress / create-block-theme

A WordPress plugin to create block themes
https://wordpress.org/plugins/create-block-theme/
GNU General Public License v2.0
310 stars 44 forks source link

Refactor theme fonts class for readability #661

Closed vcanales closed 4 weeks ago

vcanales commented 1 month ago

What?

This Pull Request refactors the them fonts class to improve code readability and maintainability. The main focus is on cleaning up the code.

I felt inclined to do this because when debugging for https://github.com/WordPress/create-block-theme/pull/660, I found some parts of the code hard to follow.

Why?

This PR addresses the need to make the code more readable and maintainable by:

  1. Adding documentation and comments.
  2. Simplifying functions and improving their readability.

How?

Key Changes:

  1. Class documentation added: Added descriptions and the purpose of the CBT_Theme_Fonts class.

  2. Function improvements:

    • Inline Descriptions/Justifications:
      • Refactored make_theme_font_src_absolute() to use array_map for cleaner array handling.
      • Replaced double newlines and improved spacing for better readability.
    • Changed foreach to array_map: This reduces the complexity and makes the handling of arrays cleaner.
  3. Code clean-up:

    • Removed some redundant comments in favor of "self documenting" practices.
    • Used shorthand assignment (?? null) for checking array keys.
    • Introduced variables for commonly used expressions.
  4. Function calls:

    • Replaced CBT_Theme_Fonts:: with self:: for static method calls within the class, enhancing readability and context awareness.
  5. Added test cases for parts of the code that could break on further refactors.