SecurityTxt::setComments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * src/SecurityTxt.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;
13
14
use AustinHeap\Security\Txt\Directives\Acknowledgement;
15
use AustinHeap\Security\Txt\Directives\Contact;
16
use AustinHeap\Security\Txt\Directives\Disclosure;
17
use AustinHeap\Security\Txt\Directives\Encryption;
18
use Exception;
19
20
/**
21
 * SecurityTxt
22
 *
23
 * @link        https://github.com/austinheap/php-security-txt
24
 * @link        https://packagist.org/packages/austinheap/php-security-txt
25
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.SecurityTxt.html
26
 * @link        https://securitytext.org/
27
 */
28
class SecurityTxt implements SecurityTxtInterface
29
{
30
    /**
31
     * Directive trait: Contact
32
     */
33
    use Contact;
34
35
    /**
36
     * Directive trait: Encryption
37
     */
38
    use Encryption;
39
40
    /**
41
     * Directive trait: Disclosure
42
     */
43
    use Disclosure;
44
45
    /**
46
     * Directive trait: Acknowledgement
47
     */
48
    use Acknowledgement;
49
50
    /**
51
     * Internal version number.
52
     *
53
     * @var string
54
     */
55
    const VERSION = '0.4.0';
56
57
    /**
58
     * Internal parent object.
59
     *
60
     * @var \AustinHeap\Security\Txt\Writer|\AustinHeap\Security\Txt\Reader
61
     */
62
    private $parent = null;
63
64
    /**
65
     * Internal Writer object.
66
     *
67
     * @var \AustinHeap\Security\Txt\Writer
68
     */
69
    protected $writer = null;
70
71
    /**
72
     * Internal Reader object.
73
     *
74
     * @var \AustinHeap\Security\Txt\Reader
75
     */
76
    protected $reader = null;
77
78
    /**
79
     * Internal text cache.
80
     *
81
     * @var string
82
     */
83
    protected $text = null;
84
85
    /**
86
     * Enable debug output.
87
     *
88
     * @var bool
89
     */
90
    protected $debug = false;
91
92
    /**
93
     * Enable built-in comments.
94
     *
95
     * @var bool
96
     */
97
    protected $comments = true;
98
99
    /**
100
     * Create a new SecurityTxt instance.
101
     *
102
     * @param  Writer|Reader $parent
103
     *
104
     * @return SecurityTxt|Writer|Reader
105
     */
106 46
    public function __construct(&$parent = null)
107
    {
108 46
        if (!defined('PHP_SECURITY_TXT_VERSION')) {
109 1
            define('PHP_SECURITY_TXT_VERSION', self::VERSION);
110
        }
111
112 46
        $this->parent = $parent;
113
114 46
        if (func_num_args() == 1) {
115 41
            if (is_null($this->parent)) {
116 1
                throw new Exception('Cannot create ' . __CLASS__ . ' with explicitly null $parent class.');
117 40
            } elseif (!$this->parent instanceof Reader && !$this->parent instanceof Writer) {
0 ignored issues
show
introduced by
The condition ! $this->parent instance...eap\Security\Txt\Writer can never be true.
Loading history...
118 1
                throw new Exception('Cannot create ' . __CLASS__ . ' with $parent class: ' . get_class($this->parent));
119
            }
120
        }
121
122 44
        return $this;
123
    }
124
125
    /**
126
     * Returns the parent's class if it exists.
127
     *
128
     * @return string
129
     */
130 4
    public function getParentClass(): string
131
    {
132 4
        return get_class($this->getParent());
133
    }
134
135
    /**
136
     * Returns the parent object if it exists.
137
     *
138
     * @return Reader|Writer
139
     */
140 4
    public function getParent()
141
    {
142 4
        if (!$this->hasParent()) {
143 2
            throw new Exception('Parent object is not set.');
144
        }
145
146 2
        return $this->parent;
147
    }
148
149
    /**
150
     * Determines if the parent object was set.
151
     *
152
     * @return bool
153
     */
154 4
    public function hasParent(): bool
155
    {
156 4
        return !is_null($this->parent);
157
    }
158
159
    /**
160
     * Enable the comments flag.
161
     *
162
     * @return \AustinHeap\Security\Txt\SecurityTxt
163
     */
164 2
    public function enableComments(): SecurityTxt
165
    {
166 2
        return $this->setComments(true);
167
    }
168
169
    /**
170
     * Disable the comments flag.
171
     *
172
     * @return \AustinHeap\Security\Txt\SecurityTxt
173
     */
174 1
    public function disableComments(): SecurityTxt
175
    {
176 1
        return $this->setComments(false);
177
    }
178
179
    /**
180
     * Set the comments flag.
181
     *
182
     * @param  string $comments
183
     *
184
     * @return \AustinHeap\Security\Txt\SecurityTxt
185
     */
186 3
    public function setComments(bool $comments): SecurityTxt
187
    {
188 3
        $this->comments = $comments;
189
190 3
        return $this;
191
    }
192
193
    /**
194
     * Get the comments flag.
195
     *
196
     * @return bool
197
     */
198 2
    public function getComments(): bool
199
    {
200 2
        return $this->comments;
201
    }
202
203
    /**
204
     * Enable the debug flag.
205
     *
206
     * @return \AustinHeap\Security\Txt\SecurityTxt
207
     */
208 2
    public function enableDebug(): SecurityTxt
209
    {
210 2
        return $this->setDebug(true);
211
    }
212
213
    /**
214
     * Disable the debug flag.
215
     *
216
     * @return \AustinHeap\Security\Txt\SecurityTxt
217
     */
218 1
    public function disableDebug(): SecurityTxt
219
    {
220 1
        return $this->setDebug(false);
221
    }
222
223
    /**
224
     * Set the debug flag.
225
     *
226
     * @param  bool $debug
227
     *
228
     * @return \AustinHeap\Security\Txt\SecurityTxt
229
     */
230 6
    public function setDebug(bool $debug): SecurityTxt
231
    {
232 6
        $this->debug = $debug;
233
234 6
        return $this;
235
    }
236
237
    /**
238
     * Get the debug flag.
239
     *
240
     * @return bool
241
     */
242 3
    public function getDebug(): bool
243
    {
244 3
        return $this->debug;
245
    }
246
247
    /**
248
     * Set the text.
249
     *
250
     * @param  string $text
251
     *
252
     * @return \AustinHeap\Security\Txt\SecurityTxt
253
     */
254 13
    public function setText(string $text): SecurityTxt
255
    {
256 13
        $this->text = $text;
257
258 13
        return $this;
259
    }
260
261
    /**
262
     * Get the text.
263
     *
264
     * @return string
265
     */
266 13
    public function getText(): string
267
    {
268 13
        return $this->text === null ? '' : $this->text;
0 ignored issues
show
introduced by
The condition $this->text === null can never be true.
Loading history...
269
    }
270
271
    /**
272
     * Stub generate function. Must be overridden by inheriting class that implements SecurityTxtInterface.
273
     *
274
     * @param bool $test_case
275
     *
276
     * @return Reader|Writer|null
277
     * @throws Exception
278
     */
279 1
    public function execute(bool $test_case = false)
280
    {
281 1
        return $this->overrideMissing(__FUNCTION__, $test_case);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->overrideMissing(__FUNCTION__, $test_case) targeting AustinHeap\Security\Txt\...yTxt::overrideMissing() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
282
    }
283
284
    /**
285
     * Stub reset function. Must be overridden by inheriting class that implements SecurityTxtInterface.
286
     *
287
     * @param bool $test_case
288
     *
289
     * @return Reader|Writer|null
290
     * @throws Exception
291
     */
292 1
    public function reset(bool $test_case = false)
293
    {
294 1
        return $this->overrideMissing(__FUNCTION__, $test_case);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->overrideMissing(__FUNCTION__, $test_case) targeting AustinHeap\Security\Txt\...yTxt::overrideMissing() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
295
    }
296
297
    /**
298
     * Throws an exception when the inheriting class did not correctly implement SecurityTxtInterface.
299
     *
300
     * @param string $function
301
     * @param bool   $test_case
302
     *
303
     * @return null
304
     * @throws Exception
305
     */
306 3
    public function overrideMissing(string $function, bool $test_case = false)
307
    {
308 3
        if ($test_case) {
309 1
            return null;
310
        }
311
312 2
        throw new Exception('Function "' . $function . '" must be overridden by parent SecurityTxtInterface before being called.');
313
    }
314
}
315