Completed
Push — dev ( 9d6294...c45123 )
by Jordan
02:10
created

Numbers::make()   C

Complexity

Conditions 15
Paths 20

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 130.2

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 7
cts 35
cp 0.2
rs 6.5498
c 0
b 0
f 0
cc 15
eloc 36
nc 20
nop 4
crap 130.2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Samsara\Fermat;
4
5
use Samsara\Exceptions\UsageError\IntegrityConstraint;
6
use Samsara\Fermat\Types\Base\CoordinateInterface;
7
use Samsara\Fermat\Types\Base\DecimalInterface;
8
use Samsara\Fermat\Types\Base\FractionInterface;
9
use Samsara\Fermat\Types\Fraction;
10
use Samsara\Fermat\Values\CartesianCoordinate;
11
use Samsara\Fermat\Values\Currency;
12
use Samsara\Fermat\Values\ImmutableFraction;
13
use Samsara\Fermat\Values\ImmutableNumber;
14
use Samsara\Fermat\Values\MutableFraction;
15
use Samsara\Fermat\Values\MutableNumber;
16
use Samsara\Fermat\Types\Base\NumberInterface;
17
18
class Numbers
19
{
20
21
    const MUTABLE = MutableNumber::class;
22
    const IMMUTABLE = ImmutableNumber::class;
23
    const MUTABLE_FRACTION = MutableFraction::class;
24
    const IMMUTABLE_FRACTION = ImmutableFraction::class;
25
    const CARTESIAN_COORDINATE = CartesianCoordinate::class;
26
    /* 105 digits after decimal, which is going to be overkill in almost all places */
27
    const PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
28
    /* Tau (2pi) to 100 digits */
29
    const TAU = '6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234136';
30
    /* Euler's Number to 100 digits */
31
    const E = '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427';
32
    /* Golden Ratio to 100 digits */
33
    const GOLDEN_RATIO = '1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137';
34
    /* Natural log of 10 to 100 digits */
35
    const LN_10 = '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298';
36
37
    /**
38
     * @param $type
39
     * @param $value
40
     * @param int|null $precision
41
     * @param int $base
42
     *
43
     * @throws IntegrityConstraint
44
     * @return ImmutableNumber|MutableNumber|ImmutableFraction|MutableFraction|CartesianCoordinate|NumberInterface|FractionInterface|CoordinateInterface
45
     */
46 23
    public static function make($type, $value, $precision = null, $base = 10)
47
    {
48
49 23
        if (is_object($type)) {
50 18
            $type = get_class($type);
51
        }
52
53 23
        if ($type == static::IMMUTABLE) {
54 23
            return new ImmutableNumber(trim($value), $precision, $base);
55 1
        } elseif ($type == static::MUTABLE) {
56 1
            return new MutableNumber(trim($value), $precision, $base);
57
        } elseif ($type == static::IMMUTABLE_FRACTION) {
58
            return self::makeFractionFromString($value, $type)->convertToBase($base);
59
        } elseif ($type == static::MUTABLE_FRACTION) {
60
            return self::makeFractionFromString($value, $type)->convertToBase($base);
61
        } elseif ($type == static::CARTESIAN_COORDINATE && is_array($value)) {
62
            return new CartesianCoordinate($value);
63
        } else {
64
            $reflector = new \ReflectionClass($type);
65
66
            if ($reflector->implementsInterface(FractionInterface::class) && $reflector->isSubclassOf(Fraction::class)) {
67
                return Numbers::makeFractionFromString($value, $reflector->getName(), $base);
0 ignored issues
show
Bug introduced by
Consider using $reflector->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
68
            }
69
70
            if ($reflector->implementsInterface(CoordinateInterface::class) && is_array($value)) {
71
                /** @var CoordinateInterface $customCoordinate */
72
                $customCoordinate = $reflector->newInstance([
73
                    $value
74
                ]);
75
                return $customCoordinate;
76
            }
77
78
            if ($reflector->implementsInterface(CoordinateInterface::class) && !is_array($value)) {
79
                throw new IntegrityConstraint(
80
                    'The $value for a CoordinateInterface must be an array',
81
                    'Provide an array for the $value',
82
                    'A CoordinateInterface expects the value to be an array of axes and values'
83
                );
84
            }
85
86
            if ($reflector->implementsInterface(NumberInterface::class)) {
87
                /** @var NumberInterface $customNumber */
88
                $customNumber = $reflector->newInstance([
89
                    trim($value),
90
                    $precision,
91
                    $base
92
                ]);
93
                return $customNumber;
94
            }
95
        }
96
97
        throw new IntegrityConstraint(
98
            '$type must be an implementation of NumberInterface',
99
            'Provide a type that implements NumberInterface (the Numbers class contains constants for the built in ones)',
100
            'The $type argument was not an implementation of NumberInterface'
101
        );
102
    }
103
104
    /**
105
     * @param $type
106
     * @param $value
107
     * @param int|null $precision
108
     * @param int $base
109
     * @return NumberInterface
110
     */
111
    public static function makeFromBase10($type, $value, $precision = null, $base = 10)
112
    {
113
        /**
114
         * @var ImmutableNumber|MutableNumber
115
         */
116
        $number = self::make($type, $value, $precision, 10);
117
118
        return $number->convertToBase($base);
0 ignored issues
show
Bug introduced by
The method convertToBase does only exist in Samsara\Fermat\Types\Base\NumberInterface, but not in Samsara\Fermat\Types\Bas...\Base\FractionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
119
    }
120
121
    /**
122
     * @param $type
123
     * @param int|float|string|NumberInterface|DecimalInterface|FractionInterface $value
124
     * @param int|null $precision
125
     * @param int $base
126
     *
127
     * @throws IntegrityConstraint
128
     * @return ImmutableNumber|MutableNumber|NumberInterface|ImmutableNumber[]|MutableNumber[]|NumberInterface[]
129
     */
130 25
    public static function makeOrDont($type, $value, $precision = null, $base = 10)
131
    {
132
133 25
        if (is_object($value)) {
134 23
            $reflector = new \ReflectionClass($value);
135
136 23
            if ($value instanceof $type) {
137 23
                return $value;
138
            }
139
140 1
            if ($reflector->implementsInterface(NumberInterface::class)) {
141 1
                return static::make($type, $value->getValue(), $precision, $base);
0 ignored issues
show
Bug introduced by
The method getValue does only exist in Samsara\Fermat\Types\Base\NumberInterface, but not in Samsara\Fermat\Types\Bas...\Base\FractionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142
            }
143 23
        } elseif (is_numeric($value)) {
144 23
            return static::make($type, $value, $precision, $base);
145
        } elseif (is_array($value)) {
146
            $newInput = [];
147
            
148
            foreach ($value as $key => $item) {
149
                $newInput[$key] = static::makeOrDont($type, $item, $precision, $base);
150
            }
151
152
            return $newInput;
153
        }
154
155
        throw new IntegrityConstraint(
156
            '$input must be an int, float, numeric string, or an implementation of NumberInterface',
157
            'Provide any of the MANY valid inputs',
158
            'The $input argument was not numeric or an implementation of NumberInterface.'
159
        );
160
161
    }
162
163
    /**
164
     * @param $value
165
     * @param $type
166
     *
167
     * @return ImmutableFraction|MutableFraction|FractionInterface
168
     * @throws IntegrityConstraint
169
     */
170
    public static function makeFractionFromString($value, $type = self::IMMUTABLE_FRACTION, $base = 10)
171
    {
172
        $parts = explode('/', $value);
173
174
        if (count($parts) > 2) {
175
            throw new IntegrityConstraint(
176
                'Only one division symbol (/) can be used',
177
                'Change the calling code to not provide more than one division symbol',
178
                'makeFractionFromString needs either one or zero division symbols in the $value argument; '.$value.' given'
179
            );
180
        }
181
182
        /** @var ImmutableNumber $numerator */
183
        $numerator = Numbers::make(Numbers::IMMUTABLE, trim(ltrim($parts[0])))->round();
184
        /** @var ImmutableNumber $denominator */
185
        $denominator = isset($parts[1]) ? Numbers::make(Numbers::IMMUTABLE, trim(ltrim($parts[1])))->round() : Numbers::makeOne();
186
187
        if ($type == self::IMMUTABLE_FRACTION) {
188
            return new ImmutableFraction($numerator, $denominator, $base);
189
        } elseif ($type == self::MUTABLE_FRACTION) {
190
            return new MutableFraction($numerator, $denominator, $base);
191
        } else {
192
            $reflector = new \ReflectionClass($type);
193
194
            if ($reflector->implementsInterface(FractionInterface::class) && $reflector->isSubclassOf(Fraction::class)) {
195
                /** @var FractionInterface|Fraction $customFraction */
196
                $customFraction = $reflector->newInstance([
0 ignored issues
show
Bug Compatibility introduced by
The expression $reflector->newInstance(... $denominator, $base)); of type object adds the type Samsara\Fermat\Types\Fraction to the return on line 201 which is incompatible with the return type documented by Samsara\Fermat\Numbers::makeFractionFromString of type Samsara\Fermat\Types\Base\FractionInterface.
Loading history...
197
                    $numerator,
198
                    $denominator,
199
                    $base
200
                ]);
201
                return $customFraction;
202
            }
203
204
            throw new IntegrityConstraint(
205
                'Type must be ImmutableFraction or MutableFraction',
206
                'Alter to calling code to use the correct type',
207
                'makeFractionFromString can only make objects of type ImmutableFraction or MutableFraction; '.$type.' given'
208
            );
209
        }
210
    }
