Completed
Push — master ( a00052...e176fb )
by Tim
06:25
created

Flt::__getClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * \AppserverIo\Lang\Flt
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/lang
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Lang;
22
23
/**
24
 * This class implements functionality to handle
25
 * a float value as object.
26
 *
27
 * @author    Tim Wagner <[email protected]>
28
 * @copyright 2015 TechDivision GmbH <[email protected]>
29
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
30
 * @link      https://github.com/appserver-io/lang
31
 * @link      http://www.appserver.io
32
 */
33
class Flt extends Number implements \Serializable
34
{
35
36
    /**
37
     * The value of the Float.
38
     *
39
     * @var float
40
     */
41
    protected $value;
42
43
    /**
44
     * Constructs a newly allocated <code>Float</code> object that
45
     * represents the primitive <code>float</code> argument.
46
     *
47
     * @param float $value The value to be represented by the <code>Float</code>
48
     */
49
    public function __construct($value)
50
    {
51
        // initialize property default values here, as declarative default values may break thread safety,
52
        // when utilizing static and non-static access on class methods within same thread context!
53
        $this->value = null;
54
55
        if (!is_float($value)) {
56
            NumberFormatException::forInputString($value);
0 ignored issues
show
Bug introduced by
The method forInputString() does not exist on AppserverIo\Lang\NumberFormatException. Did you maybe mean forInputStrg()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
        }
58
        $this->value = $value;
59
    }
60
61
    /**
62
     * This method returns the class name as
63
     * a string.
64
     *
65
     * @return string
66
     */
67
    public static function __getClass()
68
    {
69
        return __CLASS__;
70
    }
71
72
    /**
73
     * Returns a <code>Float</code> object holding the
74
     * <code>float</code> value represented by the argument string
75
     * <code>s</code>.
76
     * <p>
77
     * If <code>s</code> is <code>null</code>, then a
78
     * <code>NullPointerException</code> is thrown.
79
     * <p>
80
     * Leading and trailing whitespace characters in <code>s</code>
81
     * are ignored. The rest of <code>s</code> should constitute a
82
     * <i>Float</i> as described by the lexical syntax rules:
83
     * <blockquote><i>
84
     * <dl>
85
     * <dt>Float:
86
     * <dd><i>Sign<sub>opt</sub></i> <code>NaN</code>
87
     * <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code>
88
     * <dd>Sign<sub>opt</sub> FloatingPointLiteral
89
     * </dl>
90
     * </i></blockquote>
91
     * where <i>Sign</i> and <i>FloatingPointLiteral</i> are as
92
     * defined in <a href="http://java.sun.com/docs/books/jls/second_edition/
93
     * html/lexical.doc.html#230798">&sect;3.10.2</a>
94
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
95
     * Language Specification</a>. If <code>s</code> does not have the
96
     * form of a <i>Float</i>, then a
97
     * <code>NumberFormatException</code> is thrown. Otherwise,
98
     * <code>s</code> is regarded as representing an exact decimal
99
     * value in the usual "computerized scientific notation"; this
100
     * exact decimal value is then conceptually converted to an
101
     * "infinitely precise" binary value that is then rounded to type
102
     * <code>float</code> by the usual round-to-nearest rule of IEEE
103
     * 754 floating-point arithmetic, which includes preserving the
104
     * sign of a zero value. Finally, a <code>Float</code> object
105
     * representing this <code>float</code> value is returned.
106
     * <p>
107
     * To interpret localized string representations of a
108
     * floating-point value, use subclasses of {@link
109
     * java.text.NumberFormat}.
110
     *
111
     * <p>Note that trailing format specifiers, specifiers that
112
     * determine the type of a floating-point literal
113
     * (<code>1.0f</code> is a <code>float</code> value;
114
     * <code>1.0d</code> is a <code>double</code> value), do
115
     * <em>not</em> influence the results of this method. In other
116
     * words, the numerical value of the input string is converted
117
     * directly to the target floating-point type. In general, the
118
     * two-step sequence of conversions, string to <code>double</code>
119
     * followed by <code>double</code> to <code>float</code>, is
120
     * <em>not</em> equivalent to converting a string directly to
121
     * <code>float</code>. For example, if first converted to an
122
     * intermediate <code>double</code> and then to
123
     * <code>float</code>, the string<br>
124
     * <code>"1.00000017881393421514957253748434595763683319091796875001d"
125
     * </code><br>results in the <code>float</code> value
126
     * <code>1.0000002f</code>; if the string is converted directly to
127
     * <code>float</code>, <code>1.000000<b>1</b>f</code> results.
128
     *
129
     * @param \AppserverIo\Lang\Strg $string The string to be parsed
130
     *
131
     * @return \AppserverIo\Lang\Flt A <code>Float</code> object holding the value represented by the <code>Strg</code> argument
132
     * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable number
133
     */
134 View Code Duplication
    public static function valueOf(Strg $string)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        if (! preg_match("/([0-9\.-]+)/", $string->stringValue())) {
137
            NumberFormatException::forInputStrg($string->stringValue());
0 ignored issues
show
Documentation introduced by
$string->stringValue() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
        }
139
        if (! is_numeric($string->stringValue())) {
140
            NumberFormatException::forInputStrg($string->stringValue());
0 ignored issues
show
Documentation introduced by
$string->stringValue() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
        }
142
        return new Flt((float) $string->stringValue());
143
    }
