Skip to content

Commit

Permalink
Change ensure* methods' return type to void
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-rueegg committed Jan 18, 2024
1 parent e2bca32 commit 0d4298b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
14 changes: 8 additions & 6 deletions protected/humhub/helpers/DataTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ protected static function matchTypeHelper($typeToCheck, &$input, string $inputTy
* @throws InvalidArgumentTypeException|InvalidArgumentClassException|InvalidArgumentValueException
* @since 1.16
*/
public static function ensureClassType($value, $allowedTypes): string
`` public static function ensureClassType($value, $allowedTypes): void
{
return self::matchClassType($value, $allowedTypes, true);
self::matchClassType($value, $allowedTypes, true);
}

/**
Expand All @@ -238,9 +238,9 @@ public static function ensureClassType($value, $allowedTypes): string
* @see InvalidArgumentTypeException
* @throws InvalidArgumentTypeException|InvalidArgumentValueException
*/
public static function ensureType($value, $allowedTypes): string
public static function ensureType($value, $allowedTypes): void
{
return self::matchType($value, $allowedTypes, true);
self::matchType($value, $allowedTypes, true);
}

/**
Expand Down Expand Up @@ -300,13 +300,15 @@ public static function filterBool($value, ?bool $strict = false, bool $throwExce
}

/**
* Checks if the class or object has one of the given classes, interfaces or traits as one of its parents or implements it.
* Checks if the class or object has one of the given classes, interfaces or traits as one of its parents or
* implements it.
*
* @param string|object|null|mixed $value Object or classname to be checked. Null may be valid if included in
* $type. Everything else is invalid and either throws an error (default) or returns NULL, if $throw is false.
* @param string|string[]|object[] $allowedTypes (List of) allowed class, interface or trait names, or object
* instances. Object instances may only be passed as part of an array. In such a case, the object's type/class
* is used for comparison. If a string is provided, it will be split by `|`. If NULL value or the "NULL" string
* is used for comparison. If a string is provided, it will be split by `|`. If NULL value or the "NULL"
* string
* is included, NULL values are also allowed. *
* @param bool $throwException throws an exception instead of returning `null`.
* Code of the thrown Exception is a bit-mask consisting of the following bits:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function beforeAction($action)
$modelPk = (int)Yii::$app->request->get('objectId', Yii::$app->request->post('objectId'));

/** @var Comment|ContentActiveRecord $modelClass */
$modelClass = DataTypeHelper::ensureClassType($modelClass, [Comment::class, ContentActiveRecord::class]);
$modelClass = DataTypeHelper::matchClassType($modelClass, [Comment::class, ContentActiveRecord::class], true);
$this->target = $modelClass::findOne(['id' => $modelPk]);

if (!$this->target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ public function beforeAction($action)
}

/** @var ContentAddonActiveRecord|ContentActiveRecord $modelClass */
$modelClass = DataTypeHelper::ensureClassType(
$modelClass = DataTypeHelper::matchClassType(
$modelClass,
[ContentAddonActiveRecord::class, ContentActiveRecord::class]
[ContentAddonActiveRecord::class, ContentActiveRecord::class],
true
);
$target = $modelClass::findOne(['id' => $pk]);

Expand Down
2 changes: 1 addition & 1 deletion protected/humhub/modules/file/actions/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function loadRecord()


/** @var ActiveRecord|string $model */
if ($model != '' && $pk != '' && $model = DataTypeHelper::ensureClassType($model, ActiveRecord::class)) {
if ($model != '' && $pk != '' && $model = DataTypeHelper::matchClassType($model, ActiveRecord::class, true)) {
$record = $model::findOne(['id' => $pk]);
if ($record !== null && ($record instanceof ContentActiveRecord || $record instanceof ContentAddonActiveRecord)) {
if ($record->content->canEdit()) {
Expand Down
2 changes: 1 addition & 1 deletion protected/humhub/modules/user/models/ProfileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function getFieldType(): ?BaseType

if (
$this->field_type_class != ''
&& $type = DataTypeHelper::ensureClassType($this->field_type_class, fieldtype\BaseType::class)
&& $type = DataTypeHelper::matchClassType($this->field_type_class, fieldtype\BaseType::class, true)
) {
$this->_fieldType = new $type();
$this->_fieldType->setProfileField($this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function getTypeInstances($profileField = null)
{
$types = [];
foreach ($this->getFieldTypes() as $className => $title) {
$className = DataTypeHelper::ensureClassType($className, static::class);
$className = DataTypeHelper::matchClassType($className, static::class, true);
/** @var BaseType $instance */
$instance = new $className();
if ($profileField !== null) {
Expand Down

0 comments on commit 0d4298b

Please sign in to comment.