Skip to content

Commit

Permalink
Adds PHP CS Fixer to the repo to enforce PSR-12 with a couple of furt…
Browse files Browse the repository at this point in the history
…her linting practices I like such as aligning equal signs.
  • Loading branch information
bencorlett committed Sep 2, 2020
1 parent 051637e commit c52005b
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/composer.lock
/coverage.xml
/.phpunit.result.cache
/.php_cs.cache
.DS_Store
32 changes: 32 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

$finder = PhpCsFixer\Finder::create()->in(__DIR__ . '/src');

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['align_double_arrow' => true, 'align_equals' => true,],
'blank_line_before_return' => true,
'cast_spaces' => true,
'concat_space' => ['spacing' => 'one'],
'indentation_type' => true,
'method_chaining_indentation' => true,
'no_singleline_whitespace_before_semicolons' => true,
'not_operator_with_space' => true,
'ordered_imports' => true,
'phpdoc_align' => true,
'phpdoc_indent' => true,
'phpdoc_no_access' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_no_package' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_summary' => true,
'phpdoc_to_comment' => true,
'phpdoc_trim' => true,
'single_blank_line_at_eof' => true,
'ternary_operator_spaces' => true,
])
->setFinder($finder);
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install:
- travis_retry composer update --prefer-dist $COMPOSER_OPTS

script:
- composer php-cs-fixer:lint
- composer test:unit
- composer analyze

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"psr/http-message-implementation": "^1.0|^7.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"guzzlehttp/guzzle": "^6.0|^7.0",
"http-interop/http-factory-guzzle": "^1.0",
"mockery/mockery": "^1.3",
Expand All @@ -22,6 +23,8 @@
},
"scripts": {
"analyze": "vendor/bin/phpstan analyse -l 6 src",
"php-cs-fixer:lint": "vendor/bin/php-cs-fixer fix --verbose --dry-run",
"php-cs-fixer:format": "./vendor/bin/php-cs-fixer fix",
"test:unit": "vendor/bin/phpunit --coverage-text --coverage-clover coverage.xml"
},
"suggest": {
Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace League\OAuth1\Client;

use function GuzzleHttp\Psr7\parse_query;
use League\OAuth1\Client\Credentials\Credentials;
use League\OAuth1\Client\Provider\Provider;
use League\OAuth1\Client\Exception\CredentialsFetchingFailedException;
use League\OAuth1\Client\Provider\Provider;
use LogicException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use function GuzzleHttp\Psr7\parse_query;

class Client
{
Expand Down
2 changes: 1 addition & 1 deletion src/Credentials/ClientCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ public function getRealm(): ?string
{
return $this->realm;
}
}
}
4 changes: 2 additions & 2 deletions src/Credentials/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Credentials
public function __construct(string $identifier, string $secret)
{
$this->identifier = $identifier;
$this->secret = $secret;
$this->secret = $secret;
}

public function getIdentifier(): string
Expand All @@ -25,4 +25,4 @@ public function getSecret(): string
{
return $this->secret;
}
}
}
2 changes: 1 addition & 1 deletion src/Exception/CredentialsFetchingFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public function getResponse(): ResponseInterface
{
return $this->response;
}
}
}
14 changes: 7 additions & 7 deletions src/ParametersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function forTemporaryCredentialsRequest(ClientCredentials $clientCredenti
{
return array_replace($this->generateBaseParameters(), [
'oauth_consumer_key' => $clientCredentials->getIdentifier(),
'oauth_callback' => (string) $clientCredentials->getCallbackUri(),
'oauth_callback' => (string) $clientCredentials->getCallbackUri(),
]);
}

Expand All @@ -52,8 +52,8 @@ public function forTokenCredentialsRequest(
): array {
return array_replace($this->generateBaseParameters(), [
'oauth_consumer_key' => $clientCredentials->getIdentifier(),
'oauth_token' => $temporaryCredentials->getIdentifier(),
'oauth_verifier' => $verifier,
'oauth_token' => $temporaryCredentials->getIdentifier(),
'oauth_verifier' => $verifier,
]);
}

Expand All @@ -66,7 +66,7 @@ public function forAuthenticatedRequest(
): array {
return array_replace($this->generateBaseParameters(), [
'oauth_consumer_key' => $clientCredentials->getIdentifier(),
'oauth_token' => $tokenCredentials->getIdentifier(),
'oauth_token' => $tokenCredentials->getIdentifier(),
]);
}

Expand All @@ -77,8 +77,8 @@ private function generateBaseParameters(): array
{
$parameters = [
'oauth_signature_method' => $this->signer->getMethod(),
'oauth_timestamp' => time(),
'oauth_nonce' => $this->generateNonce(),
'oauth_timestamp' => time(),
'oauth_nonce' => $this->generateNonce(),
];

if ($this->realm) {
Expand All @@ -95,4 +95,4 @@ protected function generateNonce(): string
{
return bin2hex(random_bytes(8));
}
}
}
2 changes: 1 addition & 1 deletion src/Provider/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,4 @@ abstract protected function getTokenCredentialsUri(): string;
* Get the URI for user details from.
*/
abstract protected function getUserDetailsUri(): string;
}
}
12 changes: 6 additions & 6 deletions src/Provider/GenericProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function extractUserDetails(ResponseInterface $response): User

$user = ($this->userDetailsExtractor)($response, $this);

if (!$user instanceof User) {
if ( ! $user instanceof User) {
throw new InvalidArgumentException(sprintf(
'The configured extractor did not return an instance of %s.',
User::class
Expand All @@ -117,7 +117,7 @@ public function extractUserDetails(ResponseInterface $response): User
/**
* Configure the callback used to resolve user details. The callback will receive an instance
* of the response generated by the configured user details URI as well as this provider
* instance and must return an instance of `\League\OAuth1\Client\User`
* instance and must return an instance of `\League\OAuth1\Client\User`.
*/
public function extractUserDetailsUsing(callable $callback): GenericProvider
{
Expand All @@ -135,21 +135,21 @@ private function parseConfig(array $config): array
{
$defaults = [
'temporary_credentials' => [
'method' => 'GET',
'method' => 'GET',
'location' => RequestInjector::LOCATION_QUERY,

// Required
'uri' => null,
],
'authorization' => [
'method' => 'GET',
'method' => 'GET',
'location' => RequestInjector::LOCATION_QUERY,

// Required
'uri' => null,
],
'token_credentials' => [
'method' => 'POST',
'method' => 'POST',
'location' => RequestInjector::LOCATION_HEADER,

// Required
Expand All @@ -168,4 +168,4 @@ private function parseConfig(array $config): array

return array_replace_recursive($defaults, $config);
}
}
}
2 changes: 1 addition & 1 deletion src/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ public function prepareAuthenticatedRequest(
RequestInterface $request,
Credentials $tokenCredentials
): RequestInterface;
}
}
8 changes: 4 additions & 4 deletions src/RequestInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace League\OAuth1\Client;

use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use function GuzzleHttp\Psr7\build_query;
use function GuzzleHttp\Psr7\stream_for;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;

class RequestInjector
{
Expand Down Expand Up @@ -81,7 +81,7 @@ private function injectQuery(RequestInterface $request, array $oauthParameters):
*
* @return array<int, string>
*
* @link https://tools.ietf.org/html/rfc5849#section-3.5.1 Authorization Header
* @see https://tools.ietf.org/html/rfc5849#section-3.5.1 Authorization Header
*/
private function createAuthorizationHeader(array $oauthParameters): array
{
Expand All @@ -100,4 +100,4 @@ private function createAuthorizationHeader(array $oauthParameters): array

return ['Authorization', $value];
}
}
}
2 changes: 1 addition & 1 deletion src/Signature/BaseSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class BaseSigner implements Signer

public function __construct(ClientCredentials $clientCredentials, BaseStringBuilder $baseStringBuilder = null)
{
$this->clientCredentials = $clientCredentials;
$this->clientCredentials = $clientCredentials;
$this->baseStringBuilder = $baseStringBuilder ?: new BaseStringBuilder();
}
}
12 changes: 6 additions & 6 deletions src/Signature/BaseStringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace League\OAuth1\Client\Signature;

use Psr\Http\Message\RequestInterface;
use function GuzzleHttp\Psr7\parse_query;
use Psr\Http\Message\RequestInterface;

class BaseStringBuilder
{
Expand All @@ -17,7 +17,7 @@ class BaseStringBuilder
*
* @param array<string, string|int> $oauthParameters
*
* @link https://tools.ietf.org/html/rfc5849#section-3.4.1 Signature Base String
* @see https://tools.ietf.org/html/rfc5849#section-3.4.1 Signature Base String
*/
public function forRequest(RequestInterface $request, array $oauthParameters = []): string
{
Expand All @@ -36,8 +36,8 @@ public function forRequest(RequestInterface $request, array $oauthParameters = [
*
* @param array<string, string|int> $oauthParameters
*
* @link https://tools.ietf.org/html/rfc5849#section-3.4.1.3.1 Parameter Sources
* @link https://tools.ietf.org/html/rfc5849#section-3.4.1.3.2 Parameters Normalization
* @see https://tools.ietf.org/html/rfc5849#section-3.4.1.3.1 Parameter Sources
* @see https://tools.ietf.org/html/rfc5849#section-3.4.1.3.2 Parameters Normalization
*/
private function normalizeParameters(RequestInterface $request, array $oauthParameters = []): string
{
Expand Down Expand Up @@ -77,7 +77,7 @@ private function normalizeParameters(RequestInterface $request, array $oauthPara
// Parameters now need to be encoded prior to sorting
$parameters = array_map(static function (array $parameter): array {
return [
'key' => rawurlencode($parameter['key']),
'key' => rawurlencode($parameter['key']),
'value' => rawurlencode($parameter['value']),
];
}, $parameters);
Expand All @@ -101,4 +101,4 @@ private function normalizeParameters(RequestInterface $request, array $oauthPara
}, $parameters)
);
}
}
}
2 changes: 1 addition & 1 deletion src/Signature/HmacSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getMethod(): string
/**
* @param array<string, string|int> $oauthParameters
*
* @link https://tools.ietf.org/html/rfc5849#section-3.4.2 HMAC-SHA1
* @see https://tools.ietf.org/html/rfc5849#section-3.4.2 HMAC-SHA1
*/
public function sign(RequestInterface $request, array $oauthParameters, Credentials $contextCredentials = null): string
{
Expand Down

0 comments on commit c52005b

Please sign in to comment.