211
212
    /**
213
     * @param int|null $precision
214
     *
215
     * @throws IntegrityConstraint
216
     * @return NumberInterface
217
     */
218 5 View Code Duplication
    public static function makePi($precision = null)
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...
219
    {
220
        
221 5
        if (!is_null($precision) && ($precision > 100 || $precision < 1)) {
222
            throw new IntegrityConstraint(
223
                '$precision must be between 1 and 100 inclusive',
224
                'Provide a precision within range',
225
                'The PI constant cannot have a precision higher than the constant stored (100)'
226
            );
227
        }
228
        
229 5
        if (!is_null($precision)) {
230
            return self::make(self::IMMUTABLE, self::PI, $precision)->roundToPrecision($precision);
231
        } else {
232 5
            return self::make(self::IMMUTABLE, self::PI, 100);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::IMMUTABLE, self::PI, 100); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makePi of type Samsara\Fermat\Types\Base\NumberInterface.

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...
233
        }
234
        
235
    }
236
237
    /**
238
     * @param int|null $precision
239
     *
240
     * @throws IntegrityConstraint
241
     * @return NumberInterface
242
     */
243 6 View Code Duplication
    public static function makeTau($precision = null)
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...
244
    {
245 6
        if (!is_null($precision) && ($precision > 100 || $precision < 1)) {
246
            throw new IntegrityConstraint(
247
                '$precision must be between 1 and 100 inclusive',
248
                'Provide a precision within range',
249
                'The TAU constant cannot have a precision higher than the constant stored (100)'
250
            );
251
        }
252
253 6
        if (!is_null($precision)) {
254
            return self::make(self::IMMUTABLE, self::TAU, $precision)->roundToPrecision($precision);
255
        } else {
256 6
            return self::make(self::IMMUTABLE, self::TAU, 100);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::...TABLE, self::TAU, 100); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeTau of type Samsara\Fermat\Types\Base\NumberInterface.

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...
257
        }
