Created
January 16, 2015 18:05
-
-
Save sarciszewski/690609a0f8308f0aabd2 to your computer and use it in GitHub Desktop.
Anti-Patterns
This file contains hidden or 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 | |
class switchNotMatched { } | |
function doInteger($i) { | |
echo $i; | |
if ($i < 100) { | |
echo " is less than 100."; | |
} | |
echo "\n"; | |
} | |
function doString($i) { | |
echo str_rot13($i)."\n"; | |
} | |
function doSomething($input) { | |
static $nomatch = null; | |
if (empty($nomatch)) { | |
$nomatch = new SwitchNotMatched(); | |
} | |
switch($input) { | |
case preg_match('/^[0-9]+$/', $input)?$input:$nomatch: | |
doInteger($input); | |
break; | |
case preg_match('/^[a-zA-Z\s]+$/', $input)?$input:$nomatch: | |
doString($input); | |
break; | |
default: | |
echo "Something else...\n"; | |
} | |
} | |
doSomething(10); | |
doSomething('101'); | |
doSomething('lbh whfg ybfg gur tnzr'); | |
doSomething('x7'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment