Revert "Merge branch 'develop' of github.com:humhub/humhub into develop"

This reverts commit d91fae8d61bae6930890859684a1b0f3244254ae, reversing
changes made to 1d69a57f89e7df04b8ba1bf28be8aa7fa6405658.
This commit is contained in:
Yuriy Bakhtin 2023-09-19 15:30:35 +02:00
parent b693375952
commit ad8ffb84e3
12 changed files with 133 additions and 467 deletions

View File

@ -1,253 +0,0 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\exceptions;
use Throwable;
/**
* @since 1.15
*/
trait InvalidArgumentExceptionTrait
{
protected string $methodName;
protected ?string $parameter = null;
protected array $valid = [];
protected $given;
protected string $suffix = '';
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|string[] $valid (List of) valid parameter(s)
* @param mixed $given Parameter received
* @param int $code Optional exception code
* @param Throwable|null $previous Optional previous exception
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpMissingParamTypeInspection
*/
public function __construct($parameterOrMessage, $valid = null, $given = null, $code = null, $previous = null)
{
$exception = null;
$message = 'Invalid exception instantiation';
try {
if (!is_string($parameterOrMessage)) {
throw new InvalidArgumentTypeException('$parameterOrMessage', ['string'], $parameterOrMessage, 0, $this);
}
if (empty($parameterOrMessage = trim($parameterOrMessage))) {
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;
} 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) {
throw $t->setMethodName($this->methodName);
}
$this->given = $given;
$message = $this->formatMessage();
}
} catch (Throwable $exception) {
}
parent::__construct($message, $code, $previous);
if ($exception) {
/** @noinspection PhpUnhandledExceptionInspection */
throw $exception;
}
$this->isInstantiating = false;
}
/**
* @see static::__construct()
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
public static function newInstance($parameterOrMessage, $valid = null, $given = null, $code = null, $previous = null): self
{
return new static($parameterOrMessage, $valid, $given, $code, $previous);
}
protected function formatPrologue(): string
{
$int = filter_var($this->parameter, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($int === null) {
return $this->parameter === null
? 'Unknown argument'
: "Argument \$" . ltrim($this->parameter, '$');
}
return 'Argument #' . $int;
}
protected function formatValid(): string
{
return (count($this->valid) > 1
? 'one of '
: '') . implode(', ', $this->valid);
}
protected function formatGiven(): string
{
$given = $this->given ?? 'NULL';
/**
* @noinspection PhpLoopNeverIteratesInspection
* @noinspection LoopWhichDoesNotLoopInspection
*/
while (empty($given)) {
if ($given === '') {
$given = 'empty string';
break;
}
if ($given === '0') {
$given = "'0'";
break;
}
if ($given === []) {
$given = '[]';
break;
}
break;
}
if (!is_string($given)) {
try {
$given = json_encode($given, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$given = serialize($given);
}
}
return $given;
}
public function formatMessage(): string
{
return sprintf(
'%s passed to %s must be %s%s - %s given.',
$this->formatPrologue(),
$this->methodName,
$this->formatValid(),
$this->getSuffix(),
$this->formatGiven(),
);
}
protected function updateMessage(): self
{
if ($this->isInstantiating) {
return $this;
}
$this->message = $this->formatMessage();
return $this;
}
public function getGiven()
{
return $this->given;
}
public function getMethodName(): string
{
return $this->methodName;
}
public function setMethodName(string $methodName): self
{
$this->methodName = $methodName;
return $this->updateMessage();
}
public function getName(): string
{
if (method_exists(parent::class, 'getName')) {
return parent::getName() . " value";
}
return 'Invalid value';
}
public function getParameter(): ?string
{
return $this->parameter;
}
public function setParameter(?string $parameter): InvalidArgumentExceptionTrait
{
$this->parameter = $parameter;
return $this->updateMessage();
}
public function getSuffix(): string
{
return $this->suffix;
}
public function setSuffix(string $suffix): self
{
$this->suffix = $suffix;
return $this->updateMessage();
}
public function getValid(): array
{
return $this->valid;
}
public function setValid($valid): self
{
if (is_string($valid)) {
$this->valid = [$valid];
return $this;
}
if (is_iterable($valid)) {
foreach ($valid as $key => $value) {
try {
$this->valid[] = (string)($value ?? 'NULL');
} catch (\Error $t) {
throw new InvalidArgumentTypeException(sprintf("\$valid[%s]", $key), ['string'], $value, 0, $this);
}
}
return $this;
}
throw new InvalidArgumentTypeException('$valid', ['string', 'string[]'], $valid, 0, $this);
}
}

