Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] [Acl] [MaskBuilder] Refactor common code and reduce nesting #9723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ public function __construct($mask = 0)
*/
public function add($mask)
{
if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) {
$mask = constant($name);
} elseif (!is_int($mask)) {
throw new \InvalidArgumentException('$mask must be an integer.');
}

$this->mask |= $mask;
$this->mask |= $this->getMask($mask);

return $this;
}
Expand Down Expand Up @@ -152,13 +146,7 @@ public function getPattern()
*/
public function remove($mask)
{
if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) {
$mask = constant($name);
} elseif (!is_int($mask)) {
throw new \InvalidArgumentException('$mask must be an integer.');
}

$this->mask &= ~$mask;
$this->mask &= ~$this->getMask($mask);

return $this;
}
Expand Down Expand Up @@ -191,19 +179,43 @@ public static function getCode($mask)

$reflection = new \ReflectionClass(get_called_class());
foreach ($reflection->getConstants() as $name => $cMask) {
if (0 !== strpos($name, 'MASK_')) {
if (0 !== strpos($name, 'MASK_') || $mask !== $cMask) {
continue;
}

if ($mask === $cMask) {
if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
throw new \RuntimeException('There was no code defined for this mask.');
}

return constant($cName);
if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
throw new \RuntimeException('There was no code defined for this mask.');
}

return constant($cName);
}

throw new \InvalidArgumentException(sprintf('The mask "%d" is not supported.', $mask));
}

/**
* Returns the mask for the passed code
*
* @param mixed $code
*
* @return integer
*
* @throws \InvalidArgumentException
*/
private function getMask($code)
{
if (is_string($code)) {
if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) {
throw new \InvalidArgumentException(sprintf('The code "%s" is not supported', $code));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we merge these two nested if statements into one? This is how it used to be before extraction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, though it seemed confusing to me passing a bad string value and getting an exception saying it must be an integer. Changing the remaining exception's message to '$code must be a valid code or integer.' would remedy that somewhat.


return constant($name);
}

if (!is_int($code)) {
throw new \InvalidArgumentException('$code must be an integer.');
}

return $code;
}
}