Completed
Push — 8.1.0-changes ( 082493 )
by Joshua
11:12
created

ShortNumberInfoTest::testIsCarrierSpecific()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 14
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 testGetExpectedCost()
95
    {
96
        $premiumRateExample = $this->shortInfo->getExampleShortNumberForCost(
97
            RegionCode::FR,
98
            ShortNumberCost::PREMIUM_RATE
99
        );
100
        $this->assertEquals(
101
            ShortNumberCost::PREMIUM_RATE,
102
            $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...
103
        );
104
105
        $premiumRateNumber = new PhoneNumber();
106
        $premiumRateNumber->setCountryCode(33)->setNationalNumber($premiumRateExample);
107
        $this->assertEquals(ShortNumberCost::PREMIUM_RATE, $this->shortInfo->getExpectedCost($premiumRateNumber));
108
109
        $standardRateExample = $this->shortInfo->getExampleShortNumberForCost(
110
            RegionCode::FR,
111
            ShortNumberCost::STANDARD_RATE
112
        );
113
        $this->assertEquals(
114
            ShortNumberCost::STANDARD_RATE,
115
            $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...
116
        );
117
118
        $standardRateNumber = new PhoneNumber();
119
        $standardRateNumber->setCountryCode(33)->setNationalNumber($standardRateExample);
120
        $this->assertEquals(ShortNumberCost::STANDARD_RATE, $this->shortInfo->getExpectedCost($standardRateNumber));
121
122
        $tollFreeExample = $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE);
123
        $this->assertEquals(
124
            ShortNumberCost::TOLL_FREE,
125
            $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...
126
        );
127
        $tollFreeNumber = new PhoneNumber();
128
        $tollFreeNumber->setCountryCode(33)->setNationalNumber($tollFreeExample);
129
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($tollFreeNumber));
130
131
        $this->assertEquals(
132
            ShortNumberCost::UNKNOWN_COST,
133
            $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...
134
        );
135
        $unknownCostNumber = new PhoneNumber();
136
        $unknownCostNumber->setCountryCode(33)->setNationalNumber(12345);
137
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));
138
139
        // Test that an invalid number may nevertheless have a cost other than UNKNOWN_COST.
140
        $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...
141
        $this->assertEquals(
142
            ShortNumberCost::TOLL_FREE,
143
            $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...
144
        );
145
        $invalidNumber = new PhoneNumber();
146
        $invalidNumber->setCountryCode(33)->setNationalNumber(116123);
147
        $this->assertFalse($this->shortInfo->isValidShortNumber($invalidNumber));
148
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($invalidNumber));
149
150
        // Test a nonexistent country code.
151
        $this->assertEquals(
152
            ShortNumberCost::UNKNOWN_COST,
153
            $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...
154
        );
155
        $unknownCostNumber->clear();
156
        $unknownCostNumber->setCountryCode(123)->setNationalNumber(911);
157
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($unknownCostNumber));
158
    }
159
160
    public function testGetExpectedCostForSharedCountryCallingCode()
161
    {
162
        // Test some numbers which have different costs in countries sharing the same country calling
163
        // code. In Australia, 1234 is premium-rate, 1194 is standard-rate, and 733 is toll-free. These
164
        // are not known to be valid numbers in the Christmas Islands.
165
        $ambiguousPremiumRateString = "1234";
166
        $ambiguousPremiumRateNumber = new PhoneNumber();
167
        $ambiguousPremiumRateNumber->setCountryCode(61)->setNationalNumber(1234);
168
        $ambiguousStandardRateString = "1194";
169
        $ambiguousStandardRateNumber = new PhoneNumber();
170
        $ambiguousStandardRateNumber->setCountryCode(61)->setNationalNumber(1194);
171
        $ambiguousTollFreeString = "733";
172
        $ambiguousTollFreeNumber = new PhoneNumber();
173
        $ambiguousTollFreeNumber->setCountryCode(61)->setNationalNumber(733);
174
175
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousPremiumRateNumber));
176
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousStandardRateNumber));
177
        $this->assertTrue($this->shortInfo->isValidShortNumber($ambiguousTollFreeNumber));
178
179
        $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...
180
        $this->assertEquals(
181
            ShortNumberCost::PREMIUM_RATE,
182
            $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...
183
        );
184
        $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...
185
        $this->assertEquals(
186
            ShortNumberCost::UNKNOWN_COST,
187
            $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...
188
        );
189
        // PREMIUM_RATE takes precedence over UNKNOWN_COST.
190
        $this->assertEquals(
191
            ShortNumberCost::PREMIUM_RATE,
192
            $this->shortInfo->getExpectedCost($ambiguousPremiumRateNumber)
193
        );
194
195
        $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...
196
        $this->assertEquals(
197
            ShortNumberCost::STANDARD_RATE,
198
            $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...
199
        );
200
        $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...
201
        $this->assertEquals(
202
            ShortNumberCost::UNKNOWN_COST,
203
            $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...
204
        );
