Completed
Push — master ( 939550...54c69f )
by Joshua
16s
created

ShortNumberInfoTest::testIsSmsService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace libphonenumber\Tests\core;
4
5
use libphonenumber\CountryCodeToRegionCodeMapForTesting;
6
use libphonenumber\NumberParseException;
7
use libphonenumber\PhoneNumber;
8
use libphonenumber\PhoneNumberUtil;
9
use libphonenumber\RegionCode;
10
use libphonenumber\ShortNumberCost;
11
use libphonenumber\ShortNumberInfo;
12
13
class ShortNumberInfoTest extends \PHPUnit_Framework_TestCase
14
{
15
    private static $plusSymbol;
16
    /**
17
     * @var PhoneNumberUtil
18
     */
19
    protected $phoneUtil;
20
    /**
21
     * @var ShortNumberInfo
22
     */
23
    private $shortInfo;
24
25
    public function setUp()
26
    {
27
        self::$plusSymbol = pack('H*', 'efbc8b');
28
29
        PhoneNumberUtil::resetInstance();
30
        ShortNumberInfo::resetInstance();
31
        $this->phoneUtil = PhoneNumberUtil::getInstance(
32
            PhoneNumberUtilTest::TEST_META_DATA_FILE_PREFIX,
33
            CountryCodeToRegionCodeMapForTesting::$countryCodeToRegionCodeMapForTesting
34
        );
35
        $this->shortInfo = ShortNumberInfo::getInstance();
36
    }
37
38
    public function testIsPossibleShortNumber()
39
    {
40
        $possibleNumber = new PhoneNumber();
41
        $possibleNumber->setCountryCode(33)->setNationalNumber(123456);
42
43
        $this->assertTrue($this->shortInfo->isPossibleShortNumber($possibleNumber));
44
        $this->assertTrue($this->shortInfo->isPossibleShortNumberForRegion($this->parse(123456, RegionCode::FR), RegionCode::FR));
0 ignored issues
show
Bug introduced by
It seems like $this->parse(123456, \li...enumber\RegionCode::FR) can be null; however, isPossibleShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
45
46
        $impossibleNumber = new PhoneNumber();
47
        $impossibleNumber->setCountryCode(33)->setNationalNumber(9);
48
        $this->assertFalse($this->shortInfo->isPossibleShortNumber($impossibleNumber));
49
50
        // Note that GB and GG share the country calling code 44, and that this number is possible but
51
        // not valid.
52
        $gbNumber = new PhoneNumber();
53
        $gbNumber->setCountryCode(44)->setNationalNumber(11001);
54
        $this->assertTrue($this->shortInfo->isPossibleShortNumber($gbNumber));
55
    }
56
57
    public function testIsValidShortNumber()
58
    {
59
        $phoneNumberObj = new PhoneNumber();
60
        $phoneNumberObj->setCountryCode(33)->setNationalNumber(1010);
61
        $this->assertTrue($this->shortInfo->isValidShortNumber($phoneNumberObj));
62
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse(1010, RegionCode::FR), RegionCode::FR));
0 ignored issues
show
Bug introduced by
It seems like $this->parse(1010, \libp...enumber\RegionCode::FR) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
64
        $phoneNumberObj = new PhoneNumber();
65
        $phoneNumberObj->setCountryCode(33)->setNationalNumber(123456);
66
        $this->assertFalse($this->shortInfo->isValidShortNumber($phoneNumberObj));
67
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse(123456, RegionCode::FR), RegionCode::FR));
0 ignored issues
show
Bug introduced by
It seems like $this->parse(123456, \li...enumber\RegionCode::FR) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68
69
        // Note that GB and GG share the country calling code 44
70
        $phoneNumberObj = new PhoneNumber();
71
        $phoneNumberObj->setCountryCode(44)->setNationalNumber(18001);
72
        $this->assertTrue($this->shortInfo->isValidShortNumber($phoneNumberObj));
73
    }
74
75
    public function testIsCarrierSpecific()
