Skip to content

Commit 859449d

Browse files
committed
Drop Pear requirement - replacing all error messages by php exceptions, static keyword is added for all static functions, public type added for public functions, classes are called from global namespace - considering that in the installed application they will be autoloaded by composer, all pear isError checkes is removed, as in case of error it only throws an exception, other small improvements are done
1 parent 6362f91 commit 859449d

File tree

9 files changed

+134
-209
lines changed

9 files changed

+134
-209
lines changed

Math/Numerical/RootFinding.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@
2424
* @link http://pear.php.net/package/Math_Numerical_RootFinding
2525
*/
2626

27-
/**
28-
* PEAR
29-
*/
30-
require_once 'PEAR.php';
31-
32-
/**
33-
* Method driver aliases in order to create the prety file names,
34-
* also for insensitive-case of method name calls.
35-
*
36-
* @global array $GLOBALS['_Math_Numerical_RootFinding_drivers']
37-
* @_Math_Numerical_RootFinding_drivers
38-
*/
39-
$GLOBALS['_Math_Numerical_RootFinding_drivers'] = array(
40-
'bisection' => 'Bisection',
41-
'falseposition' => 'FalsePosition',
42-
'fixedpoint' => 'FixedPoint',
43-
'newtonraphson' => 'NewtonRaphson',
44-
'newtonraphson2' => 'NewtonRaphson2',
45-
'ralstonrabinowitz' => 'RalstonRabinowitz',
46-
'secant' => 'Secant'
47-
);
4827

