swiftmailer_generate_mimes_config.php ➔ generateUpToDateMimeArray()   D
last analyzed

Complexity

Conditions 17
Paths 34

Size

Total Lines 183
Code Lines 118

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 118
nc 34
nop 0
dl 0
loc 183
rs 4.8361
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
#!/usr/bin/php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
<?php
4
define('APACHE_MIME_TYPES_URL', 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types');
5
define('FREEDESKTOP_XML_URL', 'https://raw2.github.com/minad/mimemagic/master/script/freedesktop.org.xml');
6
7
function generateUpToDateMimeArray()
8
{
9
    $preamble = "<?php\n\n";
10
    $preamble .= "/*\n";
11
    $preamble .= " * This file is part of SwiftMailer.\n";
12
    $preamble .= " * (c) 2004-2009 Chris Corbyn\n *\n";
13
    $preamble .= " * For the full copyright and license information, please view the LICENSE\n";
14
    $preamble .= " * file that was distributed with this source code.\n *\n";
15
    $preamble .= " * autogenerated using http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types\n";
16
    $preamble .= " * and https://raw.github.com/minad/mimemagic/master/script/freedesktop.org.xml\n";
17
    $preamble .= " */\n\n";
18
    $preamble .= "/*\n";
19
    $preamble .= " * List of MIME type automatically detected in Swift Mailer.\n";
20
    $preamble .= " */\n\n";
21
    $preamble .= "// You may add or take away what you like (lowercase required)\n\n";
22
23
    // get current mime types files
24
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
25
    $mime_types = @file_get_contents(APACHE_MIME_TYPES_URL);
26
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
27
    $mime_xml = @file_get_contents(FREEDESKTOP_XML_URL);
28
29
    // prepare valid mime types
30
    $valid_mime_types = array();
31
32
    // split mime type and extensions eg. "video/x-matroska        mkv mk3d mks"
33
    if (preg_match_all('/^#?([a-z0-9\-\+\/\.]+)[\t]+(.*)$/miu', $mime_types, $matches) !== false) {
34
        // collection of predefined mimetypes (bugfix for wrong resolved or missing mime types)
35
        $valid_mime_types_preset = array(
36
            'php' => 'application/x-php',
37
            'php3' => 'application/x-php',
38
            'php4' => 'application/x-php',
39
            'php5' => 'application/x-php',
40
            'zip' => 'application/zip',
41
            'gif' => 'image/gif',
42
            'png' => 'image/png',
43
            'css' => 'text/css',
44
            'js' => 'text/javascript',
45
            'txt' => 'text/plain',
46
            'aif' => 'audio/x-aiff',
47
            'aiff' => 'audio/x-aiff',
48
            'avi' => 'video/avi',
49
            'bmp' => 'image/bmp',
50
            'bz2' => 'application/x-bz2',
51
            'csv' => 'text/csv',
52
            'dmg' => 'application/x-apple-diskimage',
53
            'doc' => 'application/msword',
54
            'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
55
            'eml' => 'message/rfc822',
56
            'aps' => 'application/postscript',
57
            'exe' => 'application/x-ms-dos-executable',
58
            'flv' => 'video/x-flv',
59
            'gz' => 'application/x-gzip',
60
            'hqx' => 'application/stuffit',
61
            'htm' => 'text/html',
62
            'html' => 'text/html',
63
            'jar' => 'application/x-java-archive',
64
            'jpeg' => 'image/jpeg',
65
            'jpg' => 'image/jpeg',
66
            'm3u' => 'audio/x-mpegurl',
67
            'm4a' => 'audio/mp4',
68
            'mdb' => 'application/x-msaccess',
69
            'mid' => 'audio/midi',
70
            'midi' => 'audio/midi',
71
            'mov' => 'video/quicktime',
72
            'mp3' => 'audio/mpeg',
73
            'mp4' => 'video/mp4',
74
            'mpeg' => 'video/mpeg',
75
            'mpg' => 'video/mpeg',
76
            'odg' => 'vnd.oasis.opendocument.graphics',
77
            'odp' => 'vnd.oasis.opendocument.presentation',
78
            'odt' => 'vnd.oasis.opendocument.text',
79
            'ods' => 'vnd.oasis.opendocument.spreadsheet',
80
            'ogg' => 'audio/ogg',
81
            'pdf' => 'application/pdf',
82
            'ppt' => 'application/vnd.ms-powerpoint',
83
            'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
84
            'ps' => 'application/postscript',
85
            'rar' => 'application/x-rar-compressed',
86
            'rtf' => 'application/rtf',
87
            'tar' => 'application/x-tar',
88
            'sit' => 'application/x-stuffit',
89
            'svg' => 'image/svg+xml',
90
            'tif' => 'image/tiff',
91
            'tiff' => 'image/tiff',
92
            'ttf' => 'application/x-font-truetype',
93
            'vcf' => 'text/x-vcard',
94
            'wav' => 'audio/wav',
95
            'wma' => 'audio/x-ms-wma',
96
            'wmv' => 'audio/x-ms-wmv',
97
            'xls' => 'application/excel',
98
            'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
99
            'xml' => 'application/xml',
100
        );
101
102
        // wrap array for generating file
103
        foreach ($valid_mime_types_preset as $extension => $mime_type) {
104
            // generate array for mimetype to extension resolver (only first match)
105
            $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
106
        }
107
108
        // all extensions from second match
109
        foreach ($matches[2] as $i => $extensions) {
110
            // explode multiple extensions from string
111
            $extensions = explode(' ', strtolower($extensions));
112
113
            // force array for foreach
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
114
            if (!is_array($extensions)) {
115
                $extensions = array($extensions);
116
            }
117
118
            foreach ($extensions as $extension) {
119
                // get mime type
120
                $mime_type = $matches[1][$i];
121
122
                // check if string length lower than 10
123
                if (strlen($extension) < 10) {
124
                    if (!isset($valid_mime_types[$mime_type])) {
125
                        // generate array for mimetype to extension resolver (only first match)
126
                        $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
127
                    }
128
                }
129
            }
130
        }
131
    }
132
133
    $xml = simplexml_load_string($mime_xml);
134
135
    foreach ($xml as $node) {
136
        // check if there is no pattern
137
        if (!isset($node->glob['pattern'])) {
138
            continue;
139
        }
140
141
        // get all matching extensions from match
142
        foreach ((array) $node->glob['pattern'] as $extension) {
143
            // skip none glob extensions
144
            if (strpos($extension, '.') === false) {
145
                continue;
146
            }
147
        }
148
149
        if (isset($node->glob['pattern'][0])) {
150
            // mime type
151
            $mime_type = strtolower((string) $node['type']);
152
153
            // get first extension
154
            $extension = strtolower(trim($node->glob['ddpattern'][0], '*.'));
155
156
            // skip none glob extensions
157
            if (strpos($extension, '.') !== false) {
158
                continue;
159
            }
160
161
            // check if string length between 1 and 10
162
            $extensionLength = strlen($extension);
163
            if (
164
                $extensionLength < 1
165
                ||
166
                $extensionLength > 9
167
            ) {
168
                continue;
169
            }
170
171
            // check if string length lower than 10
172
            if (!isset($valid_mime_types[$mime_type])) {
173
                // generate array for mimetype to extension resolver (only first match)
174
                $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
175
            }
176
        }
177
    }
178
179
    // full list of valid extensions only
180
    $valid_mime_types = array_unique($valid_mime_types);
181
    ksort($valid_mime_types);
182
183
    // combine mime types and extensions array
184
    $output = "$preamble\$swift_mime_types = array(\n    " . implode($valid_mime_types, ",\n    ") . "\n);";
185
186
    // write mime_types.php config file
187
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
188
    @file_put_contents('./mime_types.php', $output);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
189
}
190
191
generateUpToDateMimeArray();
192