Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#288)
by Jérémiah
15:49
created

BatchParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 9.46 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 7
loc 74
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 19 4
B getParsedBody() 7 29 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Overblog\GraphQLBundle\Request;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
7
8
class BatchParser implements ParserInterface
9
{
10
    use UploadParserTrait;
11
12
    const PARAM_ID = 'id';
13
14
    private static $queriesDefaultValue = [
15
        self::PARAM_ID => null,
16
        self::PARAM_QUERY => null,
17
        self::PARAM_VARIABLES => null,
18
    ];
19
20
    /**
21
     * @param Request $request
22
     *
23
     * @return array
24
     */
25 7
    public function parse(Request $request)
26
    {
27
        // Extracts the GraphQL request parameters
28 7
        $queries = $this->getParsedBody($request);
29
30 5
        if (empty($queries)) {
31 1
            throw new BadRequestHttpException('Must provide at least one valid query.');
32
        }
33
34 4
        foreach ($queries as $i => &$query) {
35 4
            $query = array_filter($query) + self::$queriesDefaultValue;
36
37 4
            if (!is_string($query[static::PARAM_QUERY])) {
38 4
                throw new BadRequestHttpException(sprintf('%s is not a valid query', json_encode($query[static::PARAM_QUERY])));
39
            }
40
        }
41
42 3
        return $queries;
43
    }
44
45
    /**
46
     * Gets the body from the request.
47
     *
48
     * @param Request $request
49
     *
50
     * @return array
51
     */
52 7
    private function getParsedBody(Request $request)
53
    {
54 7
        $contentType = explode(';', $request->headers->get('content-type'))[0];
55
56
        // JSON object
57
        switch ($contentType) {
58 7 View Code Duplication
            case static::CONTENT_TYPE_JSON:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59 5
                $parsedBody = json_decode($request->getContent(), true);
60
61 5
                if (JSON_ERROR_NONE !== json_last_error()) {
62 1
                    throw new BadRequestHttpException('POST body sent invalid JSON');
63
                }
64 4
                break;
65
66 2
            case static::CONTENT_TYPE_FORM_DATA:
67 1
                $parsedBody = $this->treatUploadFiles($request->request->all(), $request->files->all());
68 1
                break;
69
70
            default:
71 1
                throw new BadRequestHttpException(sprintf(
72 1
                    'Batching parser only accepts "%s" or "%s" content-type but got %s.',
73 1
                    static::CONTENT_TYPE_JSON,
74 1
                    static::CONTENT_TYPE_FORM_DATA,
75 1
                    json_encode($contentType)
76
                ));
77
        }
78
79 5
        return $parsedBody;
80
    }
81
}
82