Skip to content

Commit

Permalink
Move to a super-set of PSR-12 for stricter coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Dec 12, 2024
1 parent 94dd102 commit 0d9cfca
Show file tree
Hide file tree
Showing 51 changed files with 1,026 additions and 772 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
tools: "composer"
- uses: "ramsey/composer-install@v3"
- name: "Check coding standards"
run: "./vendor/bin/phpcs src --standard=psr2 -sp --colors"
run: "./vendor/bin/phpcs"

coverage:
name: "Coverage"
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"description": "OAuth 2.0 Client Library",
"license": "MIT",
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
Expand All @@ -17,6 +20,7 @@
"mockery/mockery": "^1.6",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpunit/phpunit": "^10.5 || ^11.5",
"ramsey/coding-standard": "^2.2",
"squizlabs/php_codesniffer": "^3.11"
},
"keywords": [
Expand Down
27 changes: 27 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="cache" value="build/phpcs.cache"/>
<arg name="extensions" value="php"/>
<arg name="colors"/>
<arg value="sp"/>

<file>./src</file>
<file>./test</file>

<rule ref="Ramsey">
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming"/>
<exclude name="SlevomatCodingStandard.Commenting.DocCommentSpacing.IncorrectAnnotationsGroup"/>
</rule>

<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces">
<properties>
<property name="ignoreSpacesInComment" value="true"/>
</properties>
</rule>

</ruleset>
14 changes: 10 additions & 4 deletions src/Grant/AbstractGrant.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,10 +13,14 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

use League\OAuth2\Client\Tool\RequiredParameterTrait;

use function array_merge;

/**
* Represents a type of authorization grant.
*
Expand Down Expand Up @@ -43,7 +48,7 @@ abstract protected function getName();
/**
* Returns a list of all required request parameters.
*
* @return array
* @return list<string>
*/
abstract protected function getRequiredRequestParameters();

Expand All @@ -62,9 +67,10 @@ public function __toString()
* Prepares an access token request's parameters by checking that all
* required parameters are set, then merging with any given defaults.
*
* @param array $defaults
* @param array $options
* @return array
* @param array<string, mixed> $defaults
* @param array<string, mixed> $options
*
* @return array<string, mixed>
*/
public function prepareRequestParameters(array $defaults, array $options)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Grant/AuthorizationCode.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Grant/ClientCredentials.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Grant/Exception/InvalidGrantException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,14 +13,16 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant\Exception;

use InvalidArgumentException;

/**
* Exception thrown if the grant does not extend from AbstractGrant.
*
* @see League\OAuth2\Client\Grant\AbstractGrant
* @see \League\OAuth2\Client\Grant\AbstractGrant
*/
class InvalidGrantException extends InvalidArgumentException
{
Expand Down
40 changes: 22 additions & 18 deletions src/Grant/GrantFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,28 +13,34 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

use League\OAuth2\Client\Grant\Exception\InvalidGrantException;

use function is_object;
use function is_subclass_of;
use function sprintf;
use function str_replace;
use function ucwords;

/**
* Represents a factory used when retrieving an authorization grant type.
*/
class GrantFactory
{
/**
* @var array
* @var array<string, AbstractGrant>
*/
protected $registry = [];
protected array $registry = [];

/**
* Defines a grant singleton in the registry.
*
* @param string $name
* @param AbstractGrant $grant
* @return self
*/
public function setGrant($name, AbstractGrant $grant)
public function setGrant(string $name, AbstractGrant $grant)
{
$this->registry[$name] = $grant;

Expand All @@ -45,12 +52,11 @@ public function setGrant($name, AbstractGrant $grant)
*
* If the grant has not be registered, a default grant will be loaded.
*
* @param string $name
* @return AbstractGrant
*/
public function getGrant($name)
public function getGrant(string $name)
{
if (empty($this->registry[$name])) {
if (!isset($this->registry[$name])) {
$this->registerDefaultGrant($name);
}

Expand All @@ -60,44 +66,42 @@ public function getGrant($name)
/**
* Registers a default grant singleton by name.
*
* @param string $name
* @return self
*/
protected function registerDefaultGrant($name)
protected function registerDefaultGrant(string $name)
{
// PascalCase the grant. E.g: 'authorization_code' becomes 'AuthorizationCode'
$class = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
$class = 'League\\OAuth2\\Client\\Grant\\' . $class;

$this->checkGrant($class);

return $this->setGrant($name, new $class);
return $this->setGrant($name, new $class());
}

/**
* Determines if a variable is a valid grant.
*
* @param mixed $class
* @return boolean
* @return bool
*/
public function isGrant($class)
public function isGrant(mixed $class)
{
return is_subclass_of($class, AbstractGrant::class);
}

/**
* Checks if a variable is a valid grant.
*
* @throws InvalidGrantException
* @param mixed $class
* @return void
*
* @throws InvalidGrantException
*/
public function checkGrant($class)
public function checkGrant(mixed $class)
{
if (!$this->isGrant($class)) {
throw new InvalidGrantException(sprintf(
'Grant "%s" must extend AbstractGrant',
is_object($class) ? get_class($class) : $class
is_object($class) ? $class::class : $class,
));
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Grant/Password.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Grant/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\Grant;

/**
Expand Down
9 changes: 8 additions & 1 deletion src/OptionProvider/HttpBasicAuthOptionProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,12 +13,18 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\OptionProvider;

use InvalidArgumentException;

use function base64_encode;
use function sprintf;

/**
* Add http basic auth into access token request options
*
* @link https://tools.ietf.org/html/rfc6749#section-2.3.1
*/
class HttpBasicAuthOptionProvider extends PostAuthOptionProvider
Expand All @@ -27,7 +34,7 @@ class HttpBasicAuthOptionProvider extends PostAuthOptionProvider
*/
public function getAccessTokenOptions($method, array $params)
{
if (empty($params['client_id']) || empty($params['client_secret'])) {
if (!isset($params['client_id']) || !isset($params['client_secret'])) {
throw new InvalidArgumentException('clientId and clientSecret are required for http basic auth');
}

Expand Down
11 changes: 7 additions & 4 deletions src/OptionProvider/OptionProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\OptionProvider;

/**
Expand All @@ -22,9 +25,9 @@ interface OptionProviderInterface
/**
* Builds request options used for requesting an access token.
*
* @param string $method
* @param array $params
* @return array
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*/
public function getAccessTokenOptions($method, array $params);
public function getAccessTokenOptions(string $method, array $params);
}
6 changes: 5 additions & 1 deletion src/OptionProvider/PostAuthOptionProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the league/oauth2-client library
*
Expand All @@ -12,6 +13,8 @@
* @link https://github.com/thephpleague/oauth2-client GitHub
*/

declare(strict_types=1);

namespace League\OAuth2\Client\OptionProvider;

use League\OAuth2\Client\Provider\AbstractProvider;
Expand Down Expand Up @@ -41,7 +44,8 @@ public function getAccessTokenOptions($method, array $params)
/**
* Returns the request body for requesting an access token.
*
* @param array $params
* @param array<string, mixed> $params
*
* @return string
*/
protected function getAccessTokenBody(array $params)
Expand Down
Loading

0 comments on commit 0d9cfca

Please sign in to comment.