Last active
August 29, 2015 13:56
-
-
Save schmittjoh/9308549 to your computer and use it in GitHub Desktop.
Example for how the same array is re-validated multiple times instead of validating it once/keeping track of its types.
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 | |
class MyClass | |
{ | |
public function doSomething(Foo[] $array) { // First check here. | |
if ($this->hasSomeElement($array)) { | |
$this->doSomethingBranch1($array); | |
} else { | |
$this->doSomethingBranch2($array); | |
} | |
} | |
private function hasSomeElement(Foo[] $array) { // Second validation here (unnecessary) | |
foreach ($array as $v) { | |
if ($v->isTrue()) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private function doSomethingBranch1(Foo[] $array) { // Third validation here (unnecessary) | |
// ... | |
} | |
private function doSomethingBranch2(Foo[] $array) { // Another validation here (unnecessary) | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment