I've got a lot of legacy code like this:
return isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : isset($d) ? $d : isset($e) ? $e : '';
So it's just chained ternary operator.
It works, but most of the variable names are not $a, but $veryLongAndDescriptiveCorporateVariableNames and it's just unreadable for me.
Is there a better way to refactor this? I tried something like this but I don't know if it's the best and most readable way:
$ar = array(
$a,
$b,
$c,
$d,
$e
);
foreach ($ar as $k => $v)
if (isset($v))
return $v;
return '';
It's Working fine:-
$ar = array(
$a,
$b,
$c,
$d,
$e
);
foreach ($ar as $k => $v)
{
echo (isset($v)) ? $v : '';
}
Indentation is your friend...
return isset($a)
? $a
: isset($b)
? $b
: isset($c)
? $c
: isset($d)
? $d
: isset($e)
? $e
: '';
M.Fowler would recommend to replace nested conditional with guard clauses:
if (isset($a) {
return $a;
}
if (isset($b) {
return $b;
}
if (isset($c) {
return $c;
}
if (isset($d) {
return $d;
}
if (isset($e) {
return $e;
}
return '';
https://refactoring.guru/replace-nested-conditional-with-guard-clauses
Related
I need to refactor a piece of php code to work with 7.2 but i am not sure if the way i did it is correct. I could use some info about it.
This is the old code
private function sortByFields($field, &$array)
{
usort($array, create_function('$a, $b', '
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
'));
return true;
}
And this is the code i refactored for 7.2
private function sortByFields($field, &$array)
{
usort($array, function ($a,$b) {
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
});
return true;
}
Is it correct or did i mess up ?
You missed out on the "use" part so the function body does not know about the $field and messed up the $field array keys - see below:
private function sortByFields($field, &$array)
{
usort($array, function ($a,$b) use ($field) {
$a = $a[$field];
$b = $b[$field];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
});
return true;
}
Is it possible to use the result of an if with an OR statement as a variable for a function?
As example:
$a = true;
$b = false;
if ($a || $b) {
$this->functionCall($a)
}
Other example:
$a = false;
$b = true;
if ($a || $b) {
$this->functionCall($b)
}
Third and final exmaple:
$a = true;
$b = true;
if ($a || $b) {
$this->functionCall($a, $b)
}
So I need to detect what variable is true and pass it as a paramater. Is this even possible?
Any helps is appreciated!
Many thanks in advance
I'd do the logic bit inside a two-parameter function if I were you, as such :
function myFunc($a = false, $b = false) {
if ($a == true)
echo 'a';
if ($b == true)
echo 'b';
}
myFunc(); // echoes nothing
$a = true;
$b = false;
myFunc($a, $b); // echoes 'a'
$a = false;
$b = true;
myFunc($a, $b); // echoes 'b'
$a = true;
$b = true;
myFunc($a, $b); // echoes 'ab'
PHP 5.6+ version, filter out the falsely values (you can pass a callback to array_filter for different checks) and use those with the splat operator.
$params = array_filter([$a, $b]);
$this->callFunction(...$params);
No need for any IF checks and confusing in IF assignments.
Explore Variadic functions and Argument unpacking.
where:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
I'm not sure how to work out a.
So I understand that this is a shorthand operator, and usually it's a case of:
$value ? true : false
meaning
if $a = true { true } else { false };
so:
if $a{
if $a{
true;}
else{
0;};
else{
if $0{
$a;}
else{
true;}
};
does this make the value of $a true?
The value of $a would be true
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
The shorthand can be interpreted like this:
if($a) {
if($a) {
$a = $b;
} else {
$a = $c;
}
} else {
if($c) {
$a = $a;
} else {
$a = $b;
}
}
Because $a is false for not existing in the first place, it immediately jumps to the else statement in that. So the only part that matters to you is:
if($c) {
$a = $a;
} else {
$a = $b;
}
0 is the same as false, so $c will come back as false, therefore $a is equal to $b, which is true.
Edit:
There is some discussion on the notice that is thrown, but this fails to account for the fact that notices are not truly errors and because of this there is no interruption to the code. The result is not Notice: Undefined variable: a, the "result" (think these people mean output) would be blank if it weren't for us determining the value of $a at the end with var_dump. The question was as to what the value of $a becomes, not what appears on your screen.
Something displaying on your screen in re to a variable not being set has nothing to do with the value of what $a is.
If you execute the following code, the notice is not the only thing realized:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
So the output is:
E_NOTICE : type 8 -- Undefined variable: a -- at line 5
bool(true)
The fact that a notice was thrown does not prevent $a from becoming true.
Also notices are easily suppressed...
error_reporting(0);
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
would result in $a still becoming true, and without seeing the notice.
bool(true)
If you run the code as is, you would get: Notice: Undefined variable: a in myfile.php on line 4
Therefore, I would postulate $a is set somewhere earlier. Yet, whatever value $a has prior, if $a is can be evaluated to true or false, $a would still be true after running your code for the following reason:
If $a were true, then the first part would yield $a = $b and we know $b = true.
if(TRUE) {
if(TRUE) {
$a = $b; //AND $b == TRUE
} else {
$a = $c;
}
} else {
...
}
If $a were false, then the second part would yield $a = $b again
if(FALSE) {
...
} else {
if(0) { // 0 will equate to FALSE
...
} else {
// 0 is the same as FALSE so we end up again with $a = $b
$a = $b; //AND $b == TRUE
}
}
In fact, if you run this code, it will show you the value of $a is true both times:
<?php
$a = false;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
$a = true;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
I want to convert this code to a ternary operator:
if (isset($a)) {
$arr[$k] += $c;
}
else {
$arr[$k] = $c;
}
I can do it if it were a single operator, but, operators are different in if and else.
How to do it?
Isn't it just:
$arr[$k] = isset(a$) ? ($arr[$k]+$c) : $c
or equivalently:
$arr[$k] = $c + (isset($a) ? $arr[$k] : 0)
Possible conversion algorithm with your case (like school algebra)
Direct conversion.
isset($a) ? ($arr[$k] += $c) : ($arr[$k] = $c);
Explode +=
isset($a) ? ($arr[$k] = $arr[$k] + $c) : ($arr[$k] = $c);
Move assignment to the left
$arr[$k] = isset($a) ? ($arr[$k] + $c) : ($c);
Move $c
$arr[$k] = (isset($a) ? $arr[$k] : 0) + $c;
And variant without ternary(as bonus).
$arr[$k] = $c + isset($a) * $arr[$k];
I have the following code:
function create_sort_callback($criteria)
{
return function($a, $b)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}
It turns out I can't access $criteria from within the inner function. How can I solve this problem?
Try like this
function create_sort_callback($criteria)
{
return function($a, $b) use($criteria)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}
You need using closures http://www.php.net/manual/en/functions.anonymous.php
Use the use keyword.
function create_sort_callback($criteria)
{
return function($a, $b) use ($criteria)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}