Completed
Push — master ( 0fee6d...0e1952 )
by Nazar
04:20
created

Files::init_files_internal()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 6.7272
cc 7
eloc 18
nc 5
nop 1
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\Request;
9
10
trait Files {
11
	/**
12
	 * Normalized files array
13
	 *
14
	 * Each file item can be either single file or array of files (in contrast with native PHP arrays where each field like `name` become an array) with keys
15
	 * `name`, `type`, `size`, `tmp_name`, `stream` and `error`
16
	 *
17
	 * `name`, `type`, `size` and `error` keys are similar to native PHP fields in `$_FILES`; `tmp_name` might not be temporary file, but file descriptor
18
	 * wrapper like `php://fd/1` and `stream` is resource like obtained with `fopen('/tmp/xyz')`
19
	 *
20
	 * @var array
21
	 */
22
	public $files;
23
	/**
24
	 * @param array $files Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys `name`, `type`, `size`,
25
	 *                     `error` and at least one of `tmp_name` or `stream`
26
	 */
27
	function init_files ($files = []) {
28
		$this->files = $this->init_files_internal($files);
29
	}
30
	/**
31
	 * @param array $files
32
	 *
33
	 * @return array
34
	 */
35
	protected function init_files_internal ($files) {
36
		if (!isset($files['name'])) {
37
			foreach ($files as $field => &$file) {
38
				$file = $this->init_files_internal($file);
0 ignored issues
show
Documentation introduced by
$file is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
			}
40
			return $files;
41
		}
42
		if (is_array($files['name'])) {
43
			$result = [];
44
			foreach (array_keys($files['name']) as $index) {
45
				$result[] = $this->normalize_file(
46
					[
47
						'name'     => $files['name'][$index],
48
						'type'     => $files['type'][$index],
49
						'size'     => $files['size'][$index],
50
						'tmp_name' => @$files['tmp_name'][$index] ?: null,
51
						'stream'   => @$files['stream'][$index] ?: null,
52
						'error'    => $files['error'][$index]
53
					]
54
				);
55
			}
56
			return $result;
57
		} else {
58
			return $this->normalize_file($files);
59
		}
60
	}
61
	/**
62
	 * @param array $file
63
	 *
64
	 * @return array
65
	 */
66
	protected function normalize_file ($file) {
67
		$file += [
68
			'tmp_name' => null,
69
			'stream'   => null
70
		];
71
		if (isset($file['tmp_name']) && $file['stream'] === null) {
72
			$file['stream'] = fopen($file['tmp_name'], 'br');
73
		}
74
		if (isset($file['stream']) && $file['tmp_name'] === null) {
75
			$file['tmp_name'] = "php://fd/$file[stream]";
76
		}
77
		if ($file['tmp_name'] === null && $file['stream'] === null) {
78
			$file['error'] = UPLOAD_ERR_NO_FILE;
79
		}
80
		return $file;
81
	}
82
}
83