Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KnowTheCodePro
Last active March 30, 2016 14:29
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 KnowTheCodePro/59d7361c72fccf191c5671377793b143 to your computer and use it in GitHub Desktop.
Save KnowTheCodePro/59d7361c72fccf191c5671377793b143 to your computer and use it in GitHub Desktop.
if/else Backwards pattern
<?php
namespace KnowTheCode;
/**
* Smelly code version. Why?
*
* 1. The `if` conditional expression is working on a falsey state.
* 2. The work is being done in the `else` code block.
*
* @since 1.0.0
*
* @param string $event_name
*
* @return int
*/
function count_number_of_times_fired( $event_name ) {
static $events = array();
if ( ! isset( $events[ $event_name ] ) ) {
$events[ $event_name ] = 1;
} else {
$events[ $event_name ]++;
}
return $events[ $event_name ];
}
d( count_number_of_times_fired( 'loop_start' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment