These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminBundle\Helper\Security\Acl\Permission; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use Symfony\Component\Security\Acl\Permission\AbstractMaskBuilder; |
||
7 | |||
8 | /** |
||
9 | * This class allows you to build cumulative permissions easily, or convert |
||
10 | * masks to a human-readable format. |
||
11 | * |
||
12 | * @see Symfony\Component\Security\Acl\Permission\MaskBuilder |
||
13 | */ |
||
14 | class MaskBuilder extends AbstractMaskBuilder |
||
15 | { |
||
16 | const MASK_VIEW = 1; // 1 << 0 |
||
0 ignored issues
–
show
|
|||
17 | const MASK_EDIT = 4; // 1 << 2 |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
50% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
18 | const MASK_DELETE = 8; // 1 << 3 |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
50% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
19 | const MASK_PUBLISH = 16; // 1 << 4 |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
50% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
20 | const MASK_UNPUBLISH = 32; // 1 << 5 |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
50% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
21 | const MASK_IDDQD = 1073741823; // 1 << 0 | 1 << 1 | ... | 1 << 30 |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
50% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them.
Loading history...
|
|||
22 | |||
23 | const CODE_VIEW = 'V'; |
||
24 | const CODE_EDIT = 'E'; |
||
25 | const CODE_DELETE = 'D'; |
||
26 | const CODE_PUBLISH = 'P'; |
||
27 | const CODE_UNPUBLISH = 'U'; |
||
28 | |||
29 | const ALL_OFF = '................................'; |
||
30 | const OFF = '.'; |
||
31 | const ON = '*'; |
||
32 | |||
33 | /** |
||
34 | * Returns a human-readable representation of the permission |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getPattern() |
||
39 | { |
||
40 | $pattern = self::ALL_OFF; |
||
41 | $length = strlen($pattern); |
||
42 | $bitmask = str_pad(decbin($this->mask), $length, '0', STR_PAD_LEFT); |
||
43 | |||
44 | for ($i = $length - 1; $i >= 0; --$i) { |
||
45 | if ('1' === $bitmask[$i]) { |
||
46 | try { |
||
47 | $pattern[$i] = self::getCode(1 << ($length - $i - 1)); |
||
48 | } catch (\Exception $notPredefined) { |
||
49 | $pattern[$i] = self::ON; |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return $pattern; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Returns the code for the passed mask |
||
59 | * |
||
60 | * @param null|int $mask |
||
61 | * |
||
62 | * @throws InvalidArgumentException |
||
63 | * @throws \RuntimeException |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | public static function getCode($mask) |
||
68 | { |
||
69 | if (!is_int($mask)) { |
||
70 | throw new InvalidArgumentException('$mask must be an integer.'); |
||
71 | } |
||
72 | |||
73 | $reflection = new \ReflectionClass(get_called_class()); |
||
74 | foreach ($reflection->getConstants() as $name => $cMask) { |
||
75 | if (0 !== strpos($name, 'MASK_')) { |
||
76 | continue; |
||
77 | } |
||
78 | |||
79 | if ($mask === $cMask) { |
||
80 | if (!defined($cName = 'static::CODE_'.substr($name, 5))) { |
||
81 | throw new \RuntimeException('There was no code defined for this mask.'); |
||
82 | } |
||
83 | |||
84 | return constant($cName); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | throw new InvalidArgumentException(sprintf('The mask "%d" is not supported.', $mask)); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Checks if a specific permission or mask value is set in the current mask |
||
93 | * |
||
94 | * @param string|int $mask |
||
95 | * |
||
96 | * @throws InvalidArgumentException |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function has($mask) |
||
101 | { |
||
102 | View Code Duplication | if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
103 | $mask = constant($name); |
||
104 | } elseif (!is_int($mask)) { |
||
105 | throw new InvalidArgumentException('$mask must be an integer.'); |
||
106 | } |
||
107 | |||
108 | return ($this->mask & $mask) != 0; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns the mask for the passed code. |
||
113 | * |
||
114 | * @param mixed $code |
||
115 | * |
||
116 | * @return int |
||
117 | * |
||
118 | * @throws \InvalidArgumentException |
||
119 | */ |
||
120 | public function resolveMask($code) |
||
121 | { |
||
122 | View Code Duplication | if (is_string($code)) { |
|
123 | if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) { |
||
124 | throw new \InvalidArgumentException(sprintf('The code "%s" is not supported', $code)); |
||
125 | } |
||
126 | |||
127 | return constant($name); |
||
128 | } |
||
129 | |||
130 | if (!is_int($code)) { |
||
131 | throw new \InvalidArgumentException('$code must be an integer.'); |
||
132 | } |
||
133 | |||
134 | return $code; |
||
135 | } |
||
136 | } |
||
137 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.