76
    {
77
        $carrierSpecificNumber = new PhoneNumber();
78
        $carrierSpecificNumber->setCountryCode(1)->setNationalNumber(33669);
79
        $this->assertTrue($this->shortInfo->isCarrierSpecific($carrierSpecificNumber));
80
        $this->assertTrue($this->shortInfo->isCarrierSpecificForRegion($this->parse('33669', RegionCode::US), RegionCode::US));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('33669', \l...enumber\RegionCode::US) can be null; however, isCarrierSpecificForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
81
82
        $notCarrierSpecificNumber = new PhoneNumber();
83
        $notCarrierSpecificNumber->setCountryCode(1)->setNationalNumber(911);
84
        $this->assertFalse($this->shortInfo->isCarrierSpecific($notCarrierSpecificNumber));
85
        $this->assertFalse($this->shortInfo->isCarrierSpecificForRegion($this->parse('911', RegionCode::US), RegionCode::US));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('911', \lib...enumber\RegionCode::US) can be null; however, isCarrierSpecificForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
86
87
        $carrierSpecificNumberForSomeRegion = new PhoneNumber();
88
        $carrierSpecificNumberForSomeRegion->setCountryCode(1)->setNationalNumber(211);
89
        $this->assertTrue($this->shortInfo->isCarrierSpecific($carrierSpecificNumberForSomeRegion));
90
        $this->assertTrue($this->shortInfo->isCarrierSpecificForRegion($carrierSpecificNumberForSomeRegion, RegionCode::US));
91
        $this->assertFalse($this->shortInfo->isCarrierSpecificForRegion($carrierSpecificNumberForSomeRegion, RegionCode::BB));
92
    }
93
94
    public function testIsSmsService()
95
    {
96
        $smsServiceNumberForSomeRegion = new PhoneNumber();
97
        $smsServiceNumberForSomeRegion->setCountryCode(1)->setNationalNumber(21234);
98
        $this->assertTrue($this->shortInfo->isSmsServiceForRegion($smsServiceNumberForSomeRegion, RegionCode::US));
99
        $this->assertFalse($this->shortInfo->isSmsServiceForRegion($smsServiceNumberForSomeRegion, RegionCode::BB));
100
    }
101
102
    public function testGetExpectedCost()
103
    {
104
        $premiumRateExample = $this->shortInfo->getExampleShortNumberForCost(
105
            RegionCode::FR,
106
            ShortNumberCost::PREMIUM_RATE
107
        );
108
        $this->assertEquals(
109
            ShortNumberCost::PREMIUM_RATE,
110
            $this->shortInfo->getExpectedCostForRegion($this->parse($premiumRateExample, RegionCode::FR), RegionCode::FR)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($premiumRat...enumber\RegionCode::FR) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
111
        );
112
113
        $premiumRateNumber = new PhoneNumber();
114
        $premiumRateNumber->setCountryCode(33)->setNationalNumber($premiumRateExample);
115
        $this->assertEquals(ShortNumberCost::PREMIUM_RATE, $this->shortInfo->getExpectedCost($premiumRateNumber));
116
117
        $standardRateExample = $this->shortInfo->getExampleShortNumberForCost(
118
            RegionCode::FR,
119
            ShortNumberCost::STANDARD_RATE
120
        );
121
        $this->assertEquals(
122
            ShortNumberCost::STANDARD_RATE,
123
            $this->shortInfo->getExpectedCostForRegion($this->parse($standardRateExample, RegionCode::FR), RegionCode::FR)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($standardRa...enumber\RegionCode::FR) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
124
        );
125
126
        $standardRateNumber = new PhoneNumber();
127
        $standardRateNumber->setCountryCode(33)->setNationalNumber($standardRateExample);
128
        $this->assertEquals(ShortNumberCost::STANDARD_RATE, $this->shortInfo->getExpectedCost($standardRateNumber));
129
130
        $tollFreeExample = $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE);