View File

@ -1,6 +1,6 @@
<?php
/*
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
@ -8,41 +8,28 @@
namespace humhub\exceptions;
use yii\base\InvalidArgumentException as BaseInvalidArgumentException;
use yii\base\InvalidArgumentException;
/**
* @since 1.15
*/
class InvalidArgumentTypeException extends BaseInvalidArgumentException
class InvalidArgumentTypeException extends InvalidArgumentException
{
use InvalidArgumentExceptionTrait {
getName as protected InvalidArgumentExceptionTrait_getName;
formatGiven as protected InvalidArgumentExceptionTrait_formatGiven;
formatValid as protected InvalidArgumentExceptionTrait_formatValid;
}
use InvalidTypeExceptionTrait;
protected function formatValid(): string
protected function formatPrologue(array $constructArguments): string
{
if (empty($this->valid)) {
$this->valid = ['mixed'];
}
$argumentName = is_array($this->parameter)
? reset($this->parameter)
: null;
$argumentNumber = is_array($this->parameter)
? key($this->parameter)
: $this->parameter;
return (count($this->valid) > 1
? 'one of the following types: '
: 'of type ') . implode(', ', $this->valid);
}
$argumentName = $argumentName === null
? ''
: " \$" . ltrim($argumentName, '$');
protected function formatGiven(): string
{
return $this->given === null ? 'NULL' : get_debug_type($this->given);
}
public function getName(): string
{
if (method_exists(parent::class, 'getName')) {
return $this->InvalidArgumentExceptionTrait_getName() . " Type";
}
return 'Invalid Type';
return sprintf('Argument #%d%s', $argumentNumber, $argumentName);
}
}

View File

@ -1,19 +0,0 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\exceptions;
use yii\base\InvalidArgumentException as BaseInvalidArgumentException;
/**
* @since 1.15
*/
class InvalidArgumentValueException extends BaseInvalidArgumentException
{
use InvalidArgumentExceptionTrait;
}

View File

@ -1,6 +1,6 @@
<?php
/*
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
@ -15,35 +15,10 @@ use yii\base\InvalidConfigException;
*/
class InvalidConfigTypeException extends InvalidConfigException
{
use InvalidArgumentExceptionTrait;
use InvalidTypeExceptionTrait;
protected function formatPrologue(): string
protected function formatPrologue(array $constructArguments): string
{
return "Parameter '$this->parameter' of configuration";
}
protected function formatValid(): string
{
if (empty($this->valid)) {
$this->valid = ['mixed'];
}
return (count($this->valid) > 1
? 'one of the following type '
: 'of type ') . implode(', ', $this->valid);
}
protected function formatGiven(): string
{
return get_debug_type($this->given);
}
public function getName(): string
{
if (method_exists(parent::class, 'getName')) {
return parent::getName() . " Type";
}
return 'Invalid Type';
return "Parameter $this->parameter of configuration";
}
}

View File

@ -0,0 +1,75 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\exceptions;
/**
* @since 1.15
*/
trait InvalidTypeExceptionTrait
{
// public properties
public string $methodName;
public $parameter;
public array $validType = [];
/**
* @var mixed|null
*/
public $givenValue;
/**
* @param string $method
* @param int|array $parameter = [
* int => string, // position, or [ position => name ] of the argument
* ]
* @param array|string|null $validType
* @param null $givenValue
*/
public function __construct(
$method = '',
$parameter = null,
$validType = [],
$givenValue = null,
$nullable = false,
$code = 0,
$previous = null
) {
$this->methodName = $method;
$this->parameter = $parameter;
$this->validType = (array)($validType ?? ['mixed']);
$this->givenValue = $givenValue;
if ($nullable && !in_array('null', $this->validType, true)) {
$this->validType[] = 'null';
}
$message = sprintf(
'%s passed to %s must be of type %s, %s given.',
$this->formatPrologue(func_get_args()),
$this->methodName,
implode(', ', $this->validType),
get_debug_type($this->givenValue)
);
parent::__construct($message, $code, $previous);
}
abstract protected function formatPrologue(array $constructArguments): string;
public function getName(): string
{
if (method_exists(parent::class, 'getName')) {
return parent::getName() . " Type";
}
return 'Invalid Type';
}
}

