Passed
Branch ignore-files (1e3f4a)
by Natan
02:30
created

MainController::processSniff()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 6.7272
cc 7
eloc 24
nc 12
nop 1
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);
0 ignored issues
show
Bug introduced by
It seems like $parsed_options defined by \SnifferReport\Service\O...:parseOptions($options) on line 61 can also be of type null; however, SnifferReport\Service\Sniffer::sniffFiles() does only seem to accept array, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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