131
        $this->assertEquals(
132
            ShortNumberCost::TOLL_FREE,
133
            $this->shortInfo->getExpectedCostForRegion($this->parse($tollFreeExample, RegionCode::FR), RegionCode::FR)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($tollFreeEx...enumber\RegionCode::FR) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
134
        );
135
        $tollFreeNumber = new PhoneNumber();
136
        $tollFreeNumber->setCountryCode(33)->setNationalNumber($tollFreeExample);
137
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($tollFreeNumber));
138
139
        $this->assertEquals(
140
            ShortNumberCost::UNKNOWN_COST,
141
            $this->shortInfo->getExpectedCostForRegion($this->parse("12345", RegionCode::FR), RegionCode::FR)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('12345', \l...enumber\RegionCode::FR) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
142
        );
143
        $unknownCostNumber = new PhoneNumber();
144
        $unknownCostNumber->setCountryCode(33)->setNationalNumber(12345);
145
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));
146
147
        // Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
148
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse("116123", RegionCode::FR), RegionCode::FR));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('116123', \...enumber\RegionCode::FR) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
149
        $this->assertEquals(
150
            ShortNumberCost::TOLL_FREE,
151
            $this->shortInfo->getExpectedCostForRegion($this->parse("116123", RegionCode::FR), RegionCode::FR)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('116123', \...enumber\RegionCode::FR) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
152
        );
153
        $invalidNumber = new PhoneNumber();
154
        $invalidNumber->setCountryCode(33)->setNationalNumber(116123);
155
        $this->assertFalse($this->shortInfo->isValidShortNumber($invalidNumber));
156
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($invalidNumber));
157
158
        // Test a nonexistent country code.
159
        $this->assertEquals(
160
            ShortNumberCost::UNKNOWN_COST,
161
            $this->shortInfo->getExpectedCostForRegion($this->parse("911", RegionCode::US), RegionCode::ZZ)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('911', \lib...enumber\RegionCode::US) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
162
        );
163
        $unknownCostNumber->clear();
164
        $unknownCostNumber->setCountryCode(123)->setNationalNumber(911);
165
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));
166
    }
167
168
    public function testGetExpectedCostForSharedCountryCallingCode()
169
    {
170
        // Test some numbers which have different costs in countries sharing the same country calling
171
        // code. In Australia, 1234 is premium-rate, 1194 is standard-rate, and 733 is toll-free. These
172
        // are not known to be valid numbers in the Christmas Islands.
173
        $ambiguousPremiumRateString = "1234";
174
        $ambiguousPremiumRateNumber = new PhoneNumber();
175
        $ambiguousPremiumRateNumber->setCountryCode(61)->setNationalNumber(1234);
176
        $ambiguousStandardRateString = "1194";
177
        $ambiguousStandardRateNumber = new PhoneNumber();
178
        $ambiguousStandardRateNumber->setCountryCode(61)->setNationalNumber(1194);
179
        $ambiguousTollFreeString = "733";
180
        $ambiguousTollFreeNumber = new PhoneNumber();
181
        $ambiguousTollFreeNumber->setCountryCode(61)->setNationalNumber(733);
182
183
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousPremiumRateNumber));
184
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousStandardRateNumber));
185
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousTollFreeNumber));
186
187
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousPremiumRateString, RegionCode::AU), RegionCode::AU));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousP...enumber\RegionCode::AU) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
188
        $this->assertEquals(
189
            ShortNumberCost::PREMIUM_RATE,
190
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousPremiumRateString, RegionCode::AU), RegionCode::AU)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousP...enumber\RegionCode::AU) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
191
        );
192
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousPremiumRateString, RegionCode::CX), RegionCode::CX));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousP...enumber\RegionCode::CX) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
193
        $this->assertEquals(
194
            ShortNumberCost::UNKNOWN_COST,
195
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousPremiumRateString, RegionCode::CX), RegionCode::CX)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousP...enumber\RegionCode::CX) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
196
        );
197
        // PREMIUM_RATE takes precedence over UNKNOWN_COST.
