From 3380b4a0ae027b72392b397926cdfdc22068780f Mon Sep 17 00:00:00 2001 From: Alexander Vasilyev Date: Tue, 7 May 2024 12:44:10 +0300 Subject: [PATCH] Add API base URL castomization option --- .editorconfig | 9 ++++-- .gitattributes | 43 +++++++++++++++++++++++++ .phpunit.cache/test-results | 1 + composer.json | 21 ++++++++++-- phpstan.dist.neon | 5 +++ phpunit.xml | 37 --------------------- phpunit.xml.dist | 13 ++++++++ src/Provider/SeptemberFirstProvider.php | 18 +++++------ src/Provider/SeptemberFirstUser.php | 17 +++++----- tests/UserTest.php | 22 +++++++++++++ 10 files changed, 127 insertions(+), 59 deletions(-) create mode 100644 .gitattributes create mode 100644 .phpunit.cache/test-results create mode 100644 phpstan.dist.neon delete mode 100755 phpunit.xml create mode 100755 phpunit.xml.dist create mode 100644 tests/UserTest.php diff --git a/.editorconfig b/.editorconfig index 3ab1269..07f8b1e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,10 +2,15 @@ root = true -[src/**] +[**] charset = utf-8 end_of_line = lf -tab_width = 4 indent_style = space trim_trailing_whitespace = true insert_final_newline = true + +[{src,tests}/**.php] +tab_width = 4 + +[*.{neon,xml,md}] +tab_width = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..266b762 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,43 @@ +# Common settings that generally should always be used with your language specific settings + +# Auto detect text files and perform LF normalization +# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ +# Handle line endings automatically for files detected as text +# and leave all files detected as binary untouched. +* text=auto + +# Git +.gitattributes text +.gitignore text +.gitconfig text +.gitmodules text + +# source code +*.php diff=php + +*.json text +*.markdown text +*.md text +*.neon.dist text +*.xml text +*.xml.dist text +*README* text +.editorconfig text +AUTHORS text +LICENSE text + +# Ignore export +/.appveyor.yml export-ignore +/.editorconfig export-ignore +/.editorconfig export-ignore +/.gitattributes export-ignore +/.github export-ignore +/.gitignore export-ignore +/.gitignore export-ignore +/SECURITY.md export-ignore +/docs export-ignore +/phpcs.xml.dist export-ignore +/phpstan.neon.dist export-ignore +/phpunit.xml.dist export-ignore +/psalm.xml.dist export-ignore +/tests export-ignore diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results new file mode 100644 index 0000000..d568f3c --- /dev/null +++ b/.phpunit.cache/test-results @@ -0,0 +1 @@ +{"version":1,"defects":{"Sept\\OAuth2\\Client\\Test\\UserTest::testCreate":5},"times":{"Sept\\OAuth2\\Client\\Test\\UserTest::testCreate":0.001}} \ No newline at end of file diff --git a/composer.json b/composer.json index 463c9b6..0bab53f 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,11 @@ "league/oauth2-client": "^2.0" }, "require-dev": { - "phpunit/phpunit": "~10.3" + "phpstan/extension-installer": "^1.3", + "phpstan/phpdoc-parser": "^1.29", + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-strict-rules": "^1.5", + "phpunit/phpunit": "^11.1" }, "autoload": { "psr-4": { @@ -30,12 +34,25 @@ }, "autoload-dev": { "psr-4": { - "Sept\\OAuth2\\Client\\Test\\": "test/src/" + "Sept\\OAuth2\\Client\\Test\\": "tests" } }, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } + }, + "scripts": { + "test": [ + "@phpunit", + "@phpstan" + ], + "phpunit": "vendor/bin/phpunit", + "phpstan": "vendor/bin/phpstan" + }, + "config": { + "allow-plugins": { + "phpstan/extension-installer": true + } } } diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..1cd333b --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,5 @@ +parameters: + level: 8 + paths: + - src + - tests diff --git a/phpunit.xml b/phpunit.xml deleted file mode 100755 index 289c3f5..0000000 --- a/phpunit.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - ./test/ - - - - - ./ - - ./vendor - ./test - - - - \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100755 index 0000000..2b6cf09 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,13 @@ + + + + + tests + + + + + src + + + diff --git a/src/Provider/SeptemberFirstProvider.php b/src/Provider/SeptemberFirstProvider.php index 643f292..618b8fe 100755 --- a/src/Provider/SeptemberFirstProvider.php +++ b/src/Provider/SeptemberFirstProvider.php @@ -1,12 +1,9 @@ - static::AUTH_BASE.static::AUTHORIZE_PATH, - 'urlAccessToken' => static::API_BASE.static::ACCESS_TOKEN_PATH, - 'urlResourceOwnerDetails' => static::API_BASE.static::USERINFO_PATH, + 'urlAuthorize' => $authBase.static::AUTHORIZE_PATH, + 'urlAccessToken' => $apiBase.static::ACCESS_TOKEN_PATH, + 'urlResourceOwnerDetails' => $apiBase.static::USERINFO_PATH, 'scopes' => static::SCOPES_DEFAULT, 'scopeSeparator' => static::SCOPES_SEPARATOR, ]; @@ -83,7 +83,7 @@ class SeptemberFirstProvider extends GenericProvider */ protected function checkResponse(ResponseInterface $response, $data): void { - if (! empty($data['error'])) { + if (isset($data['error'])) { throw new IdentityProviderException($data['error'].': '.$data['message'], 0, $response); } } diff --git a/src/Provider/SeptemberFirstUser.php b/src/Provider/SeptemberFirstUser.php index 76ca7c6..836e0f0 100755 --- a/src/Provider/SeptemberFirstUser.php +++ b/src/Provider/SeptemberFirstUser.php @@ -1,6 +1,4 @@ -data['birthday']) ? new \DateTime($this->data['birthday']) : null; + return isset($this->data['birthday']) ? new \DateTime($this->data['birthday']) : null; } /** @@ -168,7 +166,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface */ public function getAvatarUrl(bool $rejectDefaultAvatar = false): ?string { - return ($rejectDefaultAvatar && $this->isDefaultAvatar()) ? null : $this->getField('avatar'); + return ($rejectDefaultAvatar && ($this->isDefaultAvatar() ?? false)) ? null : $this->getField('avatar'); } /** @@ -182,7 +180,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface public function getAvatarSizeUrl(int $size, int $ratioMultiplier = 1, bool $addVersion = true): ?string { $ratio = ($ratioMultiplier > 1) ? '@' . $ratioMultiplier . 'x' : ''; - $url = static::AVATAR_BASE .'/'. $this->getId() . ($size ? '.' : '') . $size . $ratio . '.jpeg'; + $url = static::AVATAR_BASE .'/'. $this->getId() . (((bool) $size)? '.' : '') . $size . $ratio . '.jpeg'; return $url . ($addVersion ? $this->getAvatarVersionQuery() : ''); } @@ -244,7 +242,8 @@ class SeptemberFirstUser implements ResourceOwnerInterface public function getAvatarVersionQuery(): string { $query = ''; - if ($version = $this->getField('avatar_version')) { + $version = $this->getField('avatar_version'); + if ((bool) $version) { $query .= '?v=' . $version; } return $query; @@ -321,7 +320,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface public function getAddressID(): ?int { $id = $this->getField('address.id'); - return $id ? (int) $id : null; + return ((bool) $id) ? ((int) $id) : null; } /** @@ -553,7 +552,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface */ public static function getFieldFromArray(string $key, ?array $array): mixed { - if (strpos($key, '.')) { // key.subKey.subSubKey + if ((bool) strpos($key, '.')) { // key.subKey.subSubKey list ($key, $subKey) = explode('.', $key, 2); return isset($array[$key]) ? static::getFieldFromArray($subKey, $array[$key]) : null; } diff --git a/tests/UserTest.php b/tests/UserTest.php new file mode 100644 index 0000000..8f592c1 --- /dev/null +++ b/tests/UserTest.php @@ -0,0 +1,22 @@ + [ + 'name' => $name, + ], + ]); + self::assertSame($name, $user->getFirstName()); + } +}