AppConfiguration::addPermissionsNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 30
cts 30
cp 1
rs 9.264
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\DependencyInjection;
12
13
use App\Enum\Functional\PermissionEnum;
14
use App\Enum\Functional\RoleEnum;
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
class AppConfiguration implements ConfigurationInterface
20
{
21 1
    public function getConfigTreeBuilder(): TreeBuilder
22
    {
23 1
        $treeBuilder = new TreeBuilder('app');
24
25 1
        $treeBuilder->getRootNode()
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 children() 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...
26 1
            ->isRequired()
27 1
            ->children()
28 1
                ->append($this->addPermissionsNode())
29 1
            ->end()
30
        ;
31
32 1
        return $treeBuilder;
33
    }
34
35 1
    private function addPermissionsNode(): NodeDefinition
36
    {
37 1
        $treeBuilder = new TreeBuilder('permissions');
38
39 1
        return $treeBuilder->getRootNode()
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 children() 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...
40 1
            ->isRequired()
41 1
            ->children()
42
43 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_CREATE))
44 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_READ))
45 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_UPDATE))
46 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_DELETE))
47
48 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_BREAK_CREATE))
49 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_BREAK_READ))
50 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_BREAK_UPDATE))
51 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_AVAILABILITY_BREAK_DELETE))
52
53 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_CREATE))
54 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_MEMBER_READ))
55 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_UPDATE))
56 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_DELETE))
57
58 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_NEED_CREATE))
59 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_MEMBER_NEED_READ))
60 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_NEED_UPDATE))
61 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBER_NEED_DELETE))
62
63 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBERSHIP_CREATE))
64 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_MEMBERSHIP_READ))
65 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBERSHIP_UPDATE))
66 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_MEMBERSHIP_DELETE))
67
68 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_RESERVATION_CREATE))
69 1
            ->append($this->addPermissionsBoolean(PermissionEnum::PARKING_RESERVATION_READ))
70 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_RESERVATION_UPDATE))
71 1
            ->append($this->addPermissionsForRoles(PermissionEnum::PARKING_RESERVATION_DELETE))
72
73 1
            ->end()
74
        ;
75
    }
76
77 1
    private function addPermissionsForRoles(string $name): NodeDefinition
78
    {
79 1
        $treeBuilder = new TreeBuilder($name);
80
81
        /* @noinspection NullPointerExceptionInspection */
82 1
        return $treeBuilder->getRootNode()
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 requiresAtLeastOneElement() 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...
83 1
            ->isRequired()
84 1
            ->requiresAtLeastOneElement()
85 1
            ->useAttributeAsKey('name')
86 1
            ->arrayPrototype()
87 1
                ->children()
88 1
                    ->arrayNode('roles')
89 1
                        ->requiresAtLeastOneElement()
90 1
                        ->enumPrototype()
91 1
                            ->values(RoleEnum::toArray())
92 1
                            ->defaultValue([])
93 1
                        ->end()
94 1
                    ->end()
95 1
                    ->booleanNode('self_only')
96 1
                        ->defaultValue(false)
97 1
                    ->end()
98 1
                ->end()
99 1
            ->end()
100
        ;
101
    }
102
103 1
    private function addPermissionsBoolean(string $name): NodeDefinition
104
    {
105 1
        $treeBuilder = new TreeBuilder($name);
106
107 1
        return $treeBuilder->getRootNode()
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 requiresAtLeastOneElement() 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...
108 1
            ->isRequired()
109 1
            ->requiresAtLeastOneElement()
110 1
            ->useAttributeAsKey('name')
111 1
            ->booleanPrototype()
112 1
                    ->isRequired()
113 1
                    ->defaultValue(false)
114 1
            ->end()
115
        ;
116
    }
117
}
118