GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( d0a329...633762 )
by nguereza
09:02
created

Helper::normalizeFiles()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 46
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 4
nop 1
dl 0
loc 46
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Upload
5
 *
6
 * Platine Upload provides a flexible file uploads with extensible
7
 * validation and storage strategies.
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Upload
12
 * Copyright (c) 2014 Adrian Miu
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Helper.php
35
 *
36
 *  The Upload Helper class
37
 *
38
 *  @package    Platine\Upload\Util
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   http://www.iacademy.cf
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Upload\Util;
50
51
use Platine\Http\UploadedFile;
52
use Platine\Upload\File\File;
53
54
/**
55
 * Class Helper
56
 * @package Platine\Upload\Util
57
 */
58
class Helper
59
{
60
    /**
61
     * Normalize the uploaded files to fit our format
62
     *
63
     * @param array<mixed> $files the normalized
64
     * format of UploadedFile::createFromGlobals()
65
     * @return array<string, File>|array<string, array<int, File>>
66
     */
67
    public static function normalizeFiles(array $files): array
68
    {
69
        /** @var array<mixed> $result */
70
        $result = [];
71
72
        /**
73
         * TODO
74
         * Only support for :
75
         * <input type = 'file' name ='my_file' /><br />
76
         * <input type = 'file' name ='files[]' /><br />
77
         *
78
         * Not support currently:
79
         *  <input type = 'file' name ='files[my_name]' />
80
         *  <input type = 'file' name ='files[][foo]' />
81
         *  <input type = 'file' name ='files[][bar][]' />
82
         *  <input type = 'file' name ='files[file1][file2][file3][file_n]' />
83
         *  <input type = 'file' name ='files[file_name][]' />
84
         */
85
        foreach ($files as $name => $file) {
86
            if ($file instanceof UploadedFile) {
87
                $tempName = (string) tempnam(sys_get_temp_dir(), uniqid());
88
                static::moveUploadedFileToTempDirectory($file, $tempName);
89
90
                $result[$name] = File::create($tempName, $file->getClientFilename(), $file->getError());
91
                continue;
92
            }
93
94
            if (is_array($file)) {
95
                $result[$name] = [];
96
97
                foreach ($file as $index => $file2) {
98
                    if (is_int($index) && $file2 instanceof UploadedFile) {
99
                        $tempName = (string) tempnam(sys_get_temp_dir(), uniqid());
100
                        static::moveUploadedFileToTempDirectory($file2, $tempName);
101
102
                        $result[$name][$index] = File::create(
103
                            $tempName,
104
                            $file2->getClientFilename(),
105
                            $file2->getError()
106
                        );
107
                    }
108
                }
109
            }
110
        }
111
112
        return $result;
113
    }
114
115
    /**
116
     * Convert the size like 4G, 7T, 19B to byte
117
     * @param string $size
118
     * @return int
119
     */
120
    public static function sizeInBytes(string $size): int
121
    {
122
        $unit = 'B';
123
        $units = ['B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4];
124
        $matches = [];
125
        preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches);
126
        if (array_key_exists('unit', $matches)) {
127
            $unit = strtoupper($matches['unit']);
128
        }
129
        return (int)(floatval($matches['size']) * pow(1024, $units[$unit]));
130
    }
131
132
    /**
133
     * Format to human readable size
134
     * @param int $size
135
     * @param int $precision
136
     * @return string
137
     */
138
    public static function formatSize(int $size, int $precision = 2): string
139
    {
140
        if ($size > 0) {
141
            $base = log($size) / log(1024);
142
            $suffixes = ['B', 'K', 'M', 'G', 'T'];
143
            $suffix = '';
144
            if (isset($suffixes[floor($base)])) {
145
                $suffix = $suffixes[floor($base)];
146
            }
147
            return round(pow(1024, $base - floor($base)), $precision) . $suffix;
148
        }
149
150
        return '';
151
    }
152
153
    /**
154
     * Move uploadedFile to temporary directory
155
     * @param UploadedFile $file
156
     * @param string $tempDir
157
     * @return void
158
     */
159
    protected static function moveUploadedFileToTempDirectory(UploadedFile $file, string $tempDir): void
160
    {
161
        if (!in_array($file->getError(), [UPLOAD_ERR_NO_FILE])) {
162
            $file->moveTo($tempDir);
163
        }
164
    }
165
}
166