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.

Configuration::getPathNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\MenuBundle\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
use Symfony\Component\Config\Definition\NodeInterface;
19
20
final class Configuration implements ConfigurationInterface
21
{
22
    public function getConfigTreeBuilder()
23
    {
24
        $treeBuilder = new TreeBuilder('core23_menu');
25
26
        $rootNode = $treeBuilder->getRootNode();
27
28
        \assert($rootNode instanceof ArrayNodeDefinition);
29
30
        $this->addMenuSection($rootNode);
31
32
        return $treeBuilder;
33
    }
34
35
    private function addMenuSection(ArrayNodeDefinition $node): void
36
    {
37
        $menuNode = $node
38
            ->children()
39
                ->arrayNode('groups')
40
                    ->useAttributeAsKey('id')
41
                    ->prototype('array')
42
                        ->fixXmlConfig('attribute')
43
                        ->fixXmlConfig('item')
44
                        ->addDefaultsIfNotSet()
45
                        ->children()
46
                            ->scalarNode('name')->defaultNull()->end()
47
                            ->arrayNode('attributes')
48
                                 ->useAttributeAsKey('id')
49
                                 ->defaultValue([])
50
                                 ->prototype('scalar')->end()
51
                            ->end()
52
                            ->arrayNode('items')
53
                                ->useAttributeAsKey('id')
54
                                ->defaultValue([])
55
                                ->prototype('array')
56
        ;
57
58
        $this->buildPathNode($menuNode);
59
60
        $menuNode
61
                        ->end()
62
                    ->end()
63
                ->end()
64
            ->end()
65
        ;
66
    }
67
68
    private function buildPathNode(NodeDefinition $node): void
69
    {
70
        $node
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...
71
            ->addDefaultsIfNotSet()
72
            ->children()
73
                ->scalarNode('label')->defaultNull()->end()
74
                ->scalarNode('label_catalogue')->defaultFalse()->end()
75
                ->scalarNode('icon')->defaultNull()->end()
76
                ->scalarNode('class')->defaultNull()->end()
77
                ->scalarNode('route')->defaultNull()->end()
78
                ->arrayNode('routeParams')
79
                    ->defaultValue([])
80
                    ->useAttributeAsKey('param')
81
                    ->prototype('scalar')->end()
82
                    ->validate()->ifTrue(static function ($element) {
83
                        return !\is_array($element);
84
                    })->thenInvalid('The routeParams element must be an array.')->end()
85
                ->end()
86
                ->variableNode('children')
87
                    ->defaultValue([])
88
                    ->validate()->ifTrue(static function ($element) {
89
                        return !\is_array($element);
90
                    })->thenInvalid('The children element must be an array.')->end()
91
                    ->validate()->always(function ($children) {
92
                        array_walk($children, [$this, 'evaluateChildren']);
93
94
                        return $children;
95
                    })->end()
96
                ->end()
97
            ->end()
98
            ;
99
    }
100
101
    private function evaluateChildren(array &$child, string $name): void
102
    {
103
        $child = $this->getPathNode($name)->finalize($child);
104
    }
105
106
    private function getPathNode(string $name = ''): NodeInterface
107
    {
108
        $treeBuilder = new TreeBuilder($name);
109
110
        $definition = $treeBuilder->getRootNode();
111
112
        \assert($definition instanceof ArrayNodeDefinition);
113
114
        $this->buildPathNode($definition);
115
116
        return $definition->getNode(true);
117
    }
118
}
119