Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 21ecad1

Browse filesBrowse files
committed
minor #9723 [Security] [Acl] [MaskBuilder] Refactor common code and reduce nesting (djlambert)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Security] [Acl] [MaskBuilder] Refactor common code and reduce nesting | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Refactored some common code and moved a comparison to an earlier statement. Commits ------- 7d85809 Refactor common code and reduce nesting
2 parents c0a7e1b + 7d85809 commit 21ecad1
Copy full SHA for 21ecad1

File tree

Expand file treeCollapse file tree

1 file changed

+33
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+33
-21
lines changed

‎src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php
+33-21Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,7 @@ public function __construct($mask = 0)
9696
*/
9797
public function add($mask)
9898
{
99-
if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) {
100-
$mask = constant($name);
101-
} elseif (!is_int($mask)) {
102-
throw new \InvalidArgumentException('$mask must be an integer.');
103-
}
104-
105-
$this->mask |= $mask;
99+
$this->mask |= $this->getMask($mask);
106100

107101
return $this;
108102
}
@@ -152,13 +146,7 @@ public function getPattern()
152146
*/
153147
public function remove($mask)
154148
{
155-
if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) {
156-
$mask = constant($name);
157-
} elseif (!is_int($mask)) {
158-
throw new \InvalidArgumentException('$mask must be an integer.');
159-
}
160-
161-
$this->mask &= ~$mask;
149+
$this->mask &= ~$this->getMask($mask);
162150

163151
return $this;
164152
}
@@ -191,19 +179,43 @@ public static function getCode($mask)
191179

192180
$reflection = new \ReflectionClass(get_called_class());
193181
foreach ($reflection->getConstants() as $name => $cMask) {
194-
if (0 !== strpos($name, 'MASK_')) {
182+
if (0 !== strpos($name, 'MASK_') || $mask !== $cMask) {
195183
continue;
196184
}
197185

198-
if ($mask === $cMask) {
199-
if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
200-
throw new \RuntimeException('There was no code defined for this mask.');
201-
}
202-
203-
return constant($cName);
186+
if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
187+
throw new \RuntimeException('There was no code defined for this mask.');
204188
}
189+
190+
return constant($cName);
205191
}
206192

207193
throw new \InvalidArgumentException(sprintf('The mask "%d" is not supported.', $mask));
208194
}
195+
196+
/**
197+
* Returns the mask for the passed code
198+
*
199+
* @param mixed $code
200+
*
201+
* @return integer
202+
*
203+
* @throws \InvalidArgumentException
204+
*/
205+
private function getMask($code)
206+
{
207+
if (is_string($code)) {
208+
if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) {
209+
throw new \InvalidArgumentException(sprintf('The code "%s" is not supported', $code));
210+
}
211+
212+
return constant($name);
213+
}
214+
215+
if (!is_int($code)) {
216+
throw new \InvalidArgumentException('$code must be an integer.');
217+
}
218+
219+
return $code;
220+
}
209221
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.