Completed
Push — master ( b523b7...cbe5c5 )
by Woody
10s
created

PhoneNumber::util()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Equip\ValueObject;
4
5
use InvalidArgumentException;
6
use libphonenumber\PhoneNumberFormat;
7
use libphonenumber\PhoneNumberUtil;
8
9
class PhoneNumber
10
{
11
    /**
12
     * @var libphonenumber\PhoneNumber|null
13
     */
14
    private $number;
15
16 12
    public function __construct($number, $is_required = true)
17
    {
18 12
        static $empty_values = [null, ''];
19
20 12
        if (!$is_required && in_array($number, $empty_values, true)) {
21 1
            $number = null;
22 12
        } elseif (!is_string($number)) {
23 3
            throw new InvalidArgumentException('Value must be a phone number');
24
        } else {
25 8
            $number = $this->util()->parse($number, $this->preferredRegion());
26
27
            // Parsing does not ensure a valid phone number format, it only ensures
28
            // a general level of consistency in the numbers and sets a region.
29
30 8
            if (!$this->util()->isValidNumber($number)) {
31 4
                throw new InvalidArgumentException('Value must be a valid phone number for the region');
32
            }
33
        }
34
35 5
        $this->number = $number;
0 ignored issues
show
Documentation Bug introduced by
It seems like $number can also be of type object<libphonenumber\PhoneNumber>. However, the property $number is declared as type object<Equip\ValueObject...umber\PhoneNumber>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36 5
    }
37
38 5
    public function value()
39
    {
40 5
        if ($this->number) {
41 4
            return $this->util()->format($this->number, $this->preferredFormat());
42
        }
43
44 1
        return null;
45
    }
46
47 8
    protected function util()
48
    {
49 8
        return PhoneNumberUtil::getInstance();
50
    }
51
52 8
    protected function preferredRegion()
53
    {
54 8
        return PhoneNumberUtil::UNKNOWN_REGION;
55
    }
56
57 4
    protected function preferredFormat()
58
    {
59 4
        return PhoneNumberFormat::E164;
60
    }
61
}
62