From 99d4587b3b02f556a6a8ffa35f781a7adc4a4a9f Mon Sep 17 00:00:00 2001 From: naumso Date: Sat, 11 Jan 2025 01:36:15 +0300 Subject: [PATCH] UPD to php 8.4 --- .gitignore | 4 +- composer.json | 3 +- phpcs.xml.dist | 20 ++++ phpstan.dist.neon | 14 +++ src/VkProvider.php | 262 +++++++++++++++++++++++---------------------- src/VkUser.php | 94 ++++++++++------ 6 files changed, 233 insertions(+), 164 deletions(-) create mode 100644 phpcs.xml.dist create mode 100644 phpstan.dist.neon diff --git a/.gitignore b/.gitignore index 751f6c5..4fa25db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build composer.lock docs -vendor \ No newline at end of file +vendor +.php-cs-fixer.cache +.phpcs-cache diff --git a/composer.json b/composer.json index 10d4e9a..887e641 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "1sept/oauth2-vkontakte", + "version": "1.2", "description": "VK provider for league/oauth2-client", "keywords": [ "league", @@ -13,7 +14,7 @@ } ], "require": { - "php" : ">=8.1", + "php" : ">=8.3", "league/oauth2-client": "^2.0" }, "require-dev": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..71252a8 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + src/ + + + + diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..faa1f6e --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,14 @@ +parameters: + level: 5 + paths: + - bin/ + - config/ + - public/ + - src/ + + errorFormat: table + editorUrl: 'vscode://file/%%file%%:%%line%%' + editorUrlTitle: '%%relFile%%:%%line%%' + + ignoreErrors: + - '#Construct empty\(\) is not allowed. Use more strict comparison.#' diff --git a/src/VkProvider.php b/src/VkProvider.php index 2760204..0032928 100644 --- a/src/VkProvider.php +++ b/src/VkProvider.php @@ -1,5 +1,7 @@ language = (string)$language; + $this->language = $language; return $this; } @@ -130,21 +131,21 @@ class VkProvider extends AbstractProvider { return "$this->baseOAuthUri/access_token"; } - + public function getResourceOwnerDetailsUrl(AccessToken $token): string { $params = [ - 'fields' => $this->userFields, + 'fields' => $this->userFields, 'access_token' => $token->getToken(), - 'v' => $this->version, - 'lang' => $this->language + 'v' => $this->version, + 'lang' => $this->language, ]; - $query = $this->buildQueryString($params); - $url = "$this->baseUri/users.get?$query"; + $query = $this->buildQueryString($params); + $url = "$this->baseUri/users.get?$query"; return $url; } - + protected function getDefaultScopes(): array { return $this->scopes; @@ -157,14 +158,15 @@ class VkProvider extends AbstractProvider $contentTypeArray = explode(';', reset($contentTypeRaw)); $contentType = reset($contentTypeArray); // Response info - $responseCode = $response->getStatusCode(); + $responseCode = $response->getStatusCode(); $responseMessage = $response->getReasonPhrase(); + // Data info - $error = !empty($data['error']) ? $data['error'] : null; - $errorCode = !empty($error['error_code']) ? $error['error_code'] : $responseCode; + $error = !empty($data['error']) ? $data['error'] : null; + $errorCode = !empty($error['error_code']) ? $error['error_code'] : $responseCode; $errorDescription = !empty($data['error_description']) ? $data['error_description'] : null; - $errorMessage = !empty($error['error_msg']) ? $error['error_msg'] : $errorDescription; - $message = $errorMessage ?: $responseMessage; + $errorMessage = !empty($error['error_msg']) ? $error['error_msg'] : $errorDescription; + $message = (bool) $errorMessage ? $errorMessage : $responseMessage; // Request/meta validation if (399 < $responseCode) { @@ -172,89 +174,93 @@ class VkProvider extends AbstractProvider } // Content validation - if ('application/json' != $contentType) { + if ('application/json' !== $contentType) { throw new IdentityProviderException($message, $responseCode, $data); } if ($error) { throw new IdentityProviderException($errorMessage, $errorCode, $data); } } + protected function createResourceOwner(array $response, AccessToken $token): VkUser { - $response = reset($response['response']); + $response = reset($response['response']); $additional = $token->getValues(); + if (!empty($additional['email'])) { $response['email'] = $additional['email']; } - if (!empty($response['uid']) && 4 === floor($this->version)) { + + if (!empty($response['uid']) && 4 === (int) floor($this->version)) { $response['id'] = $response['uid']; } + if (!empty($additional['user_id'])) { $response['id'] = $additional['user_id']; } - return new VkUser($response, $response['id']); + return new VkUser($response); } /** + * Возвращает массив объектов пользователей. + * * @see https://vk.com/dev/users.get - * - * @param integer[] $ids - * @param AccessToken|null $token Current user if empty - * @param array $params - * - * @return User[] */ - public function usersGet(array $ids = [], AccessToken $token = null, array $params = []): array + public function usersGet(array $ids = [], ?AccessToken $token = null, array $params = []): array { - if (empty($ids) && !$token) { + if (empty($ids) && null === $token) { throw new \InvalidArgumentException('Some of parameters usersIds OR access_token are required'); } $default = [ - 'user_ids' => implode(',', $ids), - 'fields' => $this->userFields, - 'access_token' => $token ? $token->getToken() : null, - 'v' => $this->version, - 'lang' => $this->language + 'user_ids' => implode(',', $ids), + 'fields' => $this->userFields, + 'access_token' => null !== $token ? $token->getToken() : null, + 'v' => $this->version, + 'lang' => $this->language, ]; - $params = array_merge($default, $params); - $query = $this->buildQueryString($params); - $url = "$this->baseUri/users.get?$query"; - $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; - $users = !empty($response['items']) ? $response['items'] : $response; - $array2user = function ($userData) { + $params = array_merge($default, $params); + $query = $this->buildQueryString($params); + $url = "$this->baseUri/users.get?$query"; + + $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; + $users = !empty($response['items']) ? $response['items'] : $response; + + $array2user = function (mixed $userData) { return new VkUser($userData); }; return array_map($array2user, $users); } + /** + * Возвращает список идентификаторов (id) друзей пользователя, + * если параметр fields не использовался. При использовании + * параметра fields возвращает список объектов пользователей, + * но не более 5000. + * * @see https://vk.com/dev/friends.get - * - * @param integer $userId - * @param AccessToken|null $token - * @param array $params - * - * @return User[] */ - public function friendsGet($userId, AccessToken $token = null, array $params = []): array + public function friendsGet(int $userId, ?AccessToken $token = null, array $params = []): array { $default = [ - 'user_id' => $userId, - 'fields' => $this->userFields, - 'access_token' => $token ? $token->getToken() : null, - 'v' => $this->version, - 'lang' => $this->language + 'user_id' => $userId, + 'fields' => $this->userFields, + 'access_token' => null !== $token ? $token->getToken() : null, + 'v' => $this->version, + 'lang' => $this->language, ]; - $params = array_merge($default, $params); - $query = $this->buildQueryString($params); - $url = "$this->baseUri/friends.get?$query"; - $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; - $friends = !empty($response['items']) ? $response['items'] : $response; - $array2friend = function ($friendData) { + $params = array_merge($default, $params); + $query = $this->buildQueryString($params); + $url = "$this->baseUri/friends.get?$query"; + + $response = $this->getResponse($this->createRequest(static::METHOD_GET, $url, $token, []))['response']; + $friends = !empty($response['items']) ? $response['items'] : $response; + + $array2friend = function (mixed $friendData) { if (is_numeric($friendData)) { $friendData = ['id' => $friendData]; } diff --git a/src/VkUser.php b/src/VkUser.php index c044523..568ac60 100644 --- a/src/VkUser.php +++ b/src/VkUser.php @@ -1,16 +1,18 @@ response = $response; } - + public function toArray(): array { return $this->response; } - + public function getId(): int { - return (int)($this->getField('uid') ?: $this->getField('id')); + return (int) ((bool) $this->getField('uid') ? $this->getField('uid') : $this->getField('id')); } /** - * Helper for getting user data + * Helper for getting user data. */ protected function getField(string $key): mixed { - return !empty($this->response[$key]) ? $this->response[$key] : null; + return \array_key_exists($key, $this->response) ? $this->response[$key] : null; } /** * @return string|null DD.MM.YYYY */ - public function getBirthday():string + public function getBirthday(): ?string { return $this->getField('bdate'); } @@ -51,36 +53,41 @@ class VkUser implements ResourceOwnerInterface /** * @return array [id =>, title => string] */ - public function getCity(): array + public function getCity(): ?array { return $this->getField('city'); } - + /** * @return array [id =>, title => string] */ - public function getCountry(): array + public function getCountry(): ?array { return $this->getField('country'); } /** - * Short address to user page + * Short address to user page. */ - public function getDomain(): string|null + public function getDomain(): ?string { return $this->getField('domain'); } - - public function getFirstName(): string|null + + /** + * Return firstname. + */ + public function getFirstName(): ?string { return $this->getField('first_name'); } - + /** + * Friend status. + * * @return int 0|1|2|3 => nobody|resquest_sent|incoming_request|friends */ - public function getFriendStatus(): int + public function getFriendStatus(): ?int { return $this->getField('friend_Status'); } @@ -90,61 +97,80 @@ class VkUser implements ResourceOwnerInterface */ public function isHasPhoto(): bool { - return (bool)$this->getField('has_photo'); + return (bool) $this->getField('has_photo'); } - - public function getHomeTown(): string|null + + public function getHomeTown(): ?string { return $this->getField('home_town'); } /** - * Detect if current user is freind to this + * Detect if current user is freind to this. */ public function isFriend(): bool { - return (bool)$this->getField('is_friend'); + return (bool) $this->getField('is_friend'); } - - public function getLastName(): string|null + + public function getLastName(): ?string { return $this->getField('last_name'); } - public function getMaidenName(): string|null + public function getMaidenName(): ?string { return $this->getField('maiden_name'); } - public function getNickname(): string|null + public function getNickname(): ?string { return $this->getField('nickname'); } + /** * It's square! */ - public function getPhotoMax():string + public function getPhotoMax(): ?string { return $this->getField('photo_max'); } - public function getPhotoMaxOrig(): string|null + public function getPhotoMaxOrig(): ?string { return $this->getField('photo_max_orig'); } - - public function getScreenName(): string|null + + public function getScreenName(): ?string { return $this->getField('screen_name'); } - - public function getSex(): int + + public function getSex(): ?int { return $this->getField('sex'); } - public function getEmail(): string|null + public function getEmail(): ?string { return $this->getField('email'); - } + } + + /** + * Поле возвращается, если страница пользователя удалена или заблокирована, содержит значение deleted или banned. В этом случае опциональные поля не возвращаются. + * + * @return string null|deleted|banned + */ + public function getDeactivated(): ?string + { + return $this->getField('deactivated'); + } + + /** + * Скрыт ли профиль пользователя настройками приватности. + */ + public function isClosed(): bool + { + return $this->getField('is_closed'); + } }