Skip to content

Commit

Permalink
API Strongly type everything
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed May 3, 2022
1 parent cc1d219 commit a99055b
Show file tree
Hide file tree
Showing 110 changed files with 289 additions and 1,831 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -17,9 +17,11 @@ matrix:
- php: 7.4
env: DB=PGSQL PHPUNIT_TEST=1 PHPCS_TEST=1
- php: 8.0
env: DB=MYSQL PDO=1 PHPUNIT_TEST=1
env: DB=MYSQL PDO=1 PHPUNIT_TEST=1 #PHPUNIT_COVERAGE_TEST=1
- php: 7.4
env: DB=MYSQL BEHAT_TEST=1 BEHAT_SUITE="asset-admin --config vendor/silverstripe/asset-admin/behat.yml"
- php: 8.0
env: DB=PGSQL PHPUNIT_TEST=1
- php: 8.0
env: DB=MYSQL BEHAT_TEST=1 BEHAT_SUITE="silverstripe-elemental --config vendor/dnadesign/silverstripe-elemental/behat.yml"
- php: 8.1.0
Expand Down
2 changes: 1 addition & 1 deletion _config.php
Expand Up @@ -3,4 +3,4 @@
// Set to 3.0.0 to show APIs marked for removal at that version
use SilverStripe\Dev\Deprecation;

Deprecation::notification_version('2.0.0', 'silverstripe/graphql');
Deprecation::notification_version('4.0.0', 'silverstripe/graphql');
9 changes: 6 additions & 3 deletions composer.json
Expand Up @@ -5,15 +5,18 @@
"license": "BSD-3-Clause",
"require": {
"php": "^7.4 || ^8.0",
"silverstripe/framework": "^4.10",
"silverstripe/framework": "^4.11",
"silverstripe/vendor-plugin": "^1.0",
"webonyx/graphql-php": "^14.0",
"silverstripe/event-dispatcher": "^0.1.2"
"silverstripe/event-dispatcher": "^0.1.2",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^2",
"m1/env": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.0",
"silverstripe/versioned": "^1.0@dev"
"silverstripe/versioned": "^1.0@dev"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/AuthenticatorInterface.php
Expand Up @@ -37,13 +37,13 @@ interface AuthenticatorInterface
* @return Member If authentication is successful
* @throws ValidationException If authentication fails
*/
public function authenticate(HTTPRequest $request);
public function authenticate(HTTPRequest $request): ?Member;

/**
* Determine if this authenticator is applicable to the current request
*
* @param HTTPRequest $request
* @return bool
*/
public function isApplicable(HTTPRequest $request);
public function isApplicable(HTTPRequest $request): bool;
}
9 changes: 5 additions & 4 deletions src/Auth/BasicAuthAuthenticator.php
Expand Up @@ -6,24 +6,25 @@
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Security\BasicAuth;
use SilverStripe\Security\Member;

/**
* An authenticator using SilverStripe's BasicAuth
*/
class BasicAuthAuthenticator implements AuthenticatorInterface
{
public function authenticate(HTTPRequest $request)
public function authenticate(HTTPRequest $request): ?Member
{
try {
return BasicAuth::requireLogin($request, 'Restricted resource');
return BasicAuth::requireLogin($request, 'Restricted resource') ?: null;
} catch (HTTPResponse_Exception $ex) {
// BasicAuth::requireLogin may throw its own exception with an HTTPResponse in it
$failureMessage = (string) $ex->getResponse()->getBody();
throw new ValidationException($failureMessage, 401);
}
}

public function isApplicable(HTTPRequest $request)
public function isApplicable(HTTPRequest $request): bool
{
if ($this->hasAuthHandler('HTTP_AUTHORIZATION')
|| $this->hasAuthHandler('REDIRECT_HTTP_AUTHORIZATION')
Expand All @@ -42,7 +43,7 @@ public function isApplicable(HTTPRequest $request)
* @param string $servervar
* @return bool
*/
protected function hasAuthHandler($servervar)
protected function hasAuthHandler(string $servervar): bool
{
return isset($_SERVER[$servervar]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$servervar] ?? '');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Auth/Handler.php
Expand Up @@ -65,7 +65,7 @@ public function requireAuthentication(HTTPRequest $request)
* @param HTTPRequest $request
* @return null|AuthenticatorInterface
*/
public function getAuthenticator(HTTPRequest $request)
public function getAuthenticator(HTTPRequest $request): ?AuthenticatorInterface
{
// Get list of default authenticators
$authenticators = $this->config()->get('authenticators');
Expand All @@ -89,7 +89,7 @@ public function getAuthenticator(HTTPRequest $request)
* @return AuthenticatorInterface
* @throws ValidationException
*/
protected function buildAuthenticator($authenticator)
protected function buildAuthenticator(string $authenticator): AuthenticatorInterface
{
if (!ClassInfo::classImplements($authenticator, AuthenticatorInterface::class)) {
throw new ValidationException(
Expand All @@ -106,7 +106,7 @@ protected function buildAuthenticator($authenticator)
*
* @param array $authenticators
*/
public function prioritiseAuthenticators(&$authenticators)
public function prioritiseAuthenticators(array &$authenticators): void
{
usort($authenticators, function ($a, $b) {
// Set some default values
Expand Down
11 changes: 3 additions & 8 deletions src/Auth/MemberAuthenticator.php
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\GraphQL\Auth;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;

/**
Expand All @@ -19,18 +20,12 @@
*/
class MemberAuthenticator implements AuthenticatorInterface
{
public function authenticate(HTTPRequest $request)
public function authenticate(HTTPRequest $request): ?Member
{
return Security::getCurrentUser();
}

/**
* Determine if this authenticator is applicable to the current request
*
* @param HTTPRequest $request
* @return bool
*/
public function isApplicable(HTTPRequest $request)
public function isApplicable(HTTPRequest $request): bool
{
$user = Security::getCurrentUser();
return !empty($user);
Expand Down
5 changes: 1 addition & 4 deletions src/Config/Configuration.php
Expand Up @@ -52,12 +52,9 @@ public function get($path, $default = null)
}

/**
* @param $path
* @param callable $callback
* @return $this
* @throws SchemaBuilderException
*/
private function path($path, $callback): void
private function path($path, callable $callback): void
{
if (is_string($path)) {
$path = explode('.', $path ?? '');
Expand Down
5 changes: 1 addition & 4 deletions src/Config/ModelConfiguration.php
Expand Up @@ -46,12 +46,9 @@ public function getOperationConfig(string $operation): array
}

/**
* @param string $class
* @param array $mapping
* @return string
* @throws SchemaBuilderException
*/
public function getTypeName(string $class, $mapping = []): string
public function getTypeName(string $class, array $mapping = []): string
{
$typeName = $this->formatClass($class);
$prefix = $this->getPrefix($class);
Expand Down

0 comments on commit a99055b

Please sign in to comment.