144
145
    /**
146
     * Returns a new <code>float</code> initialized to the value
147
     * represented by the specified <code>Strg</code>, as performed
148
     * by the <code>valueOf</code> method of class <code>Float</code>.
149
     *
150
     * @param \AppserverIo\Lang\Strg $string She string to be parsed
151
     *
152
     * @return float The <code>float</code> value represented by the string argument
153
     * @exception \AppserverIo\Lang\NumberFormatException If the string does not contain a parsable <code>float</code>.
154
     * @see \AppserverIo\Lang\Flt::valueOf($string)
155
     */
156
    public static function parseFloat(Strg $string)
157
    {
158
        return Flt::valueOf($string)->floatValue();
159
    }
160
161
    /**
162
     * Returns the value of the specified number as a <code>float</code>.
163
     * This may involve rounding.
164
     *
165
     * @return float The numeric value represented by this object after conversion to type <code>float</code>
166
     * @see \AppserverIo\Lang\Number::floatValue()
167
     */
168
    public function floatValue()
169
    {
170
        return $this->value;
171
    }
172
173
    /**
174
     * Returns the value of the specified number as an <code>int</code>.
175
     * This may involve rounding or truncation.
176
     *
177
     * @return integer The numeric value represented by this object after conversion to type <code>int</code>
178
     * @see \AppserverIo\Lang\Number::intValue()
179
     */
180
    public function intValue()
181
    {
182
        return (int) $this->value;
183
    }
184
185
    /**
186
     * Returns the value of the specified number as a <code>double</code>.
187
     * This may involve rounding.
188
     *
189
     * @return double The numeric value represented by this object after conversion to type <code>double</code>
190
     * @see \AppserverIo\Lang\Number::doubleValue()
191
     */
192
    public function doubleValue()
193
    {
194
        return (double) $this->value;
195
    }
196
197
    /**
198
     * This method has to be called to serialize the Float.
199
     *
200
     * @return string Returns a serialized version of the Float
201
     * @see \Serializable::serialize()
202
     */
203
    public function serialize()
204
    {
205
        return serialize($this->value);
206
    }
207
208
    /**
209
     * This method unserializes the passed string and initializes the Float
210
     * itself with the data.
211
     *
212
     * @param string $data Holds the data of the instance as serialized string
213
     *
214
     * @return void
215
     * @see \Serializable::unserialize($data)
216
     */
217
    public function unserialize($data)
218
    {
219
        $this->value = unserialize($data);
220
    }
221
222
    /**
223
     * This object as String returned.
224
     *
225
     * @return \AppserverIo\Lang\Strg The value as String.
226
     */
227
    public function toString()
228
    {
229
        return new Strg($this->value);
230
    }
231
232
    /**
233
     * This method returns the class as
234
     * a string representation.
235
     *
236
     * @return string The objects string representation
237
     * @see \AppserverIo\Lang\Object::__toString()
238
     */
239
    public function __toString()
240
    {
241
        $string = new Strg($this->value);
242
        return $string->stringValue();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $string->stringValue(); (array) is incompatible with the return type of the parent method AppserverIo\Lang\Object::__toString of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
243
    }
244
245
    /**
246
     * Returns true if the passed value is equal.
247
     *
248
     * @param \AppserverIo\Lang\Object $val The value to check
249
     *
250
     * @return boolean
251
     */
252
    public function equals(Object $val)
253
    {
254
        if ($val instanceof Flt) {
255
            return $this->floatValue() == $val->floatValue();
256
        }
257
        return false;
258
    }
259
260
    /**
261
     * Adds the value of the passed Float.
262
     *
263
     * @param \AppserverIo\Lang\Flt $toAdd The Float to add
264
     *
265
     * @return \AppserverIo\Lang\Flt The instance
266
     */
267
    public function add(Flt $toAdd)
268
    {
269
        $this->value += $toAdd->floatValue();
270
        return $this;
271
    }
272
273
    /**
274
     * Subtracts the value of the passed Float.
275
     *
276
     * @param \AppserverIo\Lang\Flt $toSubtract The Float to subtract
277
     *
278
     * @return \AppserverIo\Lang\Flt The instance
279
     */
280
    public function subtract(Flt $toSubtract)
281
    {
282
        $this->value -= $toSubtract->floatValue();
283
        return $this;
284
    }
285
286
    /**
287
     * Multiplies the Float with the passed one.
288
     *
289
     * @param \AppserverIo\Lang\Flt $toMultiply The Float to multiply
290
     *
291
     * @return \AppserverIo\Lang\Flt The instance
292
     */
293
    public function multiply(Flt $toMultiply)
294
    {
295
        $this->value *= $toMultiply->intValue();
296
        return $this;
297
    }
298
299
    /**
300
     * Divides the Float by the passed one.
301
     *
302
     * @param \AppserverIo\Lang\Flt $dividyBy The Float to dividy by
303
     *
304
     * @return \AppserverIo\Lang\Flt The instance
305
     */
306
    public function divide(Flt $dividyBy)
307
    {
308
        $this->value = $this->value / $dividyBy->floatValue();
309
        return $this;
310
    }
311
}
312