Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/UploadService.php (1 issue)

Checks if the types of returned expressions are compatible with the documented types.

Best Practice Bug Major
1
<?php
2
3
namespace Elgg;
4
5
use Elgg\Http\Request;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Elgg\Filesystem\MimeTypeDetector;
8
use Elgg\ImageService;
9
10
/**
11
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
12
 *
13
 * Use the elgg_* versions instead.
14
 *
15
 * @access private
16
 * @since 2.3
17
 */
18
class UploadService {
19
20
	/**
21
	 * @var Request
22
	 */
23
	private $request;
24
25
	/**
26
	 * @var ImageService
27
	 */
28
	private $images;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param Request      $request Http request
34
	 * @param ImageService $images  The image service
35
	 */
36 68
	public function __construct(Request $request, ImageService $images) {
37 68
		$this->request = $request;
38 68
		$this->images = $images;
39 68
	}
40
41
	/**
42
	 * Returns an array of uploaded file objects regardless of upload status/errors
43
	 *
44
	 * @param string $input_name Form input name
45
	 * @return UploadedFile[]
46
	 */
47 5
	public function getFiles($input_name) {
48 5
		$files = $this->request->getFiles($input_name);
49 5
		foreach ($files as $file) {
50 4
			$this->prepareFile($file);
51
		}
52
		
53 5
		return $files;
54
	}
55
56
	/**
57
	 * Returns an single valid uploaded file object
58
	 *
59
	 * @param string $input_name         Form input name
60
	 * @param bool   $check_for_validity If there is an uploaded file, is it required to be valid
61
	 *
62
	 * @return UploadedFile[]|false
63
	 */
64 1
	public function getFile($input_name, $check_for_validity = true) {
65 1
		$file = $this->request->getFile($input_name, $check_for_validity);
66
		
67 1
		if ($file instanceof UploadedFile) {
68 1
			$this->prepareFile($file);
69
		}
70
		
71 1
		return $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file also could return the type Elgg\Http\UploadedFile which is incompatible with the documented return type Symfony\Component\HttpFo...le\UploadedFile[]|false.
Loading history...
72
	}
73
	
74
	/**
75
	 * Prepares an uploaded file
76
	 *
77
	 * @param UploadedFile $file File to prepare
78
	 *
79
	 * @return void
80
	 */
81 5
	protected function prepareFile(UploadedFile $file) {
82 5
		if (!$file->isValid()) {
83 1
			return;
84
		}
85
		
86 5
		$mime_detector = new MimeTypeDetector();
87 5
		$mime = $mime_detector->getType($file->getPathname());
88
89 5
		if (strpos($mime, 'image/') === 0) {
90 5
			$this->fixImageOrientation($file);
91
		}
92 5
	}
93
	
94
	/**
95
	 * Fixes the orientation of an image
96
	 *
97
	 * @param UploadedFile $file File to fix
98
	 *
99
	 * @return void
100
	 */
101 5
	protected function fixImageOrientation(UploadedFile $file) {
102 5
		$temp_location = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . uniqid() . $file->getClientOriginalName();
103 5
		copy($file->getPathname(), $temp_location);
104
		
105 5
		$rotated = $this->images->fixOrientation($temp_location);
106 5
		if ($rotated) {
107 5
			copy($temp_location, $file->getPathname());
108
		}
109 5
	}
110
}
111