GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#202)
by Eric
63:19
created

Configuration::createMapNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map bundle package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\GoogleMapBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 639
    public function getConfigTreeBuilder()
28
    {
29 639
        $treeBuilder = $this->createTreeBuilder();
30 639
        $children = $treeBuilder->root('ivory_google_map')
31 639
            ->children()
32 639
            ->append($this->createMapNode())
33 639
            ->append($this->createStaticMapNode());
34 639
35
        $services = [
36
            'direction'          => true,
37 639
            'distance_matrix'    => true,
38 497
            'elevation'          => true,
39 497
            'geocoder'           => true,
40 497
            'place_autocomplete' => true,
41 497
            'place_detail'       => true,
42 497
            'place_photo'        => false,
43 497
            'place_search'       => true,
44 497
            'time_zone'          => true,
45 497
        ];
46 497
47
        foreach ($services as $service => $http) {
48 639
            $children->append($this->createServiceNode($service, $http));
49 639
        }
50 497
51
        return $treeBuilder;
52 639
    }
53
54
    /**
55
     * @return ArrayNodeDefinition
56
     */
57
    private function createMapNode()
58
    {
59
        return $this->createNode('map')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method addDefaultsIfNotSet() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
60
            ->addDefaultsIfNotSet()
61 639
            ->children()
62
                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
63 639
                ->scalarNode('language')->defaultValue('%locale%')->end()
64
                ->scalarNode('api_key')->end()
65 639
            ->end();
66 639
    }
67 639
68
    /**
69 639
     * @return ArrayNodeDefinition
70
     */
71 639
    private function createStaticMapNode()
72 639
    {
73 639
        return $this->createNode('static_map')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method addDefaultsIfNotSet() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
74 639
            ->addDefaultsIfNotSet()
75 639
            ->children()
76 639
                ->scalarNode('api_key')->end()
77 639
                ->append($this->createBusinessAccountNode(false))
78 639
            ->end();
79 639
    }
80 497
81
    /**
82 639
     * @param string $service
83 639
     * @param bool   $http
84
     *
85 639
     * @return ArrayNodeDefinition
86
     */
87
    private function createServiceNode($service, $http)
88 639
    {
89
        $node = $this->createNode($service);
90
        $children = $node
91
            ->children()
92
            ->scalarNode('api_key')->end()
93
            ->append($this->createBusinessAccountNode(true));
94 639
95
        if ($http) {
96 639
            $children
97 639
                ->scalarNode('client')
98 639
                    ->isRequired()
99 639
                    ->cannotBeEmpty()
100 639
                ->end()
101 639
                ->scalarNode('message_factory')
102 639
                    ->isRequired()
103 639
                    ->cannotBeEmpty()
104 639
                ->end()
105 639
                ->scalarNode('format')->end();
106 639
        } else {
107 639
            $node
108
                ->beforeNormalization()
109
                    ->ifNull()
110
                    ->then(function () { return []; })
111
                ->end();
112
        }
113
114
        return $node;
115
    }
116 639
117
    /**
118 639
     * @param bool $service
119
     *
120
     * @return ArrayNodeDefinition
121
     */
122
    private function createBusinessAccountNode($service)
123
    {
124 639
        $node = $this->createNode('business_account');
125
        $clientIdNode = $node->children()
126 639
            ->scalarNode('secret')
127
                ->isRequired()
128
                ->cannotBeEmpty()
129
            ->end()
130
            ->scalarNode('channel')->end()
131
            ->scalarNode('client_id');
132
133
        if ($service) {
134
            $clientIdNode
135
                ->isRequired()
136
                ->cannotBeEmpty();
137
        };
138
139
        return $node;
140
    }
141
142
    /**
143
     * @param string $name
144
     * @param string $type
145
     *
146
     * @return ArrayNodeDefinition|NodeDefinition
147
     */
148
    private function createNode($name, $type = 'array')
149
    {
150
        return $this->createTreeBuilder()->root($name, $type);
151
    }
152
153
    /**
154
     * @return TreeBuilder
155
     */
156
    private function createTreeBuilder()
157
    {
158
        return new TreeBuilder();
159
    }
160
}
161