View File

@ -286,7 +286,8 @@ abstract class BaseSettingsManager extends Component
}
} elseif (!is_array($prefix)) {
throw new InvalidArgumentTypeException(
'$prefix',
__METHOD__,
[1 => '$prefix'],
['string', 'int', 'null', \Stringable::class],
$prefix
);

View File

@ -1,6 +1,6 @@
<?php
/*
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
@ -8,8 +8,8 @@
namespace humhub\libs;
use humhub\exceptions\InvalidArgumentValueException;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\Exception;
/**
@ -19,6 +19,7 @@ use yii\base\Exception;
*/
class Helpers
{
/**
* Shorten a text string
*
@ -137,9 +138,8 @@ class Helpers
* Source: http://php.net/manual/en/function.ini-get.php#96996
*
* @param string $valueString
*
* @return int bytes
* @throws InvalidArgumentValueException
* @throws InvalidParamException
*/
public static function getBytesOfIniValue($valueString)
{
@ -148,7 +148,7 @@ class Helpers
}
if ($valueString === false) {
throw new InvalidArgumentValueException('Your configuration option of ini_get function does not exist.');
throw new InvalidArgumentException('Your configuration option of ini_get function does not exist.');
}
switch (substr($valueString, -1)) {
@ -263,4 +263,5 @@ class Helpers
}
}
}
}

View File

@ -19,7 +19,6 @@ use humhub\modules\content\models\ContentContainerTagRelation;
use humhub\modules\user\models\User;
use humhub\modules\user\Module as UserModule;
use Yii;
use yii\db\ActiveQuery;
use yii\helpers\Url;
use yii\web\IdentityInterface;
@ -32,30 +31,18 @@ use yii\web\IdentityInterface;
* @property integer $id
* @property integer $visibility
* @property string $guid
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
* @property integer $contentcontainer_id
* @property ContentContainer $contentContainerRecord
* @property ContentContainerPermissionManager $permissionManager
* @property ContentContainerSettingsManager $settings
* @property-read string[] $blockedUserGuids
* @property-read int[] $blockedUserIds
* @property-read int $defaultContentVisibility
* @property-read string $displayName
* @property-read string|mixed $displayNameSub
* @property-read ContentContainerModuleManager $moduleManager
* @property-read ProfileBannerImage $profileBannerImage
* @property-read ProfileImage $profileImage
* @property-read string[] $tags
* @property-read string $wallOut
* @property ContentContainer $contentContainerRecord
*
* @since 1.0
* @noinspection PropertiesInspection
* @author Luke
*/
abstract class ContentContainerActiveRecord extends ActiveRecord
{
/**
* @var ContentContainerPermissionManager
*/
@ -106,7 +93,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
* @return string
* @since 0.11.0
*/
abstract public function getDisplayName(): string;
public abstract function getDisplayName(): string;
/**
* Returns a descriptive sub title of this container used in the frontend.
@ -114,7 +101,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
* @return mixed
* @since 1.4
*/
abstract public function getDisplayNameSub(): string;
public abstract function getDisplayNameSub(): string;
/**
* Returns the Profile Image Object for this Content Base
@ -221,7 +208,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
public function afterSave($insert, $changedAttributes)
{
if ($insert) {
$contentContainer = new ContentContainer();
$contentContainer = new ContentContainer;
$contentContainer->guid = $this->guid;
$contentContainer->class = static::class;
$contentContainer->pk = $this->getPrimaryKey();
@ -264,7 +251,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
/**
* Returns the related ContentContainer model (e.g. Space or User)
*
* @return ActiveQuery
* @return ContentContainer
* @see ContentContainer
*/
public function getContentContainerRecord()
@ -473,4 +460,5 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
return $userModule->allowBlockUsers();
}
}

View File

