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

Writer::execute()   F

Complexity

Conditions 27
Paths 648

Size

Total Lines 65
Code Lines 39

Duplication

Lines 16
Ratio 24.62 %

Importance

Changes 0
Metric Value
cc 27
eloc 39
nc 648
nop 1
dl 16
loc 65
rs 3.0769
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * src/Writer.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 Exception;
15
16
/**
17
 * Writer
18
 *
19
 * @link        https://github.com/austinheap/php-security-txt
20
 * @link        https://packagist.org/packages/austinheap/php-security-txt
21
 * @link        https://austinheap.github.io/php-security-txt/classes/AustinHeap.Security.Txt.Writer.html
22
 */
23
class Writer extends SecurityTxt implements SecurityTxtInterface
24
{
25
26
    /**
27
     * Internal lines cache.
28
     *
29
     * @var array
30
     */
31
    private $lines = [];
32
33
    /**
34
     * Create a new Writer instance.
35
     *
36
     * @return Writer|SecurityTxt
37
     */
38
    public function __construct()
39
    {
40
        return parent::__construct($this);
41
    }
42
43
    /**
44
     * Add a comment to the output buffer.
45
     *
46
     * @param  string $comment
47
     *
48
     * @return Writer
49
     */
50
    public function comment(string $comment = ''): Writer
51
    {
52
        $comment = trim($comment);
53
54
        if (!empty($comment)) {
55
            $comment = ' ' . $comment;
56
        }
57
58
        return $this->line(trim('#' . $comment));
59
    }
60
61
    /**
62
     * Add a spacer to the output buffer.
63
     *
64
     * @return Writer
65
     */
66
    public function spacer(): Writer
67
    {
68
        return $this->line('');
69
    }
70
71
    /**
72
     * Add multiple spacers to the output buffer.
73
     *
74
     * @param int $count
75
     *
76
     * @return Writer
77
     */
78
    public function spacers(int $count = 1): Writer
79
    {
80
        for ($x = 0; $x < $count; $x++) {
81
            $this->spacer();
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * Add a line.
89
     *
90
     * @param  string $line
91
     *
92
     * @return Writer
93
     */
94
    public function line(string $line): Writer
95
    {
96
        $this->lines[] = $line;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Add multiple lines.
103
     *
104
     * @param  array $lines
105
     *
106
     * @return Writer
107
     */
108
    public function lines(array $lines): Writer
109
    {
110
        foreach ($lines as $line) {
111
            $this->line($line);
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * Reset the output buffer.
119
     *
120
     * @param bool $test_case
121
     *
122
     * @return Writer
123
     */
124
    public function reset(bool $test_case = false): Writer
125
    {
126
        $this->lines = [];
127
128
        return $this;
129
    }
130
131
    /**
132
     * Execute the SecurityTxtInterface.
133
     *
134
     * @param bool $test_case
135
     *
136
     * @return Writer
137
     * @throws Exception
138
     */
139
    public function execute(bool $test_case = false): Writer
140
    {
141
        if ($this->debug) {
142
            $time = microtime(true);
143
        }
144
145
        if ($this->comments) {
146
            $this->comment('Our security address');
147
        }
148
149
        if (empty($this->contacts)) {
150
            throw new Exception('One (or more) contacts must be defined.');
151
        }
152
153
        foreach (array_keys($this->contacts) as $contact) {
154
            $this->line('Contact: ' . trim($contact));
155
        }
156
157 View Code Duplication
        if (!empty($this->encryption)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
158
            if ($this->comments) {
159
                $this->spacer()
160
                     ->comment('Our PGP key');
161
            }
162
163
            $this->line('Encryption: ' . trim($this->encryption));
164
        }
165
166
        if (!empty($this->disclosure)) {
167
            if ($this->comments) {
168
                $this->spacer()
169
                     ->comment('Our disclosure policy');
170
            }
171
172
            $this->line('Disclosure: ' . trim(ucfirst($this->disclosure)));
173
        }
174
175 View Code Duplication
        if (!empty($this->acknowledgement)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
176
            if ($this->comments) {
177
                $this->spacer()
178
                     ->comment('Our public acknowledgement');
179
            }
180
181
            $this->line('Acknowledgement: ' . trim($this->acknowledgement));
182
        }
183
184
        if ($this->debug && isset($time)) {
185
            $this->spacer()
186
                 ->comment()
187
                 ->comment(
188
                     'Generated by "' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt"' .
189
                     (defined('LARAVEL_SECURITY_TXT_VERSION') ? ' v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? ' v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : ''))) .
0 ignored issues
show
Bug introduced by
The constant AustinHeap\Security\Txt\...SS_SECURITY_TXT_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant AustinHeap\Security\Txt\...EL_SECURITY_TXT_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
190
                     ' (https://github.com/austinheap/' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? 'laravel' : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? 'wordpress' : 'php')) . '-security-txt' . (defined('LARAVEL_SECURITY_TXT_VERSION') ? '/releases/tag/v' . LARAVEL_SECURITY_TXT_VERSION : (defined('WORDPRESS_SECURITY_TXT_VERSION') ? '/releases/tag/v' . WORDPRESS_SECURITY_TXT_VERSION : (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : ''))) . ')');
191
            if (defined('LARAVEL_SECURITY_TXT_VERSION') || defined('WORDPRESS_SECURITY_TXT_VERSION')) {
192
                $this->comment(
193
                    'using "php-security-txt"' . (defined('PHP_SECURITY_TXT_VERSION') ? ' v' . PHP_SECURITY_TXT_VERSION : '') .
194
                    ' (https://github.com/austinheap/php-security-txt' . (defined('PHP_SECURITY_TXT_VERSION') ? '/releases/tag/v' . PHP_SECURITY_TXT_VERSION : '') . ')');
195
            }
196
            $this->comment('in ' . round((microtime(true) - $time) * 1000, 6) . ' seconds on ' . date('c') . '.')
197
                 ->comment()
198
                 ->spacer();
199
        }
200
201
        $output = implode(PHP_EOL, $this->lines);
202
203
        return $this->setText($output);
204
    }
205
}
206