Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active January 26, 2021 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/2253a49615ef010968ddc3661cb563ef to your computer and use it in GitHub Desktop.
Save rgadon107/2253a49615ef010968ddc3661cb563ef to your computer and use it in GitHub Desktop.
Solution to Code Challenge 1 from "Refactoring Tweaks Workbook"
// # Code Challenge 1, from “Refactoring Tweaks Workbook” by Tonya Mork ( 2016, LeanPub (https://leanpub.com/littlegreenbookofrefactoringtweaks-workbook) ).
// ## Original code to be refactored
/**
* Check if custom post type exists, to provide data
*/
function prefix_is_post_type( string $type, int $post_id ) {
global $wp_query;
if($type == get_post_type($wp_query->post->ID)) return true;
return false;
}
// ### Discussion
/**
* The function above calls the global variable `$wp_query`. Global variables are both readable and writable.
* If any values within the global variable are rewritten, this could affect the behavior of the `$wp_query` object through
* the rest of the application, and require time and effort to debug.
*
* The way to avoid interacting with the global variable is to build an API that abstracts away access to the global object.
*/
// ## Refactored code
/**
* Evaluate post_type for a given post_id. ( Refactored original function. )
*
* @since 1.0.0
*
* @param string $type The post_type to be evaluated.
* @param int $post_id The post_id
*
* @return bool Evaluation of post_type comparison.
*/
function prefix_is_post_type( string $type, int $post_id ) {
$post_type = get_the_post_type( $post_id );
if ( $type == $post_type ) return true;
return false;
}
/**
* Get post_type from the post object.
*
* @since 1.0.0
*
* @param int $post_id The post id.
* @return string|null The post_type attribute.
*/
function get_the_post_type( int $post_id = 0 ) {
if ( ! post_id ) {
return;
}
return get_post_attribute( 'post_type', $post_id );
}
/**
* Get post attribute.
*
* @since 1.0.0
*
* @param string $attribute Name of the post attribute.
* @param int $post_id The post ID.
*
* @return string|null Attribute name.
*/
function get_post_attribute( string $attribute, int $post_id = 0 ) {
if ( ! post_id ) {
return;
}
$post = get_post( $post_id );
return $post->$attribute;
}
// ### Discussion
/**
* The refactoring solution above creates an API to interact with the `$post` object ( an instance of the WP_Post class ),
* and retrieve attributes ( properties ). The function `get_post_attribute()` interacts directly with the `$post` object
* to retrieve a specific attribute. The function `get_the_post_type()` is a wrapper for `get_post_attribute()`, and returns
* the attribute passed as an argument.
*
* The refactored function `prefix_is_post_type()` then performs its single responsiblity; to evaluate whether the actual
* post_type ( argument `$type` ) is equal to the expected post_type.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment