File::move()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
c 1
b 0
f 0
rs 9.0534
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace BigFileTools;
4
5
use BigFileTools\Driver\ISizeDriver;
6
7
class File
8
{
9
	/**
10
	 * Full path to file
11
	 * @var string
12
	 */
13
	private $path;
14
15
	/**
16
	 * @var ISizeDriver
17
	 */
18
	private $sizeDriver;
19
20
	/**
21
	 * File constructor.
22
	 * @param string $absoluteFilePath **absolute** path to file
23
	 * @param ISizeDriver $sizeDriver Driver used for determining file size
24
	 */
25
	public function __construct($absoluteFilePath, ISizeDriver $sizeDriver)
26
	{
27
		$this->path = $absoluteFilePath;
28
		$this->sizeDriver = $sizeDriver;
29
	}
30
31
	/**
32
	 * @return \Brick\Math\BigInteger
33
	 * @throws Driver\Exception
34
	 */
35
	public function getSize()
36
	{
37
		return $this->sizeDriver->getFileSize($this->path);
38
	}
39
40
	/**
41
	 * Absolute path to file
42
	 * @return string
43
	 */
44
	public function getPath()
45
	{
46
		return $this->path;
47
	}
48
49
	/**
50
	 * Returns PHP's file info (some values can be incorrect due to platform limitations)
51
	 * @return \SplFileInfo
52
	 */
53
	public function getFileInfo()
54
	{
55
		return new \SplFileInfo($this->path);
56
	}
57
58
	/**
59
	 * Returns if file exists and is readable
60
	 * @return bool
61
	 */
62
	public function isReadableFile()
63
	{
64
		// Do not use is_file
65
		// @link https://bugs.php.net/bug.php?id=27792
66
		// $readable = is_readable($file); // does not always return correct value for directories
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% 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...
67
68
		$fp = @fopen($this->path, "r"); // must be file and must be readable
69
		if($fp) {
70
			fclose($fp);
71
			return true;
72
		}
73
		return false;
74
	}
75
76
	/**
77
	 * Moves file to new location / rename
78
	 * @param string $destination path
79
	 * @return bool
80
	 * @throws Exception
81
	 */
82
	function move($destination)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
	{
84
		if (move_uploaded_file($this->path, $destination)) {
85
			$this->path = $destination;
86
			return;
87
		}
88
89
		@unlink($destination); // needed in PHP < 5.3 & Windows; intentionally @
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...
90
		if (rename($this->path, $destination)) {
91
			$this->path = $destination;
92
			return;
93
		}
94
95
		if (copy($this->path, $destination)) {
96
			unlink($this->path); // delete file
97
			$this->path = $destination;
98
			return;
99
		}
100
101
		throw new Exception("File cannot be moved. All supported methods failed.");
102
	}
103
}