4928
/**
5029
* Math_Numerical_RootFinding base class.
@@ -62,6 +41,35 @@
6241
*/
6342
class Math_Numerical_RootFinding
6443
{
44+
45+
/**
46+
* Method driver aliases in order to create the prety file names,
47+
* also for insensitive-case of method name calls.
48+
*
49+
* @global array $GLOBALS['_Math_Numerical_RootFinding_drivers']
50+
* @_Math_Numerical_RootFinding_drivers
51+
*
52+
* // OLD WAY
53+
$GLOBALS['_Math_Numerical_RootFinding_drivers'] = array(
54+
'bisection' => 'Bisection',
55+
'falseposition' => 'FalsePosition',
56+
'fixedpoint' => 'FixedPoint',
57+
'newtonraphson' => 'NewtonRaphson',
58+
'newtonraphson2' => 'NewtonRaphson2',
59+
'ralstonrabinowitz' => 'RalstonRabinowitz',
60+
'secant' => 'Secant'
61+
);
62+
*/
63+
protected $drivers = array(
64+
'bisection' => 'Bisection',
65+
'falseposition' => 'FalsePosition',
66+
'fixedpoint' => 'FixedPoint',
67+
'newtonraphson' => 'NewtonRaphson',
68+
'newtonraphson2' => 'NewtonRaphson2',
69+
'ralstonrabinowitz' => 'RalstonRabinowitz',
70+
'secant' => 'Secant'
71+
);
72+
6573
// {{{ factory()
6674

6775
public function testFunction($param = 'test param')
@@ -80,41 +88,34 @@ public function testFunction($param = 'test param')
8088
* @access public
8189
* @static
8290
*/
83-
function &factory($method, $options = null)
91+
public static function factory($method, $options = null)
8492
{
8593
$method = strtolower(trim($method));
86-
if (!isset($GLOBALS['_Math_Numerical_RootFinding_drivers'][$method])) {
87-
return PEAR::raiseError('Driver file not found for ' .
94+
if (!isset($this->drivers[$method])) {
95+
throw new \Exception('Driver file not found for ' .
8896
'\'' . $method . '\' method');
8997
}
9098

91-
$method = $GLOBALS['_Math_Numerical_RootFinding_drivers'][$method];
99+
$method = $this->drivers[$method];
92100
$filename = dirname(__FILE__) . '/RootFinding/' . $method . '.php';
93101

94102
if (!file_exists($filename)) {
95-
return PEAR::raiseError('Driver file not found for ' .
103+
throw new \Exception('Driver file not found for ' .
96104
'\'' . $method . '\' method');
97105
}
98106

99107
include_once $filename;
100-
$classname = 'Math_Numerical_RootFinding_' . $method;
108+
$classname = '\Math_Numerical_RootFinding_' . $method;
101109
if (!class_exists($classname)) {
102-
return PEAR::raiseError('Undefined class \'' . $classname . '\'');
110+
throw new \Exception('Undefined class \'' . $classname . '\'');
103111
}
104112

105-
$obj =& new $classname;
113+
$obj = new $classname;
106114
if (!is_object($obj) || !is_a($obj, $classname)) {
107-
return PEAR::raiseError('Failed creating object from class '.
115+
throw new \Exception('Failed creating object from class '.
108116
'\'' . $classname . '\'');
109117
}
110118

111-
if ($options !== null) {
112-
$err = $obj->set($options);
113-
if (PEAR::isError($err)) {
114-
return $err;
115-
}
116-
}
117-
118119
return $obj;
119120
}
120121

Math/Numerical/RootFinding/Bisection.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
* @version CVS: $Id$
2828
*/
2929

30-
/**
31-
* Math_Numerical_RootFinding_Common
32-
*/
33-
require_once 'Math/Numerical/RootFinding/Common.php';
34-
3530
/**
3631
* Bisection/Binary Chopping/Interval Halving/Bolzano method class.
3732
*
@@ -44,7 +39,7 @@
4439
* @version Release: @package_version@
4540
* @link http://pear.php.net/package/Math_Numerical_RootFinding
4641
*/
47-
class Math_Numerical_RootFinding_Bisection extends Math_Numerical_RootFinding_Common
42+
class Math_Numerical_RootFinding_Bisection extends \Math_Numerical_RootFinding_Common
4843
{
4944
// {{{ Constructor
5045

@@ -56,7 +51,7 @@ class Math_Numerical_RootFinding_Bisection extends Math_Numerical_RootFinding_Co
5651
* @access public
5752
* @see Math_Numerical_RootFinding_Common::Math_Numerical_RootFinding_Common()
5853
*/
59-
function Math_Numerical_RootFinding_Bisection($options = null)
54+
public function Math_Numerical_RootFinding_Bisection($options = null)
6055
{
6156
parent::Math_Numerical_RootFinding_Common($options);
6257
}
@@ -70,7 +65,7 @@ function Math_Numerical_RootFinding_Bisection($options = null)
7065
* @access public
7166
* @return void
7267
*/
73-
function infoCompute()
68+
public function infoCompute()
7469
{
7570
print "<h2>Bisection::compute()</h2>\n" .
7671

@@ -105,26 +100,20 @@ function infoCompute()
105100
* @see Math_Numerical_RootFinding::getEqResult()
106101
* @see Math_Numerical_RootFinding_Falseposition::compute()
107102
*/
108-
function compute($fxFunction, $xL, $xU)
103+
public function compute($fxFunction, $xL, $xU)
109104
{
110105
// Validate f(x) equation function.
111-
$err = Math_Numerical_RootFinding_Common::validateEqFunction(
106+
parent::validateEqFunction(
112107
$fxFunction, $xL
113108
);
114-
if (PEAR::isError($err)) {
115-
return $err;
116-
}
117109

118110
// Sets first approximation $xR (Bisection's formula).
119111
$xR = ($xU + $xL) / 2;
120112

121113
// Validate f(x) equation function.
122-
$err = Math_Numerical_RootFinding_Common::validateEqFunction(
114+
parent::validateEqFunction(
123115
$fxFunction, $xR
124116
);
125-
if (PEAR::isError($err)) {
126-
return $err;
127-
}
128117

129118
// Sets maximum iteration and tolerance from options.
130119
$maxIteration = $this->options['max_iteration'];
@@ -136,8 +125,8 @@ function compute($fxFunction, $xL, $xU)
136125

137126
for ($i = 0; $i < $maxIteration; $i++) {
138127
// Calculate f(x), where: x = xL and x = xR
139-
$fxL = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xL);
140-
$fxR = Math_Numerical_RootFinding_Common::getEqResult($fxFunction, $xR);
128+
$fxL = parent::getEqResult($fxFunction, $xL);
129+
$fxR = parent::getEqResult($fxFunction, $xR);
141130

142131
if ($fxL * $fxR < 0) { // Root is at first subinterval.
143132
$xU = $xR;
@@ -158,7 +147,7 @@ function compute($fxFunction, $xL, $xU)
158147
// Detect for divergent rows.
159148
if ($this->isDivergentRows($epsErrors) &&
160149
$this->options['divergent_skip']) {
161-
return PEAR::raiseError(
150+
throw new \Exception(
162151
'Iteration skipped, divergent rows detected'
163152
);
164153
break;

Math/Numerical/RootFinding/Common.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
* @version CVS: $Id$
2727
*/
2828

29-
/**
30-
* PEAR
31-
*/
32-
require_once 'PEAR.php';
33-
3429
/**
3530
* Abstract class contains common properties and methods for specified method
3631
* classes.
@@ -47,6 +42,12 @@
4742
*/
4843
class Math_Numerical_RootFinding_Common
4944
{
45+
public function testFunction()
46+
{
47+
48+
return 'dddddddddd';
49+
}
50+
5051
// {{{ Properties
5152

5253
/**
@@ -103,7 +104,7 @@ class Math_Numerical_RootFinding_Common
103104
* @access public
104105
* @see set()
105106
*/
106-
function Math_Numerical_RootFinding_Common($options = null)
107+
public function Math_Numerical_RootFinding_Common($options = null)
107108
{
108109
if ($options !== null) {
109110
$this->set($options);
@@ -128,31 +129,28 @@ function Math_Numerical_RootFinding_Common($options = null)
128129
* @return bool|PEAR_Error TRUE on success or PEAR_Error on failure.
129130
* @access public
130131
*/
131-
function set($option, $value = null)
132+
public function set($option, $value = null)
132133
{
133134
if (!is_array($option) && !is_string($option)) {
134-
return PEAR::raiseError('Type mismatch for $option argument');
135+
throw new \Exception('Type mismatch for $option argument');
135136
}
136137

137138
if (is_array($option)) {
138139
foreach ($option as $key => $val) {
139140
$err = $this->set($key, $val);
140-
if (PEAR::isError($err)) {
141-
return $err;
142-
}
143141
}
144142
} elseif (is_string($option)) {
145143
if (isset($this->options[$option])) {
146144
if (!isset($value)) {
147-
return PEAR::raiseError('No value given for option ' .
145+
throw new \Exception('No value given for option ' .
148146
'\'' . $option . '\'');
149147
}
150148

151149
// Attempt to casting variable type.
152150
settype($value, gettype($this->options[$option]));
153151
$this->options[$option] = $value;
154152
} else {
155-
return PEAR::raiseError('Unknown option \'' . $option . '\'');
153+
throw new \Exception('Unknown option \'' . $option . '\'');
156154
}
157155
}
158156
}
@@ -168,10 +166,10 @@ function set($option, $value = null)
168166
* @return mixed Value of the option or PEAR_Error on failure.
169167
* @since Method available since Release 1.1.0a1
170168
*/
171-
function get($option)
169+
public function get($option)
172170
{
173171
if (!array_key_exists($option, $this->options)) {
174-
return PEAR::raiseError('Unknown option \'' . $option . '\'');
172+
throw new \Exception('Unknown option \'' . $option . '\'');
175173
}
176174

177175
return $this->options[$option];
@@ -191,9 +189,9 @@ function get($option)
191189
*
192190
* @abstract()
193191
*/
194-
function compute()
192+
public function compute($fxFunction, $dfxFunction, $xR)
195193
{
196-
return PEAR::raiseError('This function not implemented');
194+
throw new \Exception('This function not implemented');
197195
}
198196

199197
// }}}
@@ -205,7 +203,7 @@ function compute()
205203
* @return float Root value.
206204
* @access public
207205
*/
208-
function getRoot()
206+
public function getRoot()
209207
{
210208
return $this->root;
211209
}
@@ -219,7 +217,7 @@ function getRoot()
219217
* @return int Iteration count.
220218
* @access public
221219
*/
222-
function getIterationCount()
220+
public function getIterationCount()
223221
{
224222
return $this->iterationCount;
225223
}
@@ -233,7 +231,7 @@ function getIterationCount()
233231
* @return float Epsilon error.
234232
* @access public
235233
*/
236-
function getEpsError()
234+
public function getEpsError()
237235
{
238236
return $this->epsError;
239237
}
@@ -252,7 +250,7 @@ function getEpsError()
252250
* @return bool TRUE if divergent, otherwise FALSE
253251
* @access public
254252
*/
255-
function isDivergentRows($epsErrors)
253+
public function isDivergentRows($epsErrors)
256254
{
257255
$n = count($epsErrors);
258256
if ($n >= 3) {
@@ -275,7 +273,7 @@ function isDivergentRows($epsErrors)
275273
*
276274
* @access public
277275
*/
278-
function reset()
276+
public function reset()
279277
{
280278
$this->iterationCount = 0;
281279
$this->epsError = 0;
@@ -299,12 +297,9 @@ function reset()
299297
* @see getEqResult()
300298
* @static
301299
*/
302-
function validateEqFunction($eqFunction, $guess = 1)
300+
public static function validateEqFunction($eqFunction, $guess = 1)
303301
{
304-
$err = Math_Numerical_RootFinding_Common::getEqResult($eqFunction, $guess);
305-
if(PEAR::isError($err)) {
306-
return $err;
307-
}
302+
$err = self::getEqResult($eqFunction, $guess);
308303
return true;
309304
}
310305

@@ -321,13 +316,13 @@ function validateEqFunction($eqFunction, $guess = 1)
321316
* @access public
322317
* @static
323318
*/
324-
function getEqResult($eqFunction, $varValue)
319+
public static function getEqResult($eqFunction, $varValue)
325320
{
326321
if (is_callable($eqFunction, false, $callable_name)) {
327322
return call_user_func($eqFunction, $varValue);
328323
}
329324

330-
return PEAR::raiseError('Unable call equation function or method ' .
325+
throw new \Exception('Unable call equation function or method ' .
331326
'\'' . $callable_name . '()\'');
332327
}
333328

0 commit comments

Comments
 (0)