StaffProfile::EncodedEmailText()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
/**
4
 *@author: nicolaas[at]sunnysideup.co.nz
5
 *@description: individual staff profile
6
 *
7
 **/
8
9
class StaffProfile extends DataObject
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...
10
{
11
    private static $db = 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 $db 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...
12
        "Name" => "Varchar(255)",
13
        "Position" => "Varchar(255)",
14
        "Description" => "Text",
15
        "Email" => "Varchar(255)",
16
        "SubjectLine" => "Varchar(255)",
17
        "Sort" => "Int"
18
    );
19
20
    private static $has_one = 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 $has_one 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
        "ProfilePicture" => "Image",
22
        "Parent" => "StaffProfilesPage"
23
    );
24
25
    //database related settings
26
    private static $indexes = 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 $indexes 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...
27
        "Sort" => true
28
    );
29
30
    //formatting
31
    private static $searchable_fields = array("Name" => "PartialMatchFilter");
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 $searchable_fields 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...
32
33
    private static $field_labels = 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 $field_labels 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...
34
        "SortNumber" => "Sort Index Number for Sorting (lower numbers first)",
35
        "Subjectline" => "Optional Subject Line"
36
    );
37
38
    private static $summary_fields = array("Name" => "Name", "Email" => "Email", "Title" => "Title");
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 $summary_fields 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...
39
40
    private static $singular_name = "Staff Profile";
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 $singular_name 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...
41
42
    private static $plural_name = "Staff Profiles";
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 $plural_name 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...
43
44
    private static $default_sort = "Sort ASC, Name ASC";
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 $default_sort 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...
45
46
    private static $defaults = 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 $defaults 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...
47
        "Sort" => 100
48
    );
49
50
    /**
51
     * replacement placeholders
52
     * [xxx] => yyy
53
     * where xxx is the string the CMS user types
54
     * and yyy the replacement field / relation.
55
     *
56
     * @var array
57
     */
58
    private static $subject_place_holders = array(
0 ignored issues
show
Unused Code introduced by
The property $subject_place_holders 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...
59
        "Name" => "Name",
60
        "Email" => "Email",
61
        "Position" => "Position",
62
        "PageTitle" => "Parent.Title",
63
        "PageLink" => "Parent.Link"
64
    );
65
66
    public function populateDefaults()
67
    {
68
        $this->Sort = 100;
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<StaffProfile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
        parent::populateDefaults();
70
    }
71
72
    public function getCMSFields()
73
    {
74
        $fields = parent::getCMSFields();
75
        $field = $fields->dataFieldByName("SubjectLine");
76
        $field->setRightTitle(
77
            _t("StaffProfile.PLACEHOLDER_EXPLANATION", "you can use the following placeholders")
78
            . ": ["
79
            . implode("], [", array_keys($this->Config()->get("subject_place_holders")))
80
            . "]"
81
        );
82
        if (class_exists("DataObjectSorterController") && $this->hasExtension("DataObjectSorterController")) {
83
            $fields->addFieldToTab("Root.Sort", new LiteralField("InvitationToSort", $this->dataObjectSorterPopupLink()));
0 ignored issues
show
Documentation Bug introduced by
The method dataObjectSorterPopupLink does not exist on object<StaffProfile>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
            $fields->removeFieldFromTab("Root.Main", "Sort");
85
        }
86
        return $fields;
87
    }
88
89
    public function fieldLabels($includeRelations = true)
90
    {
91
        $labels = parent::fieldLabels($includeRelations);
92
        return $labels;
93
    }
94
95
    /**
96
     * Obscure all email links in StringField.
97
     * Matches mailto:[email protected] as well as [email protected]
98
     *
99
     * @return string | Null
100
     */
101
    public function EncodedEmailLink()
102
    {
103
        if ($email = $this->getBestEmail()) {
104
            $obj = $this->retrieveEmailObject();
105
            if ($obj) {
106
                return $obj->MailTo;
107
            } else {
108
                return "mailto:".$email;
109
            }
110
        }
111
    }
