Completed
Push — 5.x ( 9c6f17...909a08 )
by Lars
04:57
created

swiftmailer_generate_mimes_config.php ➔ generateUpToDateMimeArray()   D

Complexity

Conditions 17
Paths 28

Size

Total Lines 170
Code Lines 112

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 17
eloc 112
nc 28
nop 0
dl 0
loc 170
rs 4.8361

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
    $mime_types = @file_get_contents(APACHE_MIME_TYPES_URL);
25
    $mime_xml = @file_get_contents(FREEDESKTOP_XML_URL);
26
27
    // prepare valid mime types
28
    $valid_mime_types = array();
29
30
    // split mime type and extensions eg. "video/x-matroska        mkv mk3d mks"
31
    if (preg_match_all('/^#?([a-z0-9\-\+\/\.]+)[\t]+(.*)$/miu', $mime_types, $matches) !== false) {
32
        // collection of predefined mimetypes (bugfix for wrong resolved or missing mime types)
33
        $valid_mime_types_preset = array(
34
            'php' => 'application/x-php',
35
            'php3' => 'application/x-php',
36
            'php4' => 'application/x-php',
37
            'php5' => 'application/x-php',
38
            'zip' => 'application/zip',
39
            'gif' => 'image/gif',
40
            'png' => 'image/png',
41
            'css' => 'text/css',
42
            'js' => 'text/javascript',
43
            'txt' => 'text/plain',
44
            'aif' => 'audio/x-aiff',
45
            'aiff' => 'audio/x-aiff',
46
            'avi' => 'video/avi',
47
            'bmp' => 'image/bmp',
48
            'bz2' => 'application/x-bz2',
49
            'csv' => 'text/csv',
50
            'dmg' => 'application/x-apple-diskimage',
51
            'doc' => 'application/msword',
52
            'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
53
            'eml' => 'message/rfc822',
54
            'aps' => 'application/postscript',
55
            'exe' => 'application/x-ms-dos-executable',
56
            'flv' => 'video/x-flv',
57
            'gz' => 'application/x-gzip',
58
            'hqx' => 'application/stuffit',
59
            'htm' => 'text/html',
60
            'html' => 'text/html',
61
            'jar' => 'application/x-java-archive',
62
            'jpeg' => 'image/jpeg',
63
            'jpg' => 'image/jpeg',
64
            'm3u' => 'audio/x-mpegurl',
65
            'm4a' => 'audio/mp4',
66
            'mdb' => 'application/x-msaccess',
67
            'mid' => 'audio/midi',
68
            'midi' => 'audio/midi',
69
            'mov' => 'video/quicktime',
70
            'mp3' => 'audio/mpeg',
71
            'mp4' => 'video/mp4',
72
            'mpeg' => 'video/mpeg',
73
            'mpg' => 'video/mpeg',
74
            'odg' => 'vnd.oasis.opendocument.graphics',
75
            'odp' => 'vnd.oasis.opendocument.presentation',
76
            'odt' => 'vnd.oasis.opendocument.text',
77
            'ods' => 'vnd.oasis.opendocument.spreadsheet',
78
            'ogg' => 'audio/ogg',
79
            'pdf' => 'application/pdf',
80
            'ppt' => 'application/vnd.ms-powerpoint',
81
            'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
82
            'ps' => 'application/postscript',
83
            'rar' => 'application/x-rar-compressed',
84
            'rtf' => 'application/rtf',
85
            'tar' => 'application/x-tar',
86
            'sit' => 'application/x-stuffit',
87
            'svg' => 'image/svg+xml',
88
            'tif' => 'image/tiff',
89
            'tiff' => 'image/tiff',
90
            'ttf' => 'application/x-font-truetype',
91
            'vcf' => 'text/x-vcard',
92
            'wav' => 'audio/wav',
93
            'wma' => 'audio/x-ms-wma',
94
            'wmv' => 'audio/x-ms-wmv',
95
            'xls' => 'application/excel',
96
            'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
97
            'xml' => 'application/xml',
98
        );
99
100
        // wrap array for generating file
101
        foreach ($valid_mime_types_preset as $extension => $mime_type) {
102
            // generate array for mimetype to extension resolver (only first match)
103
            $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
104
        }
105
106
        // all extensions from second match
107
        foreach ($matches[2] as $i => $extensions) {
108
            // explode multiple extensions from string
109
            $extensions = explode(' ', strtolower($extensions));
110
111
            // 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...
112
            if (!is_array($extensions)) {
113
                $extensions = array($extensions);
114
            }
115
116
            foreach ($extensions as $extension) {
117
                // get mime type
118
                $mime_type = $matches[1][$i];
119
120
                // check if string length lower than 10
121
                if (strlen($extension) < 10) {
122
                    if (!isset($valid_mime_types[$mime_type])) {
123
                        // generate array for mimetype to extension resolver (only first match)
124
                        $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
125
                    }
126
                }
127
            }
128
        }
129
    }
130
131
    $xml = simplexml_load_string($mime_xml);
132
133
    foreach ($xml as $node) {
134
        // check if there is no pattern
135
        if (!isset($node->glob['pattern'])) {
136
            continue;
137
        }
138
139
        // get all matching extensions from match
140
        foreach ((array) $node->glob['pattern'] as $extension) {
141
            // skip none glob extensions
142
            if (strpos($extension, '.') === false) {
143
                continue;
144
            }
145
        }
146
147
        if (isset($node->glob['pattern'][0])) {
148
            // mime type
149
            $mime_type = strtolower((string) $node['type']);
150
151
            // get first extension
152
            $extension = strtolower(trim($node->glob['ddpattern'][0], '*.'));
153
154
            // skip none glob extensions and check if string length between 1 and 10
155
            if (strpos($extension, '.') !== false || strlen($extension) < 1 || strlen($extension) > 9) {
156
                continue;
157
            }
158
159
            // check if string length lower than 10
160
            if (!isset($valid_mime_types[$mime_type])) {
161
                // generate array for mimetype to extension resolver (only first match)
162
                $valid_mime_types[$extension] = "'{$extension}' => '{$mime_type}'";
163
            }
164
        }
165
    }
166
167
    // full list of valid extensions only
168
    $valid_mime_types = array_unique($valid_mime_types);
169
    ksort($valid_mime_types);
170
171
    // combine mime types and extensions array
172
    $output = "$preamble\$swift_mime_types = array(\n    " . implode($valid_mime_types, ",\n    ") . "\n);";
173
174
    // write mime_types.php config file
175
    @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...
176
}
177
178
generateUpToDateMimeArray();
179