Skip to content

Commit

Permalink
PHP 8.1 update for feedback (#49)
Browse files Browse the repository at this point in the history
Changes detailed in the [draft updating documentation](https://github.com/celtic-project/LTI-PHP/wiki/Updating)
  • Loading branch information
spvickers committed Apr 3, 2023
1 parent f8e4852 commit 30796f8
Show file tree
Hide file tree
Showing 108 changed files with 9,168 additions and 9,694 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
}
],
"require": {
"php": ">=5.6.0",
"firebase/php-jwt": ">=5.5.0 <=6.0.0"
"php": ">=8.1.0",
"firebase/php-jwt": "^6.3"
},
"autoload": {
"psr-4": {
Expand Down
64 changes: 33 additions & 31 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types=1);

namespace ceLTIc\LTI;

use ceLTIc\LTI\Tool;
use ceLTIc\LTI\Http\HttpMessage;
use ceLTIc\LTI\Util;

/**
* Class to represent an HTTP message
Expand All @@ -21,52 +23,52 @@ class AccessToken
*
* @var string|null $token
*/
public $token = null;
public ?string $token = null;

/**
* Timestamp at which the token string expires.
*
* @var datetime|null $expires
* @var int|null $expires
*/
public $expires = null;
public ?int $expires = null;

/**
* Scope(s) for which the access token is valid.
*
* @var array $scopes
*/
public $scopes = array();
public ?array $scopes = [];

/**
* Platform for this context.
* Platform for this token.
*
* @var Platform|null $platform
* @var Platform $platform
*/
private $platform = null;
private Platform $platform;

/**
* Timestamp for when the object was created.
*
* @var int|null $created
*/
public $created = null;
public ?int $created = null;

/**
* Timestamp for when the object was last updated.
*
* @var int|null $updated
*/
public $updated = null;
public ?int $updated = null;

/**
* Class constructor.
*
* @param Platform $platform Platform
* @param array|null $scopes Scopes for which the access token is valid
* @param string $token Access token string
* @param datetime $expires Time in seconds after which the token string will expire
* @param Platform $platform Platform
* @param array|null $scopes Scopes for which the access token is valid
* @param string $token Access token string
* @param int $expires Time in seconds after which the token string will expire
*/
public function __construct($platform, $scopes = null, $token = null, $expires = null)
public function __construct(Platform $platform, ?array $scopes = null, ?string $token = null, ?int $expires = null)
{
$this->platform = $platform;
$this->scopes = $scopes;
Expand All @@ -88,27 +90,27 @@ public function __construct($platform, $scopes = null, $token = null, $expires =
*
* @return Platform Platform object for this resource link.
*/
public function getPlatform()
public function getPlatform(): Platform
{
return $this->platform;
}

/**
* Load a nonce value from the database.
*
* @return bool True if the nonce value was successfully loaded
* @return bool True if the nonce value was successfully loaded
*/
public function load()
public function load(): bool
{
return $this->platform->getDataConnector()->loadAccessToken($this);
}

/**
* Save a nonce value in the database.
*
* @return bool True if the nonce value was successfully saved
* @return bool True if the nonce value was successfully saved
*/
public function save()
public function save(): bool
{
sort($this->scopes);
return $this->platform->getDataConnector()->saveAccessToken($this);
Expand All @@ -117,11 +119,11 @@ public function save()
/**
* Check if a valid access token exists for a specific scope (or any scope if none specified).
*
* @param string $scope Access scope
* @param string $scope Access scope
*
* @return bool True if there is an unexpired access token for specified scope
* @return bool True if there is an unexpired access token for specified scope
*/
public function hasScope($scope = '')
public function hasScope(string $scope = ''): bool
{
if (substr($scope, -9) === '.readonly') {
$scope2 = substr($scope, 0, -9);
Expand All @@ -135,17 +137,17 @@ public function hasScope($scope = '')
/**
* Obtain a valid access token for a scope.
*
* @param string $scope Access scope
* @param bool $scopeOnly If true, a token is requested just for the specified scope
* @param string $scope Access scope
* @param bool $scopeOnly If true, a token is requested just for the specified scope
*
* @return AccessToken New access token
* @return AccessToken New access token
*/
public function get($scope = '', $scopeOnly = false)
public function get(string $scope = '', bool $scopeOnly = false): AccessToken
{
$url = $this->platform->accessTokenUrl;
if (!empty($url) && !empty(Tool::$defaultTool) && !empty(Tool::$defaultTool->rsaKey)) {
if ($scopeOnly) {
$scopesRequested = array($scope);
$scopesRequested = [$scope];
} else {
$scopesRequested = Tool::$defaultTool->requiredScopes;
if (substr($scope, -9) === '.readonly') {
Expand All @@ -162,11 +164,11 @@ public function get($scope = '', $scopeOnly = false)
do {
$method = 'POST';
$type = 'application/x-www-form-urlencoded';
$body = array(
$body = [
'grant_type' => 'client_credentials',
'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
'scope' => implode(' ', $scopesRequested)
);
];
if (!empty(Tool::$defaultTool)) {
Tool::$defaultTool->platform = $this->platform;
$body = Tool::$defaultTool->signServiceRequest($url, $method, $type, $body);
Expand All @@ -175,7 +177,7 @@ public function get($scope = '', $scopeOnly = false)
}
$http = new HttpMessage($url, $method, $body, 'Accept: application/json');
if ($http->send() && !empty($http->response)) {
$http->responseJson = json_decode($http->response);
$http->responseJson = Util::json_decode($http->response);
if (!is_null($http->responseJson) && !empty($http->responseJson->access_token) && !empty($http->responseJson->expires_in)) {
if (isset($http->responseJson->scope)) {
$scopesAccepted = explode(' ', $http->responseJson->scope);
Expand All @@ -194,7 +196,7 @@ public function get($scope = '', $scopeOnly = false)
$retry = false;
} elseif (!empty($scope) && (count($scopesRequested) > 1)) { // Just ask for the single scope requested
$retry = true;
$scopesRequested = array($scope);
$scopesRequested = [$scope];
}
} while ($retry);
}
Expand Down
36 changes: 19 additions & 17 deletions src/ApiHook/ApiContext.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
declare(strict_types=1);

namespace ceLTIc\LTI\ApiHook;

use ceLTIc\LTI\Service;
use ceLTIc\LTI\Context;
use ceLTIc\LTI\Enum\ToolSettingsMode;

/**
* Class to implement context services for a platform via its proprietary API
Expand All @@ -17,16 +19,16 @@ class ApiContext
/**
* Context object.
*
* @var \ceLTIc\LTI\Context|null $context
* @var Context $context
*/
protected $context = null;
protected Context $context;

/**
* Class constructor.
*
* @param \ceLTIc\LTI\Context $context
* @param Context $context
*/
public function __construct($context)
public function __construct(Context $context)
{
$this->context = $context;
}
Expand All @@ -36,7 +38,7 @@ public function __construct($context)
*
* @return bool True if the API hook has been configured
*/
public function isConfigured()
public function isConfigured(): bool
{
return true;
}
Expand All @@ -46,44 +48,44 @@ public function isConfigured()
*
* @return bool True if the request was successful
*/
public function getGroups()
public function getGroups(): bool
{
return false;
}

/**
* Get Memberships.
*
* @param bool $withGroups True is group information is to be requested as well
* @param bool $withGroups True is group information is to be requested as well
*
* @return mixed The array of UserResult objects if successful, otherwise false
* @return array|bool The array of UserResult objects if successful, otherwise false
*/
public function getMemberships($withGroups)
public function getMemberships(bool $withGroups): array|bool
{
return false;
}

/**
* Get Tool Settings.
*
* @param int $mode Mode for request (optional, default is current level only)
* @param bool $simple True if all the simple media type is to be used (optional, default is true)
* @param ToolSettingsMode|null $mode Mode for request (optional, default is current level only)
* @param bool $simple True if all the simple media type is to be used (optional, default is true)
*
* @return mixed The array of settings if successful, otherwise false
* @return array|bool The array of settings if successful, otherwise false
*/
public function getToolSettings($mode = Service\ToolSettings::MODE_CURRENT_LEVEL, $simple = true)
public function getToolSettings(?ToolSettingsMode $mode = null, bool $simple = true): array|bool
{
return false;
}

/**
* Perform a Tool Settings service request.
*
* @param array $settings An associative array of settings (optional, default is none)
* @param array $settings An associative array of settings (optional, default is none)
*
* @return bool True if action was successful, otherwise false
* @return bool True if action was successful, otherwise false
*/
public function setToolSettings($settings = array())
public function setToolSettings(array $settings = []): bool
{
return false;
}
Expand Down
Loading

0 comments on commit 30796f8

Please sign in to comment.