Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Rolling #26

Merged
merged 7 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Astaroth/Attribute/ClassAttribute/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function validate(): bool

//if the ID array is not specified in the attribute, then we check if the type matches
if ($this->member_id === []) {
return $validate["type"];
return (bool)$validate["type"];
}

//condition opposite to above condition
Expand Down Expand Up @@ -102,10 +102,10 @@ private function chatValidate(MessageNew|MessageEvent $data): array
}

/**
* @param $haystack
* @param mixed $haystack
* @return Conversation
*/
public function setHaystack($haystack): Conversation
public function setHaystack(mixed $haystack): Conversation
{
if ($haystack instanceof MessageEvent || $haystack instanceof MessageNew) {
$this->haystack = $haystack;
Expand Down
11 changes: 6 additions & 5 deletions src/Astaroth/Attribute/ClassAttribute/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Astaroth\Contracts\AttributeValidatorInterface;
use Astaroth\Enums\Events;
use Attribute;
use function is_object;
use function method_exists;

#[Attribute(Attribute::TARGET_CLASS)]
Expand Down Expand Up @@ -37,13 +38,13 @@ public function validate(): bool

/**
*
* @param $haystack
* @return Event
* @param mixed $haystack
* @return static
*/
public function setHaystack($haystack): Event
public function setHaystack(mixed $haystack): Event
{
if (method_exists($haystack, "getType")) {
$this->type = $haystack->getType();
if (is_object($haystack) && method_exists($haystack, "getType")) {
$this->type = (string)$haystack->getType();
}

return $this;
Expand Down
25 changes: 15 additions & 10 deletions src/Astaroth/Attribute/General/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Astaroth\Enums\ConversationType;
use Astaroth\Support\Facades\Session;
use Attribute;
use LogicException;
use function is_object;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class State implements AttributeValidatorInterface, AttributeMethodInterface
Expand All @@ -22,26 +24,29 @@ final class State implements AttributeValidatorInterface, AttributeMethodInterfa

public function __construct
(
private readonly string $state_name,
private readonly string $state_name,
private readonly ConversationType $member_type = ConversationType::PERSONAL
)
{
}

/**
* @inheritDoc
* @psalm-suppress RedundantCondition
*/
public function validate(): bool
{
if ($this->haystack) {
/** @psalm-suppress PossiblyUndefinedMethod */
$user_id = match ($this->haystack::class) {
MessageNew::class => fn() => $this->haystack?->getFromId(),
MessageEvent::class => fn() => $this->haystack?->getUserId(),
};
if (is_object($this->haystack)) {
if ($this->haystack instanceof MessageNew) {
$user_id = $this->haystack->getFromId();
} elseif ($this->haystack instanceof MessageEvent) {
$user_id = $this->haystack->getUserId();
} else {
throw new LogicException("Invalid message type");
}

$member_id = match ($this->member_type) {
ConversationType::PERSONAL => (int)$user_id(),
ConversationType::PERSONAL => $user_id,
ConversationType::CHAT => (int)$this->haystack->getChatId(),
ConversationType::ALL => $this->haystack->getPeerId(),
};
Expand All @@ -54,10 +59,10 @@ public function validate(): bool

/**
* @inheritDoc
* @param $haystack
* @param mixed $haystack
* @return State
*/
public function setHaystack($haystack): State
public function setHaystack(mixed $haystack): State
{
if ($haystack instanceof MessageNew || $haystack instanceof MessageEvent) {
$this->haystack = $haystack;
Expand Down
4 changes: 2 additions & 2 deletions src/Astaroth/Attribute/Method/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return Action
*/
public function setHaystack($haystack): Action
public function setHaystack(mixed $haystack): Action
{
if ($haystack instanceof MessageNew) {
$this->haystack = $haystack->getAction();
Expand Down
6 changes: 4 additions & 2 deletions src/Astaroth/Attribute/Method/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
final class Attachment implements AttributeValidatorInterface, AttributeMethodInterface
{
/** @var object[] */
private array $haystack = [];

public function __construct(
Expand All @@ -38,10 +39,11 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return Attachment
* @psalm-suppress MixedPropertyTypeCoercion
*/
public function setHaystack($haystack): Attachment
public function setHaystack(mixed $haystack): Attachment
{
if ($haystack instanceof MessageNew) {
$this->haystack = $haystack->getAttachments() ?? [];
Expand Down
17 changes: 10 additions & 7 deletions src/Astaroth/Attribute/Method/ClientInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Attribute;
use function array_intersect_key;
use function array_map;
use function is_object;

#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
/**
Expand All @@ -31,7 +32,7 @@ final class ClientInfo implements AttributeValidatorInterface, AttributeMethodIn
* @param int $lang_id
*/
public function __construct(
array $button_actions =
array $button_actions =
[
ClientInfoEnum::TEXT,
ClientInfoEnum::VKPAY,
Expand All @@ -45,18 +46,20 @@ public function __construct(
private readonly bool $keyboard = true,
private readonly bool $inline_keyboard = true,
private readonly bool $carousel = true,
private readonly int $lang_id = 0,
private readonly int $lang_id = 0,
)
{
$this->button_actions = array_map(static fn(ClientInfoEnum $enum) => $enum->value, $button_actions);
}

public function validate(): bool
{
if ($this->client_info) {
if (($this->button_actions !== []) && array_intersect_key(
if (is_object($this->client_info)) {
if (
($this->button_actions !== []) && array_intersect_key(
$this->button_actions,
$this->client_info->button_actions) === []) {
(array)$this->client_info->button_actions) === []
) {
return false;
}

Expand All @@ -76,10 +79,10 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return ClientInfo
*/
public function setHaystack($haystack): ClientInfo
public function setHaystack(mixed $haystack): ClientInfo
{
if ($haystack instanceof DataFetcher) {
$this->client_info = $haystack->getClientInfo();
Expand Down
4 changes: 2 additions & 2 deletions src/Astaroth/Attribute/Method/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return Debug
*/
public function setHaystack($haystack): Debug
public function setHaystack(mixed $haystack): Debug
{
$this->haystack = $haystack;
return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Astaroth/Attribute/Method/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return Message
*/
public function setHaystack($haystack): Message
public function setHaystack(mixed $haystack): Message
{
if ($haystack instanceof MessageNew) {
$this->haystack = $haystack->getText();
Expand Down
27 changes: 24 additions & 3 deletions src/Astaroth/Attribute/Method/MessageRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public function validate(): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return MessageRegex
*/
public function setHaystack($haystack): MessageRegex
public function setHaystack(mixed $haystack): MessageRegex
{
if ($haystack instanceof MessageNew) {
$this->haystack = $haystack->getText();
Expand All @@ -64,16 +64,32 @@ private function match(): array
return $matches;
}

/**
* @param $offset
* @return bool
* @psalm-suppress MixedArrayOffset, MissingParamType
*/
public function offsetExists($offset): bool
{
return isset($this->matches[$offset]);
}

/**
* @param $offset
* @return mixed
* @psalm-suppress MixedArrayOffset, MissingParamType
*/
public function offsetGet($offset): mixed
{
return $this->matches[$offset] ?? null;
return $this->matches[$offset];
}

/**
* @param $offset
* @param $value
* @return void
* @psalm-suppress MixedArrayOffset, MissingParamType
*/
public function offsetSet($offset, $value): void
{
if ($offset === null) {
Expand All @@ -83,6 +99,11 @@ public function offsetSet($offset, $value): void
}
}

/**
* @param $offset
* @return void
* @psalm-suppress MixedArrayOffset, MissingParamType
*/
public function offsetUnset($offset): void
{
unset($this->matches[$offset]);
Expand Down
4 changes: 2 additions & 2 deletions src/Astaroth/Attribute/Method/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private function keyExistValidate(array|string $payload, array $haystack): bool
}

/**
* @param $haystack
* @param mixed $haystack
* @return Payload
*/
public function setHaystack($haystack): Payload
public function setHaystack(mixed $haystack): Payload
{
if ($haystack instanceof MessageNew || $haystack instanceof MessageEvent) {
$this->haystack = $haystack->getPayload();
Expand Down
7 changes: 6 additions & 1 deletion src/Astaroth/Auth/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @psalm-suppress InvalidReturnType
* @psalm-suppress PossiblyInvalidArgument
* @psalm-suppress MethodSignatureMismatch
* @psalm-suppress PossiblyInvalidCast
*/
final class Configuration implements ConfigurationInterface
{
Expand Down Expand Up @@ -83,7 +84,11 @@ public function getAppNamespace(): string
*/
public function getEntityPath(): array
{
return array_map("\\trim", explode(',', Database::ENTITY_PATH->getEnv()));
return array_map
(
trim(...),
explode(',', Database::ENTITY_PATH->getEnv())
);
}

public function isHandleRepeatedRequest(): bool
Expand Down
4 changes: 3 additions & 1 deletion src/Astaroth/Commands/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(private readonly ?string $accessToken)
* Call API method
* @throws Throwable
*/
public function customRequest(string $method, $params): array
public function customRequest(string $method, array $params): array
{
return Request::call($method, $params, $this->accessToken);
}
Expand Down Expand Up @@ -54,6 +54,8 @@ public function sendMessageEventAnswer(MessageEvent $data, array $event): array
*
* @throws Throwable
* @see https://vk.com/dev/messages.edit
*
* @psalm-suppress MixedAssignment
*/
public function messagesEdit(IBuilder $message, int $conversation_message_id = null, int $message_id = null): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Astaroth/Contracts/AttributeValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function validate(): bool;
/**
* Haystack setter to be used for validation
* Any data that can be validated for a specific attribute
* @param $haystack
* @param mixed $haystack
* @return static
*/
public function setHaystack($haystack): static;
public function setHaystack(mixed $haystack): static;
}
6 changes: 4 additions & 2 deletions src/Astaroth/Foundation/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ private function newInstance(string $class, mixed ...$parameters): object
* And add arguments
* method_exist is not needed since method 100% exists
*
* вернет объект класса или объект класса с параметрами
*
* @param object $object
* @param ReflectionMethod $method
* @param array $parameters
Expand All @@ -273,6 +271,10 @@ private function invoke(object $object, ReflectionMethod $method, array $paramet
}


/**
* todo Optimize it
* @return void
*/
private function clearStack(): void
{
unset($this->parameters, $this->replaceableObjects);
Expand Down
Loading