Completed
Push — dev ( c6e1e9...6f89f3 )
by Jordan
02:20
created

Currency::__toString()   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
namespace Samsara\Fermat\Values;
4
5
use Samsara\Fermat\Numbers;
6
use Samsara\Fermat\Types\Base\DecimalInterface;
7
use Samsara\Fermat\Types\Base\NumberInterface;
8
use Samsara\Fermat\Types\Number;
9
10
class Currency extends Number implements NumberInterface, DecimalInterface
11
{
12
    const DOLLAR = '$';
13
    const EURO = '€';
14
    const POUND = '£';
15
    const YUAN = '¥';
16
    const YEN = '¥';
17
    const WON = '₩';
18
    const RUBLE = '₽';
19
    const RIYAL = '﷼';
20
    const RUPEE_INDIA = '₹';
21
22
    protected $symbol;
23
24
    public function __construct($value, $symbol = Currency::DOLLAR, $precision = null, $base = 10)
25
    {
26
        $this->symbol = $symbol;
27
28
        parent::__construct($value, $precision, $base);
29
    }
30
31
    public function __toString()
32
    {
33
        return $this->symbol.$this->multiply(100)->floor()->divide(100)->getValue();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Samsara\Fermat\Types\Base\NumberInterface as the method floor() does only exist in the following implementations of said interface: Samsara\Fermat\Values\Currency, Samsara\Fermat\Values\ImmutableNumber, Samsara\Fermat\Values\MutableNumber.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
34
    }
35
36
    public function modulo($mod)
37
    {
38
        $oldBase = $this->convertForModification();
39
40
        return (new Currency(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->convertFromModification($oldBase);
41
    }
42
43 View Code Duplication
    public function continuousModulo($mod)
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...
44
    {
45
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod);
46
47
        $multiple = $this->divide($mod)->floor();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Samsara\Fermat\Types\Base\NumberInterface as the method floor() does only exist in the following implementations of said interface: Samsara\Fermat\Values\Currency, Samsara\Fermat\Values\ImmutableNumber, Samsara\Fermat\Values\MutableNumber.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48
49
        $remainder = $this->subtract($mod->multiply($multiple));
50
51
        return $remainder;
52
    }
53
54
    public function compoundInterest($interest, $periods)
55
    {
56
57
        $interest = Numbers::makeOrDont(Numbers::IMMUTABLE, $interest);
58
        $periods = Numbers::makeOrDont(Numbers::IMMUTABLE, $periods);
59
        $e = Numbers::makeE();
60
61
        return $this->setValue($this->multiply($e->pow($periods->multiply($interest))));
62
63
    }
64
65
    /**
66
     * @param $value
67
     *
68
     * @return Currency
69
     */
70
    protected function setValue($value)
71
    {
72
        return new Currency($value, $this->getPrecision(), $this->getBase());
73
    }
74
75
}