1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
* This file is part of StaticReview
*
* Copyright (c) 2014 Samuel Parkinson <@samparkinson_>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see http://github.com/sjparkinson/static-review/blob/master/LICENSE.md
*/
namespace StaticReview\File;
class File implements FileInterface
{
const STATUS_ADDED = 'A';
const STATUS_COPIED = 'C';
const STATUS_MODIFIED = 'M';
const STATUS_RENAMED = 'R';
/**
* The full path to the file.
*/
private $filePath;
/**
* The files status.
*/
private $fileStatus;
/**
* The projects base directory.
*/
private $projectPath;
/**
* The cached location of the file.
*/
private $cachedPath;
/**
* Initializes a new instance of the File class.
*
* @param string $fileStatus
* @param string $filePath
* @param string $projectPath
*/
public function __construct(
$fileStatus,
$filePath,
$projectPath
) {
$this->fileStatus = $fileStatus;
$this->filePath = $filePath;
$this->projectPath = $projectPath;
}
/**
* Returns the name of the file including its extension.
*
* @return string
*/
public function getFileName()
{
return basename($this->filePath);
}
/**
* Returns the local path to the file from the base of the git repository.
*
* @return string
*/
public function getRelativePath()
{
return str_replace($this->projectPath . DIRECTORY_SEPARATOR, '', $this->filePath);
}
/**
* Returns the full path to the file.
*
* @return string
*/
public function getFullPath()
{
if (file_exists($this->getCachedPath())) {
return $this->getCachedPath();
}
return $this->filePath;
}
/**
* Returns the path to the cached copy of the file.
*
* @return string
*/
public function getCachedPath()
{
return $this->cachedPath;
}
/**
* Sets the path to the cached copy of the file.
*
* @param string $path
* @return File
*/
public function setCachedPath($path)
{
$this->cachedPath = $path;
return $this;
}
/**
* Returns the files extension.
*
* @return string
*/
public function getExtension()
{
return pathinfo($this->filePath, PATHINFO_EXTENSION);
}
/**
* Returns the short hand git status of the file.
*
* @return string
*/
public function getStatus()
{
return $this->fileStatus;
}
/**
* Returns the git status of the file as a word.
*
* @return string
*
* @throws UnexpectedValueException
*/
public function getFormattedStatus()
{
switch ($this->fileStatus) {
case 'A':
return 'added';
case 'C':
return 'copied';
case 'M':
return 'modified';
case 'R':
return 'renamed';
default:
throw new \UnexpectedValueException("Unknown file status: $this->fileStatus.");
}
}
/**
* Get the mime type for the file.
*
* @param FileInterface $file
* @return string
*/
public function getMimeType()
{
// return mime type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $this->getFullPath());
return $mime;
}
}