258
    }
259
260
    /**
261
     * @param int|null $precision
262
     *
263
     * @return NumberInterface
264
     */
265 6
    public static function make2Pi($precision = null)
266
    {
267 6
        return self::makeTau($precision);
268
    }
269
270
    /**
271
     * @param int|null $precision
272
     *
273
     * @throws IntegrityConstraint
274
     * @return NumberInterface
275
     */
276 4 View Code Duplication
    public static function makeE($precision = null)
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...
277
    {
278
279 4
        if (!is_null($precision) && ($precision > 100 || $precision < 1)) {
280
            throw new IntegrityConstraint(
281
                '$precision must be between 1 and 100 inclusive',
282
                'Provide a precision within range',
283
                'The E constant cannot have a precision higher than the constant stored (100)'
284
            );
285
        }
286
287 4
        if (!is_null($precision)) {
288
            return self::make(self::IMMUTABLE, self::E, $precision)->roundToPrecision($precision);
289
        } else {
290 4
            return self::make(self::IMMUTABLE, self::E, 100);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::IMMUTABLE, self::E, 100); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeE of type Samsara\Fermat\Types\Base\NumberInterface.

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...
291
        }
292
293
    }
294
295
    /**
296
     * @param int|null $precision
297
     *
298
     * @throws IntegrityConstraint
299
     * @return NumberInterface
300
     */