205
        $this->assertEquals(
206
            ShortNumberCost::UNKNOWN_COST,
207
            $this->shortInfo->getExpectedCost($ambiguousStandardRateNumber)
208
        );
209
210
        $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...
211
        $this->assertEquals(
212
            ShortNumberCost::TOLL_FREE,
213
            $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...
214
        );
215
        $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...
216
        $this->assertEquals(
217
            ShortNumberCost::UNKNOWN_COST,
218
            $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...
219
        );
220
        $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCost($ambiguousTollFreeNumber));
221
    }
222
223
    public function testGetExampleShortNumber()
224
    {
225
        $this->assertEquals("8711", $this->shortInfo->getExampleShortNumber(RegionCode::AM));
226
        $this->assertEquals("1010", $this->shortInfo->getExampleShortNumber(RegionCode::FR));
227
        $this->assertEquals("", $this->shortInfo->getExampleShortNumber(RegionCode::UN001));
228
        $this->assertEquals("", $this->shortInfo->getExampleShortNumber(null));
229
    }
230
231
    public function testGetExampleShortNumberForCost()
232
    {
233
        $this->assertEquals(
234
            "3010",
235
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::TOLL_FREE)
236
        );
237
        $this->assertEquals(
238
            "1023",
239
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::STANDARD_RATE)
240
        );
241
        $this->assertEquals(
242
            "42000",
243
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::PREMIUM_RATE)
244
        );
245
        $this->assertEquals(
246
            "",
247
            $this->shortInfo->getExampleShortNumberForCost(RegionCode::FR, ShortNumberCost::UNKNOWN_COST)
248
        );
249
    }
250
251
    public function testConnectsToEmergencyNumber_US()
252
    {
253
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::US));
254
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("112", RegionCode::US));
255
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::US));
256
    }
257
258
    public function testConnectsToEmergencyNumberLongNumber_US()
259
    {
260
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9116666666", RegionCode::US));
261
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1126666666", RegionCode::US));
262
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996666666", RegionCode::US));
263
    }
264
265
    public function testConnectsToEmergencyNumberWithFormatting_US()
266
    {
267
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("9-1-1", RegionCode::US));
268
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("1-1-2", RegionCode::US));
269
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9-9-9", RegionCode::US));
270
    }
271
272
    public function testConnectsToEmergencyNumberWithPlusSign_US()
273
    {
274
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+911", RegionCode::US));
275
        $this->assertFalse(
276
            $this->shortInfo->connectsToEmergencyNumber(self::$plusSymbol . "911", RegionCode::US)
277
        );
278
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber(" +911", RegionCode::US));
279
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+112", RegionCode::US));
280
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("+999", RegionCode::US));
281
    }
282
283
    public function testConnectsToEmergencyNumber_BR()
284
    {
285
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::BR));
286
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber("190", RegionCode::BR));
287
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("999", RegionCode::BR));
288
    }
289
290
    public function testConnectsToEmergencyNumberLongNumber_BR()
291
    {
292
        // Brazilian emergency numbers don't work when additional digits are appended.
293
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9111", RegionCode::BR));
294
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("1900", RegionCode::BR));
295
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("9996", RegionCode::BR));
296
    }
297
298
    public function testConnectsToEmergencyNumber_CL()
299
    {
300
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber('131', RegionCode::CL));
301
        $this->assertTrue($this->shortInfo->connectsToEmergencyNumber('133', RegionCode::CL));
302
    }
303
304
    public function testConnectsToEmergencyNumberLongNumber_CL()
305
    {
306
        // Chilean emergency numbers don't work when additional digits are appended.
307
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1313', RegionCode::CL));
308
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber('1330', RegionCode::CL));
309
    }
310
311
    public function testConnectsToEmergencyNumber_AO()
312
    {
313
        // Angola doesn't have any metadata for emergency numbers in the test metadata.
314
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::AO));
315
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("222123456", RegionCode::BR));
316
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("923123456", RegionCode::BR));
317
    }
318
319
    public function testConnectsToEmergencyNumber_ZW()
320
    {
321
        // Zimbabwe doesn't have any metadata in the test metadata.
322
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("911", RegionCode::ZW));
323
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("01312345", RegionCode::ZW));
324
        $this->assertFalse($this->shortInfo->connectsToEmergencyNumber("0711234567", RegionCode::ZW));
325
    }
326
327
    public function testIsEmergencyNumber_US()
328
    {
329
        $this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::US));
330
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::US));
331
        $this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::US));
332
    }
333
334
    public function testIsEmergencyNumberLongNumber_US()
335
    {
336
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9116666666", RegionCode::US));
337
        $this->assertFalse($this->shortInfo->isEmergencyNumber("1126666666", RegionCode::US));
338
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9996666666", RegionCode::US));
339
    }
340
341
    public function testIsEmergencyNumberWithFormatting_US()
