Skip to content

Commit fb42a10

Browse files
committed
Validate POST Input in Sample 45
Errors will result if input is not numeric.
1 parent 59ee38f commit fb42a10

File tree

3 files changed

+100
-45
lines changed

3 files changed

+100
-45
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
'blank_line_after_namespace' => true,
2020
'blank_line_after_opening_tag' => true,
2121
'blank_line_before_statement' => true,
22-
'braces' => true,
2322
'cast_spaces' => true,
2423
'class_attributes_separation' => ['elements' => ['method' => 'one', 'property' => 'one']], // const are often grouped with other related const
2524
'class_definition' => false,
26-
'class_keyword_remove' => false, // ::class keyword gives us better support in IDE
2725
'combine_consecutive_issets' => true,
2826
'combine_consecutive_unsets' => true,
2927
'combine_nested_dirname' => true,
@@ -113,8 +111,7 @@
113111
'no_spaces_inside_parenthesis' => true,
114112
'no_superfluous_elseif' => false, // Might be risky on a huge code base
115113
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
116-
'no_trailing_comma_in_list_call' => true,
117-
'no_trailing_comma_in_singleline_array' => true,
114+
'no_trailing_comma_in_singleline' => ['elements' => ['arguments', 'array_destructuring', 'array', 'group_import']],
118115
'no_trailing_whitespace' => true,
119116
'no_trailing_whitespace_in_comment' => true,
120117
'no_unneeded_control_parentheses' => true,

composer.lock

Lines changed: 89 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/Basic/45_Quadratic_equation_solver.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
?>
1616
<form action="45_Quadratic_equation_solver.php" method="POST">
17-
Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
17+
Enter the coefficients for Ax<sup>2</sup> + Bx + C = 0
1818
<table border="0" cellpadding="0" cellspacing="0">
1919
<tr>
2020
<td>
@@ -47,7 +47,9 @@
4747
<?php
4848
/** If the user has submitted the form, then we need to execute a calculation * */
4949
if (isset($_POST['submit'])) {
50-
if ($_POST['A'] == 0) {
50+
if (!is_numeric($_POST['A']) || !is_numeric($_POST['B']) || !is_numeric($_POST['C'])) { // validate input
51+
$helper->log('Non-numeric input');
52+
} elseif ($_POST['A'] == 0) {
5153
$helper->log('The equation is not quadratic');
5254
} else {
5355
// Calculate and Display the results
@@ -59,8 +61,12 @@
5961
$r1Formula = '=IMDIV(IMSUM(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . ')';
6062
$r2Formula = '=IF(' . $discriminant . '=0,"Only one root",IMDIV(IMSUB(-' . $_POST['B'] . ',IMSQRT(' . $discriminant . ')),2 * ' . $_POST['A'] . '))';
6163

62-
$helper->log(Calculation::getInstance()->calculateFormula($r1Formula));
63-
$helper->log(Calculation::getInstance()->calculateFormula($r2Formula));
64+
/** @var string */
65+
$output = Calculation::getInstance()->calculateFormula($r1Formula);
66+
$helper->log("$output");
67+
/** @var string */
68+
$output = Calculation::getInstance()->calculateFormula($r2Formula);
69+
$helper->log("$output");
6470
$callEndTime = microtime(true);
6571
$helper->logEndingNotes();
6672
}

0 commit comments

Comments
 (0)