|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SnifferReport\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use SnifferReport\Exception\InvalidStandardsException; |
|
6
|
|
|
use SnifferReport\Exception\GitCloneException; |
|
7
|
|
|
use SnifferReport\Exception\MissingFileOrGitParameterException; |
|
8
|
|
|
use SnifferReport\Model\Sniff; |
|
9
|
|
|
use SnifferReport\Service\OptionsParser; |
|
10
|
|
|
use SnifferReport\Service\Validator; |
|
11
|
|
|
use SnifferReport\Service\FilesHandler; |
|
12
|
|
|
use SnifferReport\Service\GitHandler; |
|
13
|
|
|
use SnifferReport\Service\Sniffer; |
|
14
|
|
|
use SnifferReport\Service\SniffParser; |
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use \Exception; |
|
18
|
|
|
|
|
19
|
|
|
class MainController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Processes the POSTed data. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
* |
|
26
|
|
|
* @return Sniff |
|
27
|
|
|
* |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
* @throws GitCloneException |
|
30
|
|
|
* @throws InvalidStandardsException |
|
31
|
|
|
* @throws MissingFileOrGitParameterException |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function processSniff(Request $request) |
|
34
|
|
|
{ |
|
35
|
|
|
$file = $request->files->get('file'); |
|
36
|
|
|
$git_url = $request->get('git_url'); |
|
37
|
|
|
$standards = $request->get('standards'); |
|
38
|
|
|
$options = $request->get('options'); |
|
39
|
|
|
|
|
40
|
|
|
$valid_git_url = (is_null($git_url) || !Validator::isGitUrl($git_url)); |
|
41
|
|
|
if ($valid_git_url && is_null($file)) { |
|
42
|
|
|
throw new MissingFileOrGitParameterException(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!is_null($file)) { |
|
46
|
|
|
$file_name = $file->getClientOriginalName(); |
|
47
|
|
|
$file->move(FILES_DIRECTORY_ROOT, $file_name); |
|
48
|
|
|
$files = FilesHandler::handle($file_name, $file->getClientMimeType()); |
|
49
|
|
|
} else { |
|
50
|
|
|
try { |
|
51
|
|
|
$files = GitHandler::handle($git_url); |
|
52
|
|
|
} catch (Exception $e) { |
|
53
|
|
|
throw new GitCloneException($e); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!Validator::areAllStandardsValid($standards)) { |
|
58
|
|
|
throw new InvalidStandardsException(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$parsed_options = OptionsParser::parseOptions($options); |
|
62
|
|
|
$sniff_result = Sniffer::sniffFiles($files, $standards, $parsed_options); |
|
|
|
|
|
|
63
|
|
|
// Delete all files that were generated in this request. |
|
64
|
|
|
$fs = new Filesystem(); |
|
65
|
|
|
$fs->remove(FILES_DIRECTORY_ROOT); |
|
66
|
|
|
|
|
67
|
|
|
return SniffParser::parseSniff($sniff_result); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.