198
        $this->assertEquals(
199
            ShortNumberCost::PREMIUM_RATE,
200
            $this->shortInfo->getExpectedCost($ambiguousPremiumRateNumber)
201
        );
202
203
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousStandardRateString, RegionCode::AU), RegionCode::AU));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousS...enumber\RegionCode::AU) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
204
        $this->assertEquals(
205
            ShortNumberCost::STANDARD_RATE,
206
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousStandardRateString, RegionCode::AU), RegionCode::AU)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousS...enumber\RegionCode::AU) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
207
        );
208
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousStandardRateString, RegionCode::CX), RegionCode::CX));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousS...enumber\RegionCode::CX) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
209
        $this->assertEquals(
210
            ShortNumberCost::UNKNOWN_COST,
211
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousStandardRateString, RegionCode::CX), RegionCode::CX)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousS...enumber\RegionCode::CX) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
212
        );
213
        $this->assertEquals(
214
            ShortNumberCost::UNKNOWN_COST,
215
            $this->shortInfo->getExpectedCost($ambiguousStandardRateNumber)
216
        );
217
218
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousTollFreeString, RegionCode::AU), RegionCode::AU));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousT...enumber\RegionCode::AU) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
219
        $this->assertEquals(
220
            ShortNumberCost::TOLL_FREE,
221
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousTollFreeString, RegionCode::AU), RegionCode::AU)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousT...enumber\RegionCode::AU) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
222
        );
223
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse($ambiguousTollFreeString, RegionCode::CX), RegionCode::CX));
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousT...enumber\RegionCode::CX) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
224
        $this->assertEquals(
225
            ShortNumberCost::UNKNOWN_COST,
226
            $this->shortInfo->getExpectedCostForRegion($this->parse($ambiguousTollFreeString, RegionCode::CX), RegionCode::CX)
0 ignored issues
show
Bug introduced by
It seems like $this->parse($ambiguousT...enumber\RegionCode::CX) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
227
        );
228
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($ambiguousTollFreeNumber));
229
    }
230
231
    public function testGetExampleShortNumber()
232
    {
233
        $this->assertEquals("8711", $this->shortInfo->getExampleShortNumber(RegionCode::AM));
234
        $this->assertEquals("1010", $this->shortInfo->getExampleShortNumber(RegionCode::FR));
235
        $this->assertEquals("", $this->shortInfo->getExampleShortNumber(RegionCode::UN001));
236
        $this->assertEquals("", $this->shortInfo->getExampleShortNumber(null));
237
    }
238
239
    public function testGetExampleShortNumberForCost()
240
    {
241
        $this->assertEquals(
242
            "3010",
243
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE)
244
        );
245
        $this->assertEquals(
246
            "1023",
247
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::STANDARD_RATE)
248
        );
249
        $this->assertEquals(
250
            "42000",
251
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::PREMIUM_RATE)
252
        );
253
        $this->assertEquals(
254
            "",
255
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::UNKNOWN_COST)
256
        );
257
    }
258
259
    public function testConnectsToEmergencyNumber_US()
260
    {
261
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::US));
262
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("112", RegionCode::US));
263
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::US));
264
    }
265
266
    public function testConnectsToEmergencyNumberLongNumber_US()
267
    {
268
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9116666666", RegionCode::US));
269
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1126666666", RegionCode::US));
270
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996666666", RegionCode::US));
271
    }
272
273
    public function testConnectsToEmergencyNumberWithFormatting_US()
274
    {
275
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9-1-1", RegionCode::US));
276
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1-1-2", RegionCode::US));
277
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9-9-9", RegionCode::US));
278
    }
279
280
    public function testConnectsToEmergencyNumberWithPlusSign_US()
281
    {
282
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+911", RegionCode::US));
283
        $this->assertFalse(
284
            $this->shortInfo->connectsToEmergencyNumber(self::$plusSymbol . "911", RegionCode::US)
285
        );
286
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber(" +911", RegionCode::US));
287
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+112", RegionCode::US));
288
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+999", RegionCode::US));
289
    }
