Test Failed
Branch main (8f4107)
by Bingo
08:27 queued 02:15
created

PermissionConverter::getNamesForPermissions()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
rs 8.0555
cc 9
nc 5
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\ProcessEngineConfiguration;
6
use Jabe\Engine\Authorization\{
7
    AuthorizationInterface,
8
    PermissionInterface,
9
    Permissions
10
};
11
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
12
13
class PermissionConverter
14
{
15
    public static function getPermissionsForNames(array $names, int $resourceType, ProcessEngineConfiguration $engineConfiguration): array
16
    {
17
        $permissions = [];
18
19
        for ($i = 0; i < count($names); $i += 1) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Bug introduced by
The constant Jabe\Engine\Impl\Util\i was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
20
            $permissions[] = $engineConfiguration->getPermissionProvider()->getPermissionForName($names[$i], $resourceType);
0 ignored issues
show
Bug introduced by
The method getPermissionProvider() does not exist on Jabe\Engine\ProcessEngineConfiguration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            $permissions[] = $engineConfiguration->/** @scrutinizer ignore-call */ getPermissionProvider()->getPermissionForName($names[$i], $resourceType);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
        }
22
23
        return $permissions;
24
    }
25
26
    public static function getNamesForPermissions(AuthorizationInterface $authorization, array $permissions): array
27
    {
28
        $type = $authorization->getAuthorizationType();
29
30
        // special case all permissions are granted
31
        if (
32
            ($type == AuthorizationInterface::AUTH_TYPE_GLOBAL || $type == AuthorizationInterface::AUTH_TYPE_GRANT)
33
            && $authorization->isEveryPermissionGranted()
34
        ) {
35
            return [ Permissions::all()->getName() ];
36
        }
37
38
        // special case all permissions are revoked
39
        if ($type == Authorization::AUTH_TYPE_REVOKE && $authorization->isEveryPermissionRevoked()) {
40
            return [ Permissions::all()->getName() ];
41
        }
42
43
        $names = [];
44
45
        foreach ($permissions as $permission) {
46
            $name = $permission->getName();
47
            // filter NONE and ALL from permissions array
48
            if ($name != Permissions::none()->getName() && $name != Permissions::all()->getName()) {
49
                $names[] = $name;
50
            }
51
        }
52
53
        return $names;
54
    }
55
}
56