Skip to content

Commit

Permalink
fix: fix some bugs from scrutinizer
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Aug 31, 2023
1 parent abb7a82 commit 89ab3e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/CoreEnforcer.php
Expand Up @@ -360,7 +360,9 @@ public function loadPolicy(): void
$newModel->clearPolicy();

try {
$this->adapter->loadPolicy($newModel);
if (!is_null($this->adapter)){
$this->adapter->loadPolicy($newModel);
}
$newModel->printPolicy();
$newModel->sortPoliciesBySubjectHierarchy();
$newModel->sortPoliciesByPriority();
Expand Down Expand Up @@ -463,7 +465,9 @@ public function savePolicy(): void
throw new CasbinException('cannot save a filtered policy');
}

$this->adapter->savePolicy($this->model);
if (!is_null($this->adapter)){
$this->adapter->savePolicy($this->model);
}

if ($this->watcher !== null && $this->autoNotifyWatcher) {
if ($this->watcher instanceof WatcherEx) {
Expand Down
5 changes: 2 additions & 3 deletions src/Util/BuiltinOperations.php
Expand Up @@ -251,14 +251,13 @@ public static function keyMatch4(string $key1, string $key2): bool
$pattern,
function ($m) use (&$tokens) {
$tokens[] = $m[1];

return '([^\/]+)';
},
$key2
);

$matched = preg_match_all('~^' . $key2 . '$~', $key1, $matches);
if (!$matched) {
if (!boolval($matched)) {
return false;
}

Expand Down Expand Up @@ -302,7 +301,7 @@ public static function keyMatch4Func(...$args): bool
public static function keyMatch5(string $key1, string $key2): bool
{
$pos = strpos($key1, '?');
if ($pos == false) {
if ($pos === false) {
return $key1 == $key2;
}

Expand Down
9 changes: 2 additions & 7 deletions src/Util/Util.php
Expand Up @@ -21,7 +21,6 @@ class Util
* @param string $s
*
* @return string
* @throws CasbinException
*/
public static function escapeAssertion(string $s): string
{
Expand All @@ -32,15 +31,11 @@ public static function escapeAssertion(string $s): string
}
}

$s = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)((r|p)[0-9]*)(\.)~", function ($m) {
$ss = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)((r|p)[0-9]*)(\.)~", function ($m) {
return $m[1] . $m[2] . '_';
}, $s);

if (null === $s) {
throw new CasbinException(sprintf('Unable to escape assertion "%s"', $s));
}

return $s;
return is_string($ss) ? $ss : $s;
}

/**
Expand Down

0 comments on commit 89ab3e8

Please sign in to comment.