Skip to content
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

Enh: Code de-duplication by introducing PermissionManager::handlePermissionStateChange() #6750

Merged
merged 12 commits into from
Jan 19, 2024
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HumHub Changelog

1.16.0 (Unreleased)
-------------------
- Enh #6750: Code de-duplication by introducing `PermissionManager::handlePermissionStateChange()`
- Fix #6772: Polymorphic relation lookup (Regression #6587)
- Enh #6745: Harmonise term `enabled/disabled` vs `active/inactive` for modules
- Fix #6754: Regression due to return type (#6550)
Expand Down
4 changes: 2 additions & 2 deletions protected/humhub/components/behaviors/PolymorphicRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Exception;
use humhub\components\ActiveRecord;
use humhub\libs\Helpers;
use humhub\helpers\DataTypeHelper;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use ReflectionClass;
Expand Down Expand Up @@ -156,7 +156,7 @@ private function validateUnderlyingObjectType(?object $object)
return true;
}

if (Helpers::checkClassType($object, $this->mustBeInstanceOf, false)) { //|| $object->asa($instance) !== null
if (DataTypeHelper::matchClassType($object, $this->mustBeInstanceOf)) { //|| $object->asa($instance) !== null
return true;
}

Expand Down
49 changes: 35 additions & 14 deletions protected/humhub/exceptions/InvalidArgumentExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ trait InvalidArgumentExceptionTrait
protected bool $isInstantiating = true;

/**
* @param string $parameterOrMessage Name of parameter in question, or alternatively the full message string containing at
* least one space character (ASCII 32). In this case, `$valid` and `$given` are considered to be
* `$code` and `$previous` respectively
* @param string $parameterOrMessage Name of parameter in question, or alternatively the full message string
* containing at least one space character (ASCII 32). In this case, `$valid` and `$given` are considered to be
* `$code` and `$previous` respectively
* @param string|string[] $valid (List of) valid parameter(s)
* @param mixed $given Parameter received
* @param int $code Optional exception code
Expand All @@ -41,25 +41,46 @@ public function __construct($parameterOrMessage, $valid = null, $given = null, $

try {
if (!is_string($parameterOrMessage)) {
throw new InvalidArgumentTypeException('$parameterOrMessage', ['string'], $parameterOrMessage, 0, $this);
throw new InvalidArgumentTypeException(
'$parameterOrMessage',
['string'],
$parameterOrMessage,
0,
$this
);
}

if (empty($parameterOrMessage = trim($parameterOrMessage))) {
throw new InvalidArgumentValueException('$parameterOrMessage', 'non-empty string', $parameterOrMessage, 0, $this);
throw new InvalidArgumentValueException(
'$parameterOrMessage',
'non-empty string',
$parameterOrMessage,
0,
$this
);
}

// check if $parameter is actually the $message
if (strpos($parameterOrMessage, ' ') !== false) {
$message = $parameterOrMessage;
$code = $code ?? $valid ?? 0;
$previous = $previous ?? $given;
$code ??= is_int($valid) ? $valid : 0;
if ($given instanceof Throwable) {
$previous ??= $given;
}
} else {
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$trace = end($trace);
$this->methodName = ltrim(($trace['class'] ?? '') . '::' . ($trace['function'] ?? 'unknown method'), ':');

$this->parameter = $parameterOrMessage;

if (false !== $pos = strrpos($parameterOrMessage, '::')) {
$this->methodName = trim(substr($parameterOrMessage, 0, $pos), ':');
$this->parameter = trim(substr($parameterOrMessage, $pos), ':');
} else {
$trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$trace = end($trace);
$this->methodName = ltrim(
($trace['class'] ?? '') . '::' . ($trace['function'] ?? 'unknown method'),
':'
);

$this->parameter = $parameterOrMessage;
}
try {
$this->setValid($valid);
} catch (InvalidArgumentTypeException $t) {
Expand All @@ -84,7 +105,7 @@ public function __construct($parameterOrMessage, $valid = null, $given = null, $
}

/**
* @see static::__construct()
* @see static::__construct()
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
Expand Down