290
291
    public function testConnectsToEmergencyNumber_BR()
292
    {
293
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::BR));
294
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("190", RegionCode::BR));
295
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::BR));
296
    }
297
298
    public function testConnectsToEmergencyNumberLongNumber_BR()
299
    {
300
        // Brazilian emergency numbers don't work when additional digits are appended.
301
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9111", RegionCode::BR));
302
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("1900", RegionCode::BR));
303
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996", RegionCode::BR));
304
    }
305
306
    public function testConnectsToEmergencyNumber_CL()
307
    {
308
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber('131', RegionCode::CL));
309
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber('133', RegionCode::CL));
310
    }
311
312
    public function testConnectsToEmergencyNumberLongNumber_CL()
313
    {
314
        // Chilean emergency numbers don't work when additional digits are appended.
315
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1313', RegionCode::CL));
316
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1330', RegionCode::CL));
317
    }
318
319
    public function testConnectsToEmergencyNumber_AO()
320
    {
321
        // Angola doesn't have any metadata for emergency numbers in the test metadata.
322
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::AO));
323
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("222123456", RegionCode::BR));
324
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("923123456", RegionCode::BR));
325
    }
326
327
    public function testConnectsToEmergencyNumber_ZW()
328
    {
329
        // Zimbabwe doesn't have any metadata in the test metadata.
330
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::ZW));
331
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("01312345", RegionCode::ZW));
332
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("0711234567", RegionCode::ZW));
333
    }
334
335
    public function testIsEmergencyNumber_US()
336
    {
337
        $this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::US));
338
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::US));
339
        $this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::US));
340
    }
341
342
    public function testIsEmergencyNumberLongNumber_US()
343
    {
344
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9116666666", RegionCode::US));
345
        $this->assertFalse($this->shortInfo->isEmergencyNumber("1126666666", RegionCode::US));
346
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9996666666", RegionCode::US));
347
    }
348
349
    public function testIsEmergencyNumberWithFormatting_US()
350
    {
351
        $this->assertTrue($this->shortInfo->isEmergencyNumber("9-1-1", RegionCode::US));
352
        $this->assertTrue($this->shortInfo->isEmergencyNumber("*911", RegionCode::US));
353
        $this->assertTrue($this->shortInfo->isEmergencyNumber("1-1-2", RegionCode::US));
354
        $this->assertTrue($this->shortInfo->isEmergencyNumber("*112", RegionCode::US));
355
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9-9-9", RegionCode::US));
356
        $this->assertFalse($this->shortInfo->isEmergencyNumber("*999", RegionCode::US));
357
    }
358
359
    public function testIsEmergencyNumberWithPlusSign_US()
360
    {
361
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+911", RegionCode::US));
362
        $this->assertFalse($this->shortInfo->isEmergencyNumber(self::$plusSymbol . "911", RegionCode::US));
363
        $this->assertFalse($this->shortInfo->isEmergencyNumber(" +911", RegionCode::US));
364
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+112", RegionCode::US));
365
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+999", RegionCode::US));
366
    }
367
368
    public function testIsEmergencyNumber_BR()
369
    {
370
        $this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::BR));
371
        $this->assertTrue($this->shortInfo->isEmergencyNumber("190", RegionCode::BR));
372
        $this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::BR));
373
    }
374
375
    public function testIsEmergencyNumberLongNumber_BR()
376
    {
377
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9111", RegionCode::BR));
378
        $this->assertFalse($this->shortInfo->isEmergencyNumber("1900", RegionCode::BR));
379
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9996", RegionCode::BR));
380
    }
381
382
    public function testIsEmergencyNumber_AO()
383
    {
384
        // Angola doesn't have any metadata for emergency numbers in the test metadata.
385
        $this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::AO));
386
        $this->assertFalse($this->shortInfo->isEmergencyNumber("222123456", RegionCode::AO));
