Issues (2551)

src/Authorization/PermissionTrait.php (1 issue)

1
<?php
2
3
namespace Jabe\Authorization;
4
5
trait PermissionTrait
6
{
7
    private $name;
8
    private $id;
9
10
    public function getName(): string
11
    {
12
        return $this->name;
13
    }
14
15
    public function getValue(): int
16
    {
17
        return $this->id;
18
    }
19
20
    public static function forName(string $name): PermissionInterface
21
    {
22
        $names = explode("_", strtolower($name));
23
        $func = $names[0];
24
        if (count($names) > 1) {
25
            for ($i = 1; $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...
26
                $func .= ucfirst($names[$i]);
27
            }
28
        }
29
30
        return self::$func();
31
    }
32
33
    public static function values(): array
34
    {
35
        $reflection = new \ReflectionClass(__CLASS__);
36
        $methods = $reflection->getMethods(\ReflectionMethod::IS_STATIC);
37
        $arr = [];
38
39
        foreach ($methods as $method) {
40
            if ($method->getName() !== "values" && $method->getName() !== "forName" && $method->getName() !== "resources") {
41
                $arr[] = $method->invoke(null);
42
            }
43
        }
44
        return $arr;
45
    }
46
}
47