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 (#277)
by Jérémiah
14:57
created

AclConfigProcessor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 21.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 16
loc 75
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C acl() 0 23 7
A process() 16 16 3
A findFieldResolver() 0 12 3

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\Definition\ConfigProcessor;
4
5
use GraphQL\Type\Definition\ResolveInfo;
6
use Overblog\GraphQLBundle\Definition\Argument;
7
use Overblog\GraphQLBundle\Definition\LazyConfig;
8
use Overblog\GraphQLBundle\Error\UserWarning;
9
use Overblog\GraphQLBundle\Resolver\AccessResolver;
10
11
final class AclConfigProcessor implements ConfigProcessorInterface
12
{
13
    /** @var AccessResolver */
14
    private $accessResolver;
15
16
    /** @var callable */
17
    private $defaultResolver;
18
19 77
    public function __construct(AccessResolver $accessResolver, callable $defaultResolver)
20
    {
21 77
        $this->accessResolver = $accessResolver;
22 77
        $this->defaultResolver = $defaultResolver;
23 77
    }
24
25
    public static function acl(array $fields, AccessResolver $accessResolver, callable $defaultResolver)
26
    {
27 72
        $deniedAccess = static function () {
28 1
            throw new UserWarning('Access denied to this field.');
29 72
        };
30 72
        foreach ($fields as &$field) {
31 72
            if (isset($field['access']) && true !== $field['access']) {
32 10
                $accessChecker = $field['access'];
33 10
                if (false === $accessChecker) {
34 10
                    $field['resolve'] = $deniedAccess;
35 10
                } elseif (is_callable($accessChecker)) {
36 72
                    $field['resolve'] = function ($value, $args, $context, ResolveInfo $info) use ($field, $accessChecker, $accessResolver, $defaultResolver) {
37 9
                        $resolverCallback = self::findFieldResolver($field, $info, $defaultResolver);
38 9
                        $isMutation = 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType();
39
40 9
                        return $accessResolver->resolve($accessChecker, $resolverCallback, [$value, new Argument($args), $context, $info], $isMutation);
41 72
                    };
42
                }
43
            }
44
        }
45
46 72
        return $fields;
47
    }
48
49 View Code Duplication
    public function process(LazyConfig $lazyConfig)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51 77
        $lazyConfig->addPostLoader(function ($config) {
52 77
            if (isset($config['fields']) && is_callable($config['fields'])) {
53 72
                $config['fields'] = function () use ($config) {
54 72
                    $fields = $config['fields']();
55
56 72
                    return static::acl($fields, $this->accessResolver, $this->defaultResolver);
57
                };
58
            }
59
60 77
            return $config;
61 77
        });
62
63 77
        return $lazyConfig;
64
    }
65
66
    /**
67
     * @param array       $field
68
     * @param ResolveInfo $info
69
     * @param callable    $defaultResolver
70
     *
71
     * @return callable
72
     */
73 9
    private static function findFieldResolver(array $field, ResolveInfo $info, callable $defaultResolver)
74
    {
75 9
        if (isset($field['resolve'])) {
76 7
            $resolver = $field['resolve'];
77 5
        } elseif (isset($info->parentType->config['resolveField'])) {
78
            $resolver = $info->parentType->config['resolveField'];
79
        } else {
80 5
            $resolver = $defaultResolver;
81
        }
82
83 9
        return $resolver;
84
    }
85
}
86