-
-
Save KnowTheCodePro/59d7361c72fccf191c5671377793b143 to your computer and use it in GitHub Desktop.
if/else Backwards pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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