387
        $this->assertFalse($this->shortInfo->isEmergencyNumber("923123456", RegionCode::AO));
388
    }
389
390
    public function testIsEmergencyNumber_ZW()
391
    {
392
        // Zimbabwe doesn't have any metadata in the test metadata.
393
        $this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::ZW));
394
        $this->assertFalse($this->shortInfo->isEmergencyNumber("01312345", RegionCode::ZW));
395
        $this->assertFalse($this->shortInfo->isEmergencyNumber("0711234567", RegionCode::ZW));
396
    }
397
398
399
    public function testEmergencyNumberForSharedCountryCallingCode()
400
    {
401
        // Test the emergency number 112, which is valid in both Australia and the Christmas Islands.
402
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::AU));
403
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::AU), RegionCode::AU));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('112', \lib...enumber\RegionCode::AU) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
404
        $this->assertEquals(
405
            ShortNumberCost::TOLL_FREE,
406
            $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::AU), RegionCode::AU)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('112', \lib...enumber\RegionCode::AU) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
407
        );
408
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::CX));
409
        $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::CX), RegionCode::CX));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('112', \lib...enumber\RegionCode::CX) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
410
        $this->assertEquals(
411
            ShortNumberCost::TOLL_FREE,
412
            $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::CX), RegionCode::CX)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('112', \lib...enumber\RegionCode::CX) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
413
        );
414
        $sharedEmergencyNumber = new PhoneNumber();
415
        $sharedEmergencyNumber->setCountryCode(61)->setNationalNumber(112);
416
        $this->assertTrue($this->shortInfo->isValidShortNumber($sharedEmergencyNumber));
417
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($sharedEmergencyNumber));
418
    }
419
420
    public function testOverlappingNANPANumber()
421
    {
422
        // 211 is an emergency number in Barbados, while it is a toll-free information line in Canada
423
        // and the USA.
424
        $this->assertTrue($this->shortInfo->isEmergencyNumber("211", RegionCode::BB));
425
        $this->assertEquals(
426
            ShortNumberCost::TOLL_FREE,
427
            $this->shortInfo->getExpectedCostForRegion($this->parse("211", RegionCode::BB), RegionCode::BB)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('211', \lib...enumber\RegionCode::BB) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
428
        );
429
        $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::US));
430
        $this->assertEquals(
431
            ShortNumberCost::UNKNOWN_COST,
432
            $this->shortInfo->getExpectedCostForRegion($this->parse("211", RegionCode::US), RegionCode::US)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('211', \lib...enumber\RegionCode::US) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
433
        );
434
        $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::CA));
435
        $this->assertEquals(
436
            ShortNumberCost::TOLL_FREE,
437
            $this->shortInfo->getExpectedCostForRegion($this->parse("211", RegionCode::CA), RegionCode::CA)
0 ignored issues
show
Bug introduced by
It seems like $this->parse('211', \lib...enumber\RegionCode::CA) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
438
        );
439
    }
440
441
    public function testCountryCallingCodeIsNotIgnored()
442
    {
443
        // +46 is the country calling code for Sweden (SE), and 40404 is a valid short number in the US.
444
        $this->assertFalse($this->shortInfo->isPossibleShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('+4640404',...enumber\RegionCode::SE) can be null; however, isPossibleShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
445
        $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('+4640404',...enumber\RegionCode::SE) can be null; however, isValidShortNumberForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
446
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCostForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
0 ignored issues
show
Bug introduced by
It seems like $this->parse('+4640404',...enumber\RegionCode::SE) can be null; however, getExpectedCostForRegion() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
447
    }
448
449
    /**
450
     * @param string $number
451
     * @param string $regionCode
452
     * @return PhoneNumber
453
     */
454
    private function parse($number, $regionCode)
455
    {
456
        try {
457
            return $this->phoneUtil->parse($number, $regionCode);
458
        } catch (NumberParseException $e) {
459
            $this->fail("Test input data should always parse correctly: " . $number . " (" . $regionCode . ")");
460
        }
461
    }
462
}
463