Skip to content

Conversation

@RuslanSamburov
Copy link

No description provided.


foreach ($response as $key => $value) {
$widgetModel->$key = $value;
$widgetModel->$key = $widget->$key;
Copy link
Collaborator

Choose a reason for hiding this comment

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

а что именно за ошибка была?

Copy link
Collaborator

Choose a reason for hiding this comment

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

в других методах нет подобной ошибки?

Copy link
Author

Choose a reason for hiding this comment

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

а что именно за ошибка была?

Ошибка типизации, когда Widgets::install пытался пропихнуть массив в эту функцию

public function setSettingsTemplate(?SettingsTemplatesCollection $settingsTemplate)

Когда эта функция ожидает объект

Copy link
Author

Choose a reason for hiding this comment

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

в других методах нет подобной ошибки?

Похожий функционал нашел в Widgets::uninstall и Webhooks::subscribe

Метод Widgets::uninstall переделал и теперь он возвращает bool
Метод Webhooks::subscribe реализовал также как и Widgets::install

В других сервисах похожего функционала не нашел что могло бы в будущем вызвать такую ошибку

Copy link
Author

Choose a reason for hiding this comment

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

Или как вариант, можно изменить метод __set в классе BaseApiModel на

public function __set($name, $value)
{
$methodName = 'set' . Str::camel(Str::ucfirst($name));

if (method_exists($this, $methodName) && is_callable([$this, $methodName])) {
    $method = new \ReflectionMethod($this, $methodName);
    $param  = $method->getParameters()[0] ?? null;
    $type   = $param?->getType();

    if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
        $class = $type->getName();

        if (is_array($value) && method_exists($class, 'fromArray')) {
            $value = $class::fromArray($value);
        }
    }

    $this->$methodName($value);
}

}

Тут была добавлена рефлексия, которая позволяет получать тип аргумента функции и если передается массив, когда функция ожидает объект, то будет вызываться функция ::fromArray этого объекта в которой будет указан передаваемый массив

Copy link
Collaborator

Choose a reason for hiding this comment

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

скорее так исправить

это более системное решение

{
$method = new \ReflectionMethod($this, $methodName);
$param = $method->getParameters()[0] ?? null;
$type = $param?->getType();
Copy link
Collaborator

Choose a reason for hiding this comment

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

либа пхп71 поддерживает

$response = $this->request->delete($this->getMethod() . '/' . $widgetModel->getCode());

return $widgetModel;
return $response['result'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

уверен, что это нужно?

нарушение обратной совместимости

Copy link
Author

Choose a reason for hiding this comment

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

Не думаю, что это нужно т.к жалоб не было)

Изменил, эту функцию, чтобы она возвращала именно bool, а не пыталась перезаписывать передаваемый объект пустым телом ответа

Сейчас откатил это изменение

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants