Check the logic in the Shell::in($prompt, $options = null, $default = null) #411

Merged
merged 4 commits into from Jan 18, 2012

Projects

None yet

2 participants

@fitorec
Contributor
fitorec commented Jan 16, 2012

Reviewing the code of the Shell I found a logic statement in particular that made ​​me confuzed, to which we apply a simple reduction:

If we review the logic of the following code:

<?php
     while ($in === '' || ($in !== '' && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) 

We will realize that $in must be different of '', which indicates that the code is equivalent to

<?php
     while ($in === '' || (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options) && !in_array($in, $options)) 

Finally, the logic is: (!a && !b && !c) may take the form: !(a || b || c):

<?php
      while ($in === '' || !(in_array(strtolower($in), $options) || in_array(strtoupper($in), $options) || in_array($in, $options))) {

Saludos!.

@markstory
Member

The logic could be simplified even more if $options was pre-seeded with the various casings. Something like:

$options = array_merge(
   $options,
   array_map('strtolower', $options),
   array_may('strtoupper', $options)
);

Then the changed lines could just be in_array($in, $options).

@markstory markstory merged commit e080bb2 into cakephp:2.0 Jan 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment