* - Reorder parameters. * - Change parameter names. * - Propagate new parameters through the function call hierarchy. * * Ctrl+F6 (Windows/Linux) * Command+F6 (Mac OS X) */ namespace Refactoring2\JetBrains; // 1. Change the function name to "add" using the Change Signature refactoring. function addTwoNumbers($a, $b) { return $a + $b; } // 2. Note that the affected code is updated as well. $four = addTwoNumbers(2, 2); // 3. Change the function signature: rename the $name parameter to $firstName add a $lastName parameter. // Specify a default value for the $lastName variable, we want our code to compile. // E.g. use '' to set an empty string everywhere sayHello is called. // // We can also propagate parameters. The usingSayHello() below uses $name as well, we may want to propagate // the new $lastName variable. Do this from the refactor window's toolbar on the right. // // Preview the refactoring and apply it when satisfied. function sayHello($name) { // 4. Uncomment the following line: // $name = $firstName . ' ' . $lastName; return "Hello, {$name}"; } sayHello('Maarten');
} } function addTwoNumbers($l1, $l2) { $sum = $l1->val + $l2->val; $next1 = $l1->next; $next2 = $l2->next; $l3 = new ListNode($sum % 10); $node = $l3; $carray_digit = floor($sum / 10); while ($next1 || $next2 || $carry_digit) { $next1_v = $next1 !== null ? $next1->val : 0; $next2_v = $next2 !== null ? $next2->val : 0; $next_sum = $next1_v + $next2_v + $carry_digit; $node->next = new ListNode($next_sum % 10); $node = $node->next; $next1 = $next1 !== null ? $next1->next : null; $next2 = $next2 !== null ? $next2->next : null; $carry_digit = floor($next_sum / 10); } return $l3; } //------------------------------ Simple Testing Code ------------------------------// $l1 = new ListNode(2); $l1->next = new ListNode(4); $l1->next->next = new ListNode(3); $l2 = new ListNode(5); $l2->next = new ListNode(6); $l2->next->next = new ListNode(4); var_dump(addTwoNumbers($l1, $l2));
function update_stock_quantity($amount_of_this_stock, $passed_quantity, $symbol) { global $dbh; $dbh->beginTransaction(); $sum_of_stocks = addTwoNumbers($amount_of_this_stock, $passed_quantity); $sql = 'UPDATE `table_portfolio` SET `amount_of_stock` = ' . $sum_of_stocks . ' WHERE `user_id` = ' . $_SESSION['user_id'] . ' AND `stock_symbol` = "' . $symbol . '"'; foreach ($dbh->query($sql) as $row) { $result = $row; // really no need for this, but I dont want to do more testing. Going to leave this foreach loop. } $dbh->commit(); }