112
113
    /**
114
     * Obscure all email links in StringField.
115
     * Matches mailto:[email protected] as well as [email protected]
116
     *
117
     * @return string
118
     */
119
    public function EncodedEmailText()
120
    {
121
        if ($email = $this->getBestEmail()) {
122
            $obj = $this->retrieveEmailObject();
123
            if ($obj) {
124
                return $obj->Text;
125
            } else {
126
                return $email;
127
            }
128
        }
129
    }
130
131
    public function onBeforeWrite()
132
    {
133
        parent::onBeforeWrite();
134
        if (!$this->Sort) {
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<StaffProfile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
            $this->Sort = 100;
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<StaffProfile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
136
        }
137
        if (!$this->ParentID) {
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<StaffProfile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
            $page = StaffProfilesPage::get()->First();
139
            $this->ParentID = $page->ID;
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<StaffProfile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
140
        }
141
    }
142
143
    /**
144
     * puts together a subject line with replacements
145
     *
146
     * @return String
147
     */
148
    protected function SubjectLineCreator()
149
    {
150
        if ($this->SubjectLine) {
0 ignored issues
show
Documentation introduced by
The property SubjectLine does not exist on object<StaffProfile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
            $str = $this->SubjectLine;
0 ignored issues
show
Documentation introduced by
The property SubjectLine does not exist on object<StaffProfile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
        } else {
153
            $str = $this->Parent()->SubjectLine;
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on StaffProfile. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
154
        }
155
        if (!$str) {
156
            $str =  "Enquiry from [PageLink] for [Name]";
157
        }
158
        $replace = $this->Config()->get("subject_place_holders");
159
        foreach ($replace as $findKey => $replaceField) {
160
            if (strpos($str, $findKey) !== null) {
161
                if (strpos($replaceField, ".")) {
162
                    $replaceFieldParts = explode(".", $replaceField);
163
                    $method1 = $replaceFieldParts[0];
164
                    $method2 = $replaceFieldParts[1];
165
                    $relationalObject = $this->$method1();
166
                    if ($relationalObject) {
167
                        if ($relationalObject->hasMethod($method2)) {
168
                            $replaceValue = $relationalObject->$method2();
169
                        } elseif ($relationalObject->hasMethod("get".$method2)) {
170
                            $method2 = "get".$method2;
171
                            $replaceValue = $relationalObject->$method2();
172
                        }
173
                    }
174
                } else {
175
                    $replaceValue = $this->$replaceField;
176
                }
177
                $str = str_ireplace("[".$findKey."]", $replaceValue, $str);
0 ignored issues
show
Bug introduced by
The variable $replaceValue does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
178
            }
179
        }
180
        return $str;
181
    }
182
183
184
    /**
185
     * @var EmailObject
186
     */
187
    protected $emailObject = null;
188
189
    /**
190
     *
191
     * @return EmailObject | NULL
192
     */
193
    protected function retrieveEmailObject()
194
    {
195
        if (!$this->emailObject) {
196
            if (class_exists("HideMailto")) {
197
                if ($email = $this->getBestEmail()) {
198
                    $this->emailObject = HideMailto::convert_email($email, $this->SubjectLineCreator());
0 ignored issues
show
Documentation Bug introduced by
It seems like \HideMailto::convert_ema...->SubjectLineCreator()) of type object<ViewableData> is incompatible with the declared type object<EmailObject> of property $emailObject.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
199
                }
200
            } else {
201
                user_error("This module requires Sunnysideup/hidemailto, but it can function without it", E_USER_NOTICE);
202
            }
203
        }
204
        return $this->emailObject;
205
    }
206
207
    /**
208
     * finds the best email available.
209
     *
210
     * @return String
211
     */
212
    protected function getBestEmail()
213
    {
214
        if ($this->Email) {
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<StaffProfile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
215
            $email = $this->Email;
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<StaffProfile>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
216
        } else {
217
            $email = $this->Parent()->DefaultEmail;
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on StaffProfile. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
218
        }
219
        return $email;
220
    }
221
}
222