Completed
Push — master ( 19e213...d2cbe0 )
by
unknown
01:38
created

PhoneField::IntlFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * you can now use the following in your silverstripe templates
4
 * $MyPhoneField.TellLink
5
 * which then removes the first 0
6
 * adds country code at the end
7
 * and adds + and country code
8
 *
9
 * e.g
10
 * 09 5556789
11
 * becomes
12
 * tel:+649555789
13
 *
14
 * if you would like a different country code then use:
15
 * $MyPhoneField.TellLink(55)
16
 */
17
18
class PhoneField extends Varchar
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
    private static $default_country_code = "64";
0 ignored issues
show
Unused Code introduced by
The property $default_country_code is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
        "TellLink" => "Varchar",
24
        "CallToLink" => "Varchar"
25
    );
26
27
28
    /**
29
     * This method is accessed by other pages!
30
     *
31
     * @param int $countryCode (e.g. 64)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $countryCode not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
32
     *
33
     * @return string
34
     */
35
    public function IntlFormat($countryCode = "")
36
    {
37
        //remove non digits
38
        if (!$countryCode) {
39
            $countryCode = $this->Config()->default_country_code;
40
        }
41
        //remove non-digits
42
        $phoneNumber = preg_replace('/\D/', '', $this->value);
43
        //remove country code with plus - NOT NECESSARY
44
        //$phoneNumber = $this->literalLeftTrim($phoneNumber, '+'.$countryCode);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
        //remove country code
46
        $phoneNumber = $this->literalLeftTrim($phoneNumber, $countryCode);
0 ignored issues
show
Bug introduced by
It seems like $phoneNumber can also be of type array<integer,string>; however, PhoneField::literalLeftTrim() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
        //remove leading zero
48
        $phoneNumber = $this->literalLeftTrim($phoneNumber, '0');
49
50
        return '+'.$countryCode.$phoneNumber;
51
    }
52
53
    /**
54
     * This method is accessed by other pages!
55
     *
56
     * @param int $countryCode (e.g. 64)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $countryCode not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
     *
58
     * @return string
59
     */
60
    public function TellLink($countryCode = "")
61
    {
62
        return 'tel:'.$this->IntlFormat($countryCode);
63
    }
64
65
66
67
    /**
68
     *
69
     * @param countryCode $countryCode (e.g. 64)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $countryCode not be string|countryCode?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     *
71
     * @return string
72
     */
73
    public function CallToLink($countryCode = "")
74
    {
75
        return 'callto:'.$this->IntlFormat($countryCode);
76
    }
77
78
    /**
79
     * @see DBField::scaffoldFormField()
80
     *
81
     * @param string $title (optional)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $title not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
82
     * @param array $params (optional)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
83
     *
84
     * @return PhoneNumberField | NullableField
0 ignored issues
show
Documentation introduced by
Should the return type not be NullableField|TextField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86
    public function scaffoldFormField($title = null, $params = null)
87
    {
88
        if (!$this->nullifyEmpty) {
89
            return NullableField::create(TextField::create($this->name, $title));
90
        } else {
91
            return TextField::create($this->name, $title);
92
        }
93
    }
94
95
    /**
96
     * removes a string at the start of a string, if present...
97
     * @param string $str - the haystack
98
     * @param string $prefix - the needle
99
     *
100
     * @return string
101
     */
102
    protected function literalLeftTrim($str, $prefix)
103
    {
104
        if (substr($str, 0, strlen($prefix)) == $prefix) {
105
            $str = substr($str, strlen($prefix));
106
        }
107
        return $str;
108
    }
109
}
110