@ -23,10 +23,10 @@ use yii\db\ActiveRecord;
* @property integer $owner_user_id
* @property string $tags_cached readonly, a comma separted list of assigned tags
* @mixin PolymorphicRelation
* @noinspection PropertiesInspection
*/
class ContentContainer extends ActiveRecord
{
/**
* @inheritdoc
*/

View File

@ -32,34 +32,33 @@ use humhub\modules\user\models\GroupSpace;
use humhub\modules\user\models\Invite;
use humhub\modules\user\models\User;
use Yii;
use yii\db\ActiveQuery;
/**
* This is the model class for table "space".
*
* @property integer $id
* @property string $guid
* @property string $name
* @property string $description
* @property string $about
* @property string $url
* @property integer $join_policy
* @property integer $visibility
* @property integer $status
* @property integer $sort_order
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
* @property integer $auto_add_new_members
* @property integer $contentcontainer_id
* @property integer $default_content_visibility
* @property string $color
* @property User $ownerUser the owner of this space
* @property-read AdvancedSettings $advancedSettings
* @property-read mixed $applicants
* @property-read GroupSpace[] $groupSpaces
* @property-read Membership[] $memberships
* @property-read mixed $nonMembershipUser
* @property-read array $privilegedGroupUsers
* @property-read array $searchAttributes
*
* @mixin \humhub\components\behaviors\GUID
* @mixin \humhub\modules\space\behaviors\SpaceModelMembership
* @mixin \humhub\modules\user\behaviors\Followable
* @noinspection PropertiesInspection
*/
class Space extends ContentContainerActiveRecord implements Searchable
{
@ -537,7 +536,7 @@ class Space extends ContentContainerActiveRecord implements Searchable
* Membership::getSpaceMembersQuery($this->space)->active()->visible()->count()
* ```
*
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getMemberships()
{
@ -579,7 +578,7 @@ class Space extends ContentContainerActiveRecord implements Searchable
}
/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getOwnerUser()
{
@ -693,7 +692,7 @@ class Space extends ContentContainerActiveRecord implements Searchable
/**
* Gets query for [[GroupSpace]].
*
* @return ActiveQuery
* @return \yii\db\ActiveQuery
* @since 1.8
*/
public function getGroupSpaces()

View File

@ -1,6 +1,6 @@
<?php
/*
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
@ -42,20 +42,29 @@ use yii\web\IdentityInterface;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $guid
* @property integer $status
* @property string $username
* @property string $email
* @property string $auth_mode
* @property string $language
* @property string $time_zone
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
* @property string $last_login
* @property string $authclient_id
* @property string $auth_key
* @property integer $visibility
* @property integer $contentcontainer_id
* @property Profile $profile
* @property Password $currentPassword
* @property Auth[] $auths
* @property string $displayName
* @property string $displayNameSub
* @mixin Followable
* @noinspection PropertiesInspection
*/
class User extends ContentContainerActiveRecord implements IdentityInterface, Searchable
{

View File

@ -1,97 +0,0 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2018 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\tests\codeception\unit\exceptions;
use Codeception\Test\Unit;
use humhub\exceptions\InvalidArgumentTypeException;
use humhub\exceptions\InvalidArgumentValueException;
use yii\base\BaseObject;
/**
* Class MimeHelperTest
*/
class InvalidArgumentExceptionTest extends Unit
{
public function testInvalidArgumentValueExceptionMessageCase1()
{
$message = 'Hello World';
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException($message);
}
public function testInvalidArgumentValueExceptionMessageCase2()
{
$message = 'Hello World';
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(1);
throw new InvalidArgumentValueException($message, 1);
}
public function testInvalidArgumentValueExceptionParameterCase1()
{
$message = 'Argument $parameter passed to ' . __METHOD__ . ' must be bool - 3 given.';
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException('parameter', 'bool', 3);
}
public function testInvalidArgumentValueExceptionParameterCase2()
{
$message = 'Argument $parameter passed to ' . __METHOD__ . ' must be bool - NULL given.';
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException('parameter', 'bool');
}
public function testInvalidArgumentValueExceptionParameterCase3()
{
$message = 'Argument $parameter passed to ' . __METHOD__ . ' must be one of bool, NULL - 2 given.';
$this->expectException(InvalidArgumentValueException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException('parameter', ['bool', null], 2);
}
public function testInvalidArgumentValueExceptionParameterCase4()
{
$message = 'Argument $valid passed to ' . __METHOD__ . ' must be one of the following types: string, string[] - NULL given.';
$this->expectException(InvalidArgumentTypeException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException('parameter');
}
public function testInvalidArgumentValueExceptionParameterCase5()
{
$message = 'Argument $valid[1] passed to ' . __METHOD__ . ' must be of type string - yii\base\BaseObject given.';
$this->expectException(InvalidArgumentTypeException::class);
$this->expectExceptionMessage($message);
$this->expectExceptionCode(0);
throw new InvalidArgumentValueException('parameter', ['bool', new BaseObject()]);
}
}