8000 Refactor common code and reduce nesting · symfony/symfony@7d85809 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d85809

Browse files
committed
Refactor common code and reduce nesting
1 parent 5e37fc8 commit 7d85809

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

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

Lines changed: 33 additions & 21 deletions
< 8000 td data-grid-cell-id="diff-3f63f1c061a01a37e840d894d08e4da68bfb60dd911a238f03ea9538f4b5008f-101-98-2" data-line-anchor="diff-3f63f1c061a01a37e840d894d08e4da68bfb60dd911a238f03ea9538f4b5008fL101" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionLine-bgColor, var(--diffBlob-deletion-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell left-side-diff-cell border-right left-side">-
} elseif (!is_int($mask)) {
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
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