Completed
Push — master ( e17d9a...f8eac8 )
by Austin
01:42
created

Contact   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 169
Duplicated Lines 14.2 %

Importance

Changes 0
Metric Value
dl 24
loc 169
rs 10
c 0
b 0
f 0
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addContact() 0 3 1
A hasContacts() 0 9 3
A hasContact() 0 3 1
A validContacts() 0 13 4
A setContacts() 0 9 2
A getContacts() 0 3 2
A validContact() 0 4 2
A removeContacts() 11 11 3
A removeContact() 0 5 1
A addContacts() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * src/Directives/Contact.php
4
 *
5
 * @package     php-security-txt
6
 * @author      Austin Heap <[email protected]>
7
 * @version     v0.4.0
8
 */
9
10
declare(strict_types = 1);
11
12
namespace AustinHeap\Security\Txt\Directives;
13
14
use AustinHeap\Security\Txt\SecurityTxt;
15
use Exception;
16
17
/**
18
 * Contact
19
 *
20
 * @link        https://github.com/austinheap/php-security-txt
21
 * @link        https://packagist.org/packages/austinheap/php-security-txt
22
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.SecurityTxt.html
23
 */
24
trait Contact
25
{
26
27
    /**
28
     * The security contact(s).
29
     *
30
     * @var array
31
     */
32
    protected $contacts = [];
33
34
    /**
35
     * Set the contacts.
36
     *
37
     * @param  array $contacts
38
     *
39
     * @return SecurityTxt
40
     */
41
    public function setContacts(array $contacts): SecurityTxt
42
    {
43
        if (!$this->validContacts($contacts, true)) {
44
            throw new Exception('Contacts array must contain well-formed e-mails and/or URLs.');
45
        }
46
47
        $this->contacts = $contacts;
48
49
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
50
    }
51
52
    /**
53
     * Get the contacts.
54
     *
55
     * @return array
56
     */
57
    public function getContacts(): array
58
    {
59
        return is_null($this->contacts) ? [] : array_keys($this->contacts);
60
    }
61
62
    /**
63
     * Add a contact.
64
     *
65
     * @param  string $contact
66
     *
67
     * @return SecurityTxt
68
     */
69
    public function addContact(string $contact): SecurityTxt
70
    {
71
        return $this->addContacts([$contact]);
72
    }
73
74
    /**
75
     * Add contacts.
76
     *
77
     * @param  array $contacts
78
     *
79
     * @return SecurityTxt
80
     */
81 View Code Duplication
    public function addContacts(array $contacts): SecurityTxt
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        if (!$this->validContacts($contacts)) {
84
            throw new Exception('Contacts must be well-formed e-mails and/or URLs.');
85
        }
86
87
        foreach ($contacts as $contact) {
88
            $this->contacts[$contact] = true;
89
        }
90
91
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
92
    }
93
94
    /**
95
     * Validates a contact.
96
     *
97
     * @param string $contact
98
     *
99
     * @string string $contact
100
     * @return bool
101
     */
102
    public function validContact(string $contact): bool
103
    {
104
        return filter_var($contact, FILTER_VALIDATE_EMAIL) !== false ||
105
               filter_var($contact, FILTER_VALIDATE_URL) !== false;
106
    }
107
108
    /**
109
     * Validates an array of contacts.
110
     *
111
     * @param array $contacts
112
     * @param bool  $use_keys
113
     *
114
     * @return bool
115
     */
116
    public function validContacts(array $contacts, bool $use_keys = false): bool
117
    {
118
        if ($use_keys) {
119
            $contacts = array_keys($contacts);
120
        }
121
122
        foreach ($contacts as $contact) {
123
            if (!$this->validContact($contact)) {
124
                return false;
125
            }
126
        }
127
128
        return true;
129
    }
130
131
    /**
132
     * Remove a contact.
133
     *
134
     * @param  string $contact
135
     *
136
     * @return SecurityTxt
137
     */
138
    public function removeContact(string $contact): SecurityTxt
139
    {
140
        $this->removeContacts([$contact]);
141
142
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
143
    }
144
145
    /**
146
     * Remove contacts.
147
     *
148
     * @param  array $contacts
149
     *
150
     * @return SecurityTxt
151
     */
152 View Code Duplication
    public function removeContacts(array $contacts): SecurityTxt
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    {
154
        if (!$this->hasContacts($contacts)) {
155
            throw new Exception('Cannot remove contacts that do not exist.');
156
        }
157
158
        foreach ($contacts as $contact) {
159
            unset($this->contacts[$contact]);
160
        }
161
162
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AustinHeap\Security\Txt\Directives\Contact which includes types incompatible with the type-hinted return AustinHeap\Security\Txt\SecurityTxt.
Loading history...
163
    }
164
165
    /**
166
     * Determines if a contact exists.
167
     *
168
     * @param string $contact
169
     *
170
     * @return bool
171
     */
172
    public function hasContact(string $contact): bool
173
    {
174
        return array_key_exists($contact, $this->contacts);
175
    }
176
177
    /**
178
     * Determines if an array of contacts exists.
179
     *
180
     * @param array $contacts
181
     *
182
     * @return bool
183
     */
184
    public function hasContacts(array $contacts): bool
185
    {
186
        foreach ($contacts as $contact) {
187
            if (!$this->hasContact($contact)) {
188
                return false;
189
            }
190
        }
191
192
        return true;
193
    }
194
195
}
196