Skip to content

Commit

Permalink
新增phpstan.neon.dist及调整检测至9级(level8) (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory authored Jul 8, 2022
1 parent 82ae04b commit 7bbaabf
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 38 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

Socialite is an [OAuth2](https://oauth.net/2/) Authentication tool. It is inspired by [laravel/socialite](https://github.com/laravel/socialite), You can easily use it in any PHP project. [中文文档](/README_CN.md)

![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/overtrue/laravel-socialite?style=flat-square)
![GitHub License](https://img.shields.io/github/license/overtrue/laravel-socialite?style=flat-square)
![Packagist Downloads](https://img.shields.io/packagist/dt/overtrue/laravel-socialite?style=flat-square)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/overtrue/socialite?style=flat-square)](https://github.com/overtrue/socialite/releases)
[![GitHub License](https://img.shields.io/github/license/overtrue/socialite?style=flat-square)](https://github.com/overtrue/socialite/blob/master/LICENSE)
[![Packagist Downloads](https://img.shields.io/packagist/dt/overtrue/socialite?style=flat-square)](https://packagist.org/overtrue/socialite)

[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)

This tool now supports platforms such as Facebook, GitHub, Google, Figma, LinkedIn, Outlook, QQ, Tapd, Alipay, Taobao, Baidu, DingTalk, Weibo, WeChat, Douyin, Feishu, Douban, WeWork, Tencent Cloud, Line, Gitee.

如果你喜欢我的项目并想支持它,[点击这里 :heart:](https://github.com/sponsors/overtrue)

- [Socialite](#socialite)
- [Requirement](#requirement)
- [Installation](#installation)
- [Usage](#usage)
Expand Down Expand Up @@ -40,6 +41,7 @@ This tool now supports platforms such as Facebook, GitHub, Google, Figma, Linked
- [Get user with access token](#get-user-with-access-token)
- [Enjoy it! :heart:](#enjoy-it-heart)
- [Reference](#reference)
- [Project supported by JetBrains](#project-supported-by-jetbrains)
- [PHP 扩展包开发](#php-扩展包开发)
- [License](#license)

Expand Down
7 changes: 4 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

Socialite 是一个 [OAuth2](https://oauth.net/2/) 认证工具。 它的灵感来源于 [laravel/socialite](https://github.com/laravel/socialite) , 你可以很轻易的在任何 PHP 项目中使用它。

![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/overtrue/laravel-socialite?style=flat-square)
![GitHub License](https://img.shields.io/github/license/overtrue/laravel-socialite?style=flat-square)
![Packagist Downloads](https://img.shields.io/packagist/dt/overtrue/laravel-socialite?style=flat-square)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/overtrue/socialite?style=flat-square)](https://github.com/overtrue/socialite/releases)
[![GitHub License](https://img.shields.io/github/license/overtrue/socialite?style=flat-square)](https://github.com/overtrue/socialite/blob/master/LICENSE)
[![Packagist Downloads](https://img.shields.io/packagist/dt/overtrue/socialite?style=flat-square)](https://packagist.org/overtrue/socialite)

[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)

该工具现已支持平台有:Facebook,Github,Google,Linkedin,Outlook,QQ,TAPD,支付宝,淘宝,百度,钉钉,微博,微信,抖音,飞书,豆瓣,企业微信,腾讯云,Line,Gitee。

- [Socialite](#socialite)
- [版本要求](#版本要求)
- [安装](#安装)
- [使用指南](#使用指南)
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
level: 8
paths:
- src
16 changes: 12 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function get(string $key, mixed $default = null): mixed
return $config;
}

public function set(string $key, $value): mixed
public function set(string $key, mixed $value): array
{
$keys = \explode('.', $key);
$config = &$this->config;
Expand All @@ -60,21 +60,29 @@ public function has(string $key): bool

public function offsetExists(mixed $offset): bool
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');

return \array_key_exists($offset, $this->config);
}

public function offsetGet(mixed $offset): mixed
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');

return $this->get($offset);
}

public function offsetSet(mixed $offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');

$this->set($offset, $value);
}

public function offsetUnset(mixed $offset): void
{
\is_string($offset) || throw new Exceptions\InvalidArgumentException('The $offset must be type of string here.');

$this->set($offset, null);
}

Expand All @@ -83,8 +91,8 @@ public function jsonSerialize(): array
return $this->config;
}

public function __toString()
public function __toString(): string
{
return \json_encode($this, \JSON_UNESCAPED_UNICODE);
return \json_encode($this, \JSON_UNESCAPED_UNICODE) ?: '';
}
}
4 changes: 2 additions & 2 deletions src/Providers/Alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Alipay extends Base
public function __construct(array $config)
{
parent::__construct($config);
$this->sandbox = $this->config->get('sandbox', false);
$this->sandbox = (bool)$this->config->get('sandbox', false);
if ($this->sandbox) {
$this->baseUrl = 'https://openapi.alipaydev.com/gateway.do';
$this->authUrl = 'https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm';
Expand Down Expand Up @@ -171,7 +171,7 @@ public function getPublicFields(string $method): array
/**
* @see https://opendocs.alipay.com/open/289/105656
*/
protected function generateSign($params): string
protected function generateSign(array $params): string
{
\ksort($params);

Expand Down
10 changes: 7 additions & 3 deletions src/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function tokenFromCode(string $code): array
/**
* @throws Exceptions\MethodDoesNotSupportException
*/
public function refreshToken(string $refreshToken)
public function refreshToken(string $refreshToken): void
{
throw new Exceptions\MethodDoesNotSupportException('refreshToken does not support.');
}
Expand Down Expand Up @@ -236,7 +236,7 @@ protected function getCodeFields(): array
/**
* @throws Exceptions\AuthorizeFailedException
*/
protected function normalizeAccessTokenResponse($response): array
protected function normalizeAccessTokenResponse(mixed $response): array
{
if ($response instanceof StreamInterface) {
$response->tell() && $response->rewind();
Expand Down Expand Up @@ -264,6 +264,10 @@ protected function normalizeAccessTokenResponse($response): array

protected function fromJsonBody(MessageInterface $response): array
{
return Utils::jsonDecode((string) $response->getBody(), true);
$result = Utils::jsonDecode((string) $response->getBody(), true);

\is_array($result) || throw new Exceptions\InvalidArgumentException('Decoded the given response payload failed.');

return $result;
}
}
4 changes: 2 additions & 2 deletions src/Providers/DingTalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getClientSecret(): ?string

protected function createSignature(int $time): string
{
return \base64_encode(\hash_hmac('sha256', $time, $this->getClientSecret(), true));
return \base64_encode(\hash_hmac('sha256', (string)$time, (string)$this->getClientSecret(), true));
}

/**
Expand All @@ -108,7 +108,7 @@ public function userFromCode(string $code): Contracts\UserInterface
]);
$response = $this->fromJsonBody($responseInstance);

if (0 != $response['errcode'] ?? 1) {
if (0 != ($response['errcode'] ?? 1)) {
throw new Exceptions\BadRequestException((string)$responseInstance->getBody());
}

Expand Down
8 changes: 6 additions & 2 deletions src/Providers/FeiShu.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function withAppTicket(string $appTicket): self
* @throws Exceptions\FeiShu\InvalidTicketException
* @throws Exceptions\InvalidTokenException
*/
protected function configAppAccessToken()
protected function configAppAccessToken(): self
{
$url = $this->baseUrl . '/auth/v3/app_access_token/';
$params = [
Expand Down Expand Up @@ -175,6 +175,8 @@ protected function configAppAccessToken()
}

$this->config->set('app_access_token', $response['app_access_token']);

return $this;
}

/**
Expand All @@ -185,7 +187,7 @@ protected function configAppAccessToken()
* @throws Exceptions\BadRequestException
* @throws Exceptions\AuthorizeFailedException
*/
protected function configTenantAccessToken()
protected function configTenantAccessToken(): self
{
$url = $this->baseUrl . '/auth/v3/tenant_access_token/';
$params = [
Expand Down Expand Up @@ -217,5 +219,7 @@ protected function configTenantAccessToken()
}

$this->config->set('tenant_access_token', $response['tenant_access_token']);

return $this;
}
}
4 changes: 2 additions & 2 deletions src/Providers/Linkedin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getTokenUrl(): string
Contracts\RFC6749_ABNF_REDIRECT_URI => 'null|string',
Contracts\RFC6749_ABNF_GRANT_TYPE => 'string',
])]
protected function getTokenFields($code): array
protected function getTokenFields(string $code): array
{
return parent::getTokenFields($code) + [Contracts\RFC6749_ABNF_GRANT_TYPE => Contracts\RFC6749_ABNF_AUTHORATION_CODE];
}
Expand Down Expand Up @@ -78,7 +78,7 @@ protected function getEmailAddress(string $token): array

protected function mapUserToObject(array $user): Contracts\UserInterface
{
$preferredLocale = ($user['firstName.preferredLocale.language'] ?? null).'_'.($user['firstName.preferredLocale.country']) ?? null;
$preferredLocale = ($user['firstName.preferredLocale.language'] ?? null).'_'.($user['firstName.preferredLocale.country'] ?? null);
$firstName = $user['firstName.localized.'.$preferredLocale] ?? null;
$lastName = $user['lastName.localized.'.$preferredLocale] ?? null;
$name = $firstName.' '.$lastName;
Expand Down
8 changes: 4 additions & 4 deletions src/Providers/QCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function performRequest(string $method, string $host, string $action, str
return $response['Response'] ?? [];
}

protected function sign(string $requestMethod, string $host, array $query, string $payload, $headers, $credential, ?string $secretKey = null): bool|string
protected function sign(string $requestMethod, string $host, array $query, string $payload, array $headers, string $credential, ?string $secretKey = null): bool|string
{
$canonicalRequestString = \join(
"\n",
Expand Down Expand Up @@ -167,13 +167,13 @@ protected function sign(string $requestMethod, string $host, array $query, strin
/**
* @throws Exceptions\AuthorizeFailedException
*/
protected function parseAccessToken(array | string $body)
protected function parseAccessToken(array | string $body): array
{
if (!\is_array($body)) {
$body = \json_decode($body, true);
}

if (empty($body['UserOpenId'])) {
if (empty($body['UserOpenId'] ?? null)) {
throw new Exceptions\AuthorizeFailedException('Authorize Failed: ' . \json_encode($body, JSON_UNESCAPED_UNICODE), $body);
}

Expand Down Expand Up @@ -205,7 +205,7 @@ protected function getFederationToken(string $accessToken): array
]
);

if (empty($response['Credentials'])) {
if (empty($response['Credentials'] ?? null)) {
throw new Exceptions\AuthorizeFailedException('Get Federation Token failed.', $response);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Providers/QQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function getUserByToken(string $token): array
'query' => [
Contracts\RFC6749_ABNF_ACCESS_TOKEN => $token,
'fmt' => 'json',
] + $this->withUnionId ? ['unionid' => 1] : []
] + ($this->withUnionId ? ['unionid' => 1] : [])
]);

$me = $this->fromJsonBody($response);
Expand All @@ -78,7 +78,7 @@ protected function getUserByToken(string $token): array
]
]);

return ($this->fromJsonBody($response) ?? []) + [
return $this->fromJsonBody($response) + [
'unionid' => $me['unionid'] ?? null,
'openid' => $me['openid'] ?? null,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Tapd.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function mapUserToObject(array $user): Contracts\UserInterface
/**
* @throws Exceptions\AuthorizeFailedException
*/
protected function normalizeAccessTokenResponse($response): array
protected function normalizeAccessTokenResponse(mixed $response): array
{
if ($response instanceof StreamInterface) {
$response->rewind();
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/WeChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function getTokenUrl(): string
return \sprintf($this->baseUrl . '/oauth2%s/access_token', empty($this->component) ? '' : '/component');
}

public function userFromCode(string $code): User
public function userFromCode(string $code): Contracts\UserInterface
{
if (\in_array('snsapi_base', $this->scopes)) {
return $this->mapUserToObject($this->fromJsonBody($this->getTokenFromCode($code)));
Expand Down Expand Up @@ -178,7 +178,7 @@ protected function getTokenFromCode(string $code): ResponseInterface
/**
* @throws Exceptions\InvalidArgumentException
*/
protected function prepareForComponent(array $component)
protected function prepareForComponent(array $component): void
{
$config = [];
foreach ($component as $key => $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/WeWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(array $config)
}
}

public function getBaseUrl()
public function getBaseUrl(): ?string
{
return $this->baseUrl;
}
Expand Down
6 changes: 5 additions & 1 deletion src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public function getResolvedProviders(): array

public function buildProvider(string $provider, array $config): Contracts\ProviderInterface
{
return new $provider($config);
$instance = new $provider($config);

$instance instanceof Contracts\ProviderInterface || throw new Exceptions\InvalidArgumentException("The {$provider} must be instanceof ProviderInterface.");

return $instance;
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Overtrue\Socialite\Traits;

use JetBrains\PhpStorm\Pure;
use Overtrue\Socialite\Exceptions;

trait HasAttributes
{
Expand All @@ -13,7 +14,7 @@ public function getAttributes(): array
return $this->attributes;
}

public function getAttribute(string $name, mixed $default = null)
public function getAttribute(string $name, mixed $default = null): mixed
{
return $this->attributes[$name] ?? $default;
}
Expand Down Expand Up @@ -52,7 +53,7 @@ public function offsetUnset(mixed $offset): void
unset($this->attributes[$offset]);
}

public function __get($property)
public function __get(string $property): mixed
{
return $this->getAttribute($property);
}
Expand All @@ -65,6 +66,10 @@ public function toArray(): array

public function toJSON(): string
{
return \json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE);
$result = \json_encode($this->getAttributes(), JSON_UNESCAPED_UNICODE);

false === $result && throw new Exceptions\Exception('Cannot Processing this instance as JSON stringify.');

return $result;
}
}
6 changes: 3 additions & 3 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function setTokenResponse(array $response): self
return $this;
}

public function getTokenResponse()
public function getTokenResponse(): mixed
{
return $this->getAttribute('token_response');
}
Expand All @@ -109,14 +109,14 @@ public function __serialize(): array
return $this->attributes;
}

public function __unserialize(array $serialized)
public function __unserialize(array $serialized): void
{
$this->attributes = $serialized ?: [];
}

public function getProvider(): Contracts\ProviderInterface
{
return $this->provider;
return $this->provider ?? throw new Exceptions\Exception('The provider instance doesn\'t initialized correctly.');
}

public function setProvider(Contracts\ProviderInterface $provider): self
Expand Down

0 comments on commit 7bbaabf

Please sign in to comment.