342
    {
343
        $this->assertTrue($this->shortInfo->isEmergencyNumber("9-1-1", RegionCode::US));
344
        $this->assertTrue($this->shortInfo->isEmergencyNumber("*911", RegionCode::US));
345
        $this->assertTrue($this->shortInfo->isEmergencyNumber("1-1-2", RegionCode::US));
346
        $this->assertTrue($this->shortInfo->isEmergencyNumber("*112", RegionCode::US));
347
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9-9-9", RegionCode::US));
348
        $this->assertFalse($this->shortInfo->isEmergencyNumber("*999", RegionCode::US));
349
    }
350
351
    public function testIsEmergencyNumberWithPlusSign_US()
352
    {
353
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+911", RegionCode::US));
354
        $this->assertFalse($this->shortInfo->isEmergencyNumber(self::$plusSymbol . "911", RegionCode::US));
355
        $this->assertFalse($this->shortInfo->isEmergencyNumber(" +911", RegionCode::US));
356
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+112", RegionCode::US));
357
        $this->assertFalse($this->shortInfo->isEmergencyNumber("+999", RegionCode::US));
358
    }
359
360
    public function testIsEmergencyNumber_BR()
361
    {
362
        $this->assertTrue($this->shortInfo->isEmergencyNumber("911", RegionCode::BR));
363
        $this->assertTrue($this->shortInfo->isEmergencyNumber("190", RegionCode::BR));
364
        $this->assertFalse($this->shortInfo->isEmergencyNumber("999", RegionCode::BR));
365
    }
366
367
    public function testIsEmergencyNumberLongNumber_BR()
368
    {
369
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9111", RegionCode::BR));
370
        $this->assertFalse($this->shortInfo->isEmergencyNumber("1900", RegionCode::BR));
371
        $this->assertFalse($this->shortInfo->isEmergencyNumber("9996", RegionCode::BR));
372
    }
373
374
    public function testIsEmergencyNumber_AO()
375
    {
376
        // Angola doesn't have any metadata for emergency numbers in the test metadata.
377
        $this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::AO));
378
        $this->assertFalse($this->shortInfo->isEmergencyNumber("222123456", RegionCode::AO));
379
        $this->assertFalse($this->shortInfo->isEmergencyNumber("923123456", RegionCode::AO));
380
    }
381
382
    public function testIsEmergencyNumber_ZW()
383
    {
384
        // Zimbabwe doesn't have any metadata in the test metadata.
385
        $this->assertFalse($this->shortInfo->isEmergencyNumber("911", RegionCode::ZW));
386
        $this->assertFalse($this->shortInfo->isEmergencyNumber("01312345", RegionCode::ZW));
387
        $this->assertFalse($this->shortInfo->isEmergencyNumber("0711234567", RegionCode::ZW));
388
    }
389
390
391
    public function testEmergencyNumberForSharedCountryCallingCode()
392
    {
393
        // Test the emergency number 112, which is valid in both Australia and the Christmas Islands.
394
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::AU));
395
        $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...
396
        $this->assertEquals(
397
            ShortNumberCost::TOLL_FREE,
398
            $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...
399
        );
400
        $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::CX));
401
        $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...
402
        $this->assertEquals(
403
            ShortNumberCost::TOLL_FREE,
404
            $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...
405
        );
406
        $sharedEmergencyNumber = new PhoneNumber();
407
        $sharedEmergencyNumber->setCountryCode(61)->setNationalNumber(112);
408
        $this->assertTrue($this->shortInfo->isValidShortNumber($sharedEmergencyNumber));
409
        $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($sharedEmergencyNumber));
410
    }
411
412
    public function testOverlappingNANPANumber()
413
    {
414
        // 211 is an emergency number in Barbados, while it is a toll-free information line in Canada
415
        // and the USA.
416
        $this->assertTrue($this->shortInfo->isEmergencyNumber("211", RegionCode::BB));
417
        $this->assertEquals(
418
            ShortNumberCost::TOLL_FREE,
419
            $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...
420
        );
421
        $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::US));
422
        $this->assertEquals(
423
            ShortNumberCost::UNKNOWN_COST,
424
            $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...
425
        );
426
        $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::CA));
427
        $this->assertEquals(
428
            ShortNumberCost::TOLL_FREE,
429
            $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...
430
        );
431
    }
432
433
    public function testCountryCallingCodeIsNotIgnored()
434
    {
435
        // +46 is the country calling code for Sweden (SE), and 40404 is a valid short number in the US.
436
        $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...
437
        $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...
438
        $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...
439
    }
440
441
    /**
442
     * @param string $number
443
     * @param string $regionCode
444
     * @return PhoneNumber
445
     */
446
    private function parse($number, $regionCode)
447
    {
448
        try {
449
            return $this->phoneUtil->parse($number, $regionCode);
450
        } catch (NumberParseException $e) {
451
            $this->fail("Test input data should always parse correctly: " . $number . " (" . $regionCode . ")");
452
        }
453
    }
454
}
455