Completed
Push — master ( 1e6744...d2c602 )
by Nicolaas
01:21
created

PhoneField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 3
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B TellLink() 0 24 2
A CallToLink() 0 4 1
A scaffoldFormField() 0 8 2
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
     * This method is accessed by other pages!
29
     *
30
     * @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...
31
     *
32
     * @return string
33
     */
34
    public function TellLink($countryCode = "")
35
    {
36
        //remove non digits
37
        if (!$countryCode) {
38
            $countryCode = $this->Config()->default_country_code;
39
        }
40
        $phoneNumber = preg_replace('/\D/', '', $this->value);
41
42
        //hack the 1300 scenario
43
        //if (substr($phoneNumber, 0, 4) == '1300') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
44
        //    $phoneNumber = preg_replace('/^1300/', '+611300', $phoneNumber);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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
        //} else {
46
            //remove leading zero
47
            $phoneNumber = preg_replace('/^0/', '+'.$countryCode, $phoneNumber);
48
        //}
49
        //remove double-ups
50
        $phoneNumber = str_replace(
51
            '+'.$countryCode.''.$countryCode,
52
            '+'.$countryCode,
53
            $phoneNumber
54
        );
55
        $phoneNumber = "tel:+".$phoneNumber;
56
        return $phoneNumber;
57
    }
58
59
60
61
    /**
62
     *
63
     * @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...
64
     *
65
     * @return string
66
     */
67
    public function CallToLink($countryCode = "")
68
    {
69
        return str_replace('tel:', 'callto:', $this->TellLink($countryCode));
70
    }
71
    
72
    /**
73
     * @see DBField::scaffoldFormField()
74
     *
75
     * @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...
76
     * @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...
77
     *
78
     * @return PhoneNumberField | NullableField
79
     */
80
    public function scaffoldFormField($title = null, $params = null)
81
    {
82
        if (!$this->nullifyEmpty) {
83
            return NullableField::create(PhoneNumberField::create($this->name, $title));
84
        } else {
85
            return PhoneNumberField::create($this->name, $title);
86
        }
87
    }
88
    
89
}
90