301 View Code Duplication
    public static function makeGoldenRatio($precision = null)
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...
302
    {
303
304
        if (!is_null($precision) && ($precision > 100 || $precision < 1)) {
305
            throw new IntegrityConstraint(
306
                '$precision must be between 1 and 100 inclusive',
307
                'Provide a precision within range',
308
                'The Golden Ratio constant cannot have a precision higher than the constant stored (100)'
309
            );
310
        }
311
312
        if (!is_null($precision)) {
313
            return self::make(self::IMMUTABLE, self::GOLDEN_RATIO, $precision)->roundToPrecision($precision);
314
        } else {
315
            return self::make(self::IMMUTABLE, self::GOLDEN_RATIO, 100);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::...lf::GOLDEN_RATIO, 100); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeGoldenRatio of type Samsara\Fermat\Types\Base\NumberInterface.

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...
316
        }
317
318
    }
319
320
    /**
321
     * @param int|null $precision
322
     *
323
     * @throws IntegrityConstraint
324
     * @return NumberInterface
325
     */
326 1 View Code Duplication
    public static function makeNaturalLog10($precision = null)
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...
327
    {
328
329 1
        if (!is_null($precision) && ($precision > 100 || $precision < 1)) {
330
            throw new IntegrityConstraint(
331
                '$precision must be between 1 and 100 inclusive',
332
                'Provide a precision within range',
333
                'The natural log of 10 constant cannot have a precision higher than the constant stored (100)'
334
            );
335
        }
336
337 1
        if (!is_null($precision)) {
338
            return self::make(self::IMMUTABLE, self::LN_10, $precision)->roundToPrecision($precision);
339
        } else {
340 1
            return self::make(self::IMMUTABLE, self::LN_10, 100);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::...BLE, self::LN_10, 100); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeNaturalLog10 of type Samsara\Fermat\Types\Base\NumberInterface.

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...
341
        }
342
343
    }
344
345
    /**
346
     * @return ImmutableNumber
347
     */
348 12
    public static function makeOne($precision = null)
349
    {
350 12
        return self::make(self::IMMUTABLE, 1, $precision);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::IMMUTABLE, 1, $precision); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeOne of type Samsara\Fermat\Values\ImmutableNumber.

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...
351
    }
352
353
    /**
354
     * @return ImmutableNumber
355
     */
356 12
    public static function makeZero($precision = null)
357
    {
358 12
        return self::make(self::IMMUTABLE, 0, $precision);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return self::make(self::IMMUTABLE, 0, $precision); (Samsara\Fermat\Types\Bas...ase\CoordinateInterface) is incompatible with the return type documented by Samsara\Fermat\Numbers::makeZero of type Samsara\Fermat\Values\ImmutableNumber.

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...
359
    }
360
361
}