Skip to content

Commit

Permalink
Merge branch 'phpstan' into lti-lib-v6
Browse files Browse the repository at this point in the history
  • Loading branch information
dbhynds committed Dec 15, 2023
2 parents 6bfa83a + 901bd91 commit d344c34
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 58 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"jubeki/laravel-code-style": "^2.0",
"mockery/mockery": "^1.4",
"nesbot/carbon": "^2.43",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand All @@ -44,7 +45,10 @@
},
"scripts": {
"test": "phpunit",
"lint": "php-cs-fixer fix -v --dry-run",
"lint": [
"php-cs-fixer fix -v --dry-run",
"phpstan analyse src --level 5"
],
"lint-fix": "php-cs-fixer fix -v"
}
}
16 changes: 16 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Packback\\\\Lti1p3\\\\Interfaces\\\\IDatabase\\:\\:findLti1p1Keys\\(\\)\\.$#"
count: 1
path: src/LtiMessageLaunch.php

-
message: "#^Call to an undefined method Packback\\\\Lti1p3\\\\Interfaces\\\\IDatabase\\:\\:migrateFromLti1p1\\(\\)\\.$#"
count: 1
path: src/LtiMessageLaunch.php

-
message: "#^Call to an undefined method Packback\\\\Lti1p3\\\\Interfaces\\\\IDatabase\\:\\:shouldMigrate\\(\\)\\.$#"
count: 1
path: src/LtiMessageLaunch.php
4 changes: 4 additions & 0 deletions src/Interfaces/IServiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public function setAccept(string $accept): self;
public function setContentType(string $contentType): self;

public function getErrorPrefix(): string;

public function getMaskResponseLogs(): bool;

public function setMaskResponseLogs(bool $shouldMask): self;
}
2 changes: 1 addition & 1 deletion src/LtiAssignmentsGradesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function findLineItem(LtiLineitem $newLineItem): ?LtiLineitem
return null;
}

public function updateLineitem(LtiLineItem $lineitemToUpdate): LtiLineitem
public function updateLineitem(LtiLineitem $lineitemToUpdate): LtiLineitem
{
$request = new ServiceRequest(
ServiceRequest::METHOD_PUT,
Expand Down
20 changes: 10 additions & 10 deletions src/LtiLineitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __toString(): string
/**
* Static function to allow for method chaining without having to assign to a variable first.
*/
public static function new(?array $lineItem = null): LtiLineItem
public static function new(?array $lineItem = null): LtiLineitem
{
return new LtiLineitem($lineItem);
}
Expand All @@ -56,7 +56,7 @@ public function getId()
return $this->id;
}

public function setId($value): LtiLineItem
public function setId($value): LtiLineitem
{
$this->id = $value;

Expand All @@ -68,7 +68,7 @@ public function getLabel()
return $this->label;
}

public function setLabel($value): LtiLineItem
public function setLabel($value): LtiLineitem
{
$this->label = $value;

Expand All @@ -80,7 +80,7 @@ public function getScoreMaximum()
return $this->score_maximum;
}

public function setScoreMaximum($value): LtiLineItem
public function setScoreMaximum($value): LtiLineitem
{
$this->score_maximum = $value;

Expand All @@ -92,7 +92,7 @@ public function getResourceId()
return $this->resource_id;
}

public function setResourceId($value): LtiLineItem
public function setResourceId($value): LtiLineitem
{
$this->resource_id = $value;

Expand All @@ -104,7 +104,7 @@ public function getResourceLinkId()
return $this->resource_link_id;
}

public function setResourceLinkId($value): LtiLineItem
public function setResourceLinkId($value): LtiLineitem
{
$this->resource_link_id = $value;

Expand All @@ -116,7 +116,7 @@ public function getTag()
return $this->tag;
}

public function setTag($value): LtiLineItem
public function setTag($value): LtiLineitem
{
$this->tag = $value;

Expand All @@ -128,7 +128,7 @@ public function getStartDateTime()
return $this->start_date_time;
}

public function setStartDateTime($value): LtiLineItem
public function setStartDateTime($value): LtiLineitem
{
$this->start_date_time = $value;

Expand All @@ -140,7 +140,7 @@ public function getEndDateTime()
return $this->end_date_time;
}

public function setEndDateTime($value): LtiLineItem
public function setEndDateTime($value): LtiLineitem
{
$this->end_date_time = $value;

Expand All @@ -152,7 +152,7 @@ public function getGradesReleased(): ?bool
return $this->grades_released;
}

public function setGradesReleased(?bool $value): LtiLineItem
public function setGradesReleased(?bool $value): LtiLineitem
{
$this->grades_released = $value;

Expand Down
9 changes: 5 additions & 4 deletions src/LtiMessageLaunch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Packback\Lti1p3\Interfaces\ICache;
use Packback\Lti1p3\Interfaces\ICookie;
use Packback\Lti1p3\Interfaces\IDatabase;
use Packback\Lti1p3\Interfaces\ILtiDeployment;
use Packback\Lti1p3\Interfaces\ILtiRegistration;
use Packback\Lti1p3\Interfaces\ILtiServiceConnector;
use Packback\Lti1p3\Interfaces\IMigrationDatabase;
Expand Down Expand Up @@ -53,7 +54,7 @@ class LtiMessageLaunch
private array $request;
private array $jwt;
private ?ILtiRegistration $registration;
private ?LtiDeployment $deployment;
private ?ILtiDeployment $deployment;
private string $launch_id;

// See https://www.imsglobal.org/spec/security/v1p1#approved-jwt-signing-algorithms.
Expand Down Expand Up @@ -362,8 +363,8 @@ private function jwtAlgMatchesJwkKty(array $key): bool
{
$jwtAlg = $this->jwt['header']['alg'];

return isset(static::$ltiSupportedAlgs[$jwtAlg]) &&
static::$ltiSupportedAlgs[$jwtAlg] === $key['kty'];
return isset(self::$ltiSupportedAlgs[$jwtAlg]) &&
self::$ltiSupportedAlgs[$jwtAlg] === $key['kty'];
}

protected function validateState(): self
Expand Down Expand Up @@ -549,7 +550,7 @@ private function matchingLti1p1KeyExists(): bool
return false;
}

private function oauthConsumerKeySignMatches(Lti1p1Key $key): string
private function oauthConsumerKeySignMatches(Lti1p1Key $key): bool
{
return $this->jwt['body'][LtiConstants::LTI1P1]['oauth_consumer_key_sign'] === $this->getOauthSignature($key);
}
Expand Down
12 changes: 6 additions & 6 deletions src/LtiServiceConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Firebase\JWT\JWT;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;
use Packback\Lti1p3\Interfaces\ICache;
use Packback\Lti1p3\Interfaces\ILtiRegistration;
use Packback\Lti1p3\Interfaces\ILtiServiceConnector;
use Packback\Lti1p3\Interfaces\IServiceRequest;
use Psr\Http\Message\ResponseInterface;

class LtiServiceConnector implements ILtiServiceConnector
{
Expand Down Expand Up @@ -80,7 +80,7 @@ public function getAccessToken(ILtiRegistration $registration, array $scopes): s
return $tokenData['access_token'];
}

public function makeRequest(IServiceRequest $request): Response
public function makeRequest(IServiceRequest $request): ResponseInterface
{
$response = $this->client->request(
$request->getMethod(),
Expand All @@ -99,7 +99,7 @@ public function makeRequest(IServiceRequest $request): Response
return $response;
}

public function getResponseHeaders(Response $response): ?array
public function getResponseHeaders(ResponseInterface $response): ?array
{
$responseHeaders = $response->getHeaders();
array_walk($responseHeaders, function (&$value) {
Expand All @@ -109,7 +109,7 @@ public function getResponseHeaders(Response $response): ?array
return $responseHeaders;
}

public function getResponseBody(Response $response): ?array
public function getResponseBody(ResponseInterface $response): ?array
{
$responseBody = (string) $response->getBody();

Expand Down Expand Up @@ -182,8 +182,8 @@ public static function getLogMessage(
?array $responseBody
): string {
if ($request->getMaskResponseLogs()) {
$responseHeaders = static::maskValues($responseHeaders);
$responseBody = static::maskValues($responseBody);
$responseHeaders = self::maskValues($responseHeaders);
$responseBody = self::maskValues($responseBody);
}

$contextArray = [
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ServiceRequest implements IServiceRequest
public function __construct(
private string $method,
private string $url,
private string $type = self::UNSUPPORTED)
private string $type = self::TYPE_UNSUPPORTED)
{
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Certification/Lti13CertificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function buildJWT($data, $header)
return \implode('.', $segments);
}

return JWT::encode($data, $this->issuer['tool_private_key'], $alg, $this->issuer['kid']);
return JWT::encode($data, $this->issuer['tool_private_key'], $this->issuer['alg'], $this->issuer['kid']);
}

// tests
Expand Down Expand Up @@ -376,7 +376,7 @@ public function testLti1p1MigrationSuccessfullyMakesDeployment()
$db->shouldMigrate = true;
$db->createdDeployment = LtiDeployment::new($payload[LtiConstants::DEPLOYMENT_ID]);

$payload['exp'] = 3272987750; // To ensure signature matches
$payload['exp'] = '3272987750'; // To ensure signature matches
$payload[LtiConstants::LTI1P1] = [
'oauth_consumer_key' => $key->getKey(),
'oauth_consumer_key_sign' => $key->sign(
Expand Down Expand Up @@ -504,7 +504,7 @@ public function testInvalidCertificationCases()
// I couldn't find a better output function
echo PHP_EOL."--> TESTING INVALID TEST CASE: {$testCase}";

$jwt = $this->buildJWT($payload, $this->issuer, $jwtHeader);
$jwt = $this->buildJWT($payload, $this->issuer);
if (isset($payload['nonce'])) {
$this->cache->cacheNonce($payload['nonce'], static::STATE);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/LtiMessageLaunchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ public function testALaunchIsNotAResource()

public function tesGetLaunchDataForALaunch()
{
$launch = $this->getLaunchData($this->payload);
$launch = $this->fakeLaunch($this->payload);

$actual = $launch->getLaunchData();

Expand Down Expand Up @@ -891,6 +891,6 @@ private function buildJWT($data, $header)
return \implode('.', $segments);
}

return JWT::encode($data, $this->issuer['tool_private_key'], $alg, $this->issuer['kid']);
return JWT::encode($data, $this->issuer['tool_private_key'], $this->issuer['alg'], $this->issuer['kid']);
}
}
24 changes: 12 additions & 12 deletions tests/MessageValidators/DeepLinkMessageValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ class DeepLinkMessageValidatorTest extends TestCase
{
public function testItCanValidate()
{
$this->assertTrue(DeepLinkMessageValidator::canValidate(static::validJwtBody()));
$this->assertTrue(DeepLinkMessageValidator::canValidate(self::validJwtBody()));
}

public function testItCannotValidate()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
$jwtBody[LtiConstants::MESSAGE_TYPE] = 'some other type';

$this->assertFalse(DeepLinkMessageValidator::canValidate($jwtBody));
}

public function testJwtBodyIsValid()
{
$this->assertNull(DeepLinkMessageValidator::validate(static::validJwtBody()));
$this->assertNull(DeepLinkMessageValidator::validate(self::validJwtBody()));
}

public function testJwtBodyIsInvalidMissingSub()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
$jwtBody['sub'] = '';

$this->expectException(LtiException::class);
Expand All @@ -39,7 +39,7 @@ public function testJwtBodyIsInvalidMissingSub()

public function testJwtBodyIsInvalidMissingLtiVersion()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::VERSION]);

$this->expectException(LtiException::class);
Expand All @@ -49,7 +49,7 @@ public function testJwtBodyIsInvalidMissingLtiVersion()

public function testJwtBodyIsInvalidWrongLtiVersion()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
$jwtBody[LtiConstants::VERSION] = '1.2.0';

$this->expectException(LtiException::class);
Expand All @@ -59,7 +59,7 @@ public function testJwtBodyIsInvalidWrongLtiVersion()

public function testJwtBodyIsInvalidMissingRoles()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::ROLES]);

$this->expectException(LtiException::class);
Expand All @@ -69,7 +69,7 @@ public function testJwtBodyIsInvalidMissingRoles()

public function testJwtBodyIsInvalidMissingDeepLinkSetting()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::DL_DEEP_LINK_SETTINGS]);

$this->expectException(LtiException::class);
Expand All @@ -79,7 +79,7 @@ public function testJwtBodyIsInvalidMissingDeepLinkSetting()

public function testJwtBodyIsInvalidMissingDeepLinkReturnUrl()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::DL_DEEP_LINK_SETTINGS]['deep_link_return_url']);

$this->expectException(LtiException::class);
Expand All @@ -89,7 +89,7 @@ public function testJwtBodyIsInvalidMissingDeepLinkReturnUrl()

public function testJwtBodyIsInvalidMissingAcceptType()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::DL_DEEP_LINK_SETTINGS]['accept_types']);

$this->expectException(LtiException::class);
Expand All @@ -99,7 +99,7 @@ public function testJwtBodyIsInvalidMissingAcceptType()

public function testJwtBodyIsInvalidAcceptTypeIsInvalid()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
$jwtBody[LtiConstants::DL_DEEP_LINK_SETTINGS]['accept_types'] = [];

$this->expectException(LtiException::class);
Expand All @@ -109,7 +109,7 @@ public function testJwtBodyIsInvalidAcceptTypeIsInvalid()

public function testJwtBodyIsInvalidMissingPresentation()
{
$jwtBody = static::validJwtBody();
$jwtBody = self::validJwtBody();
unset($jwtBody[LtiConstants::DL_DEEP_LINK_SETTINGS]['accept_presentation_document_targets']);

$this->expectException(LtiException::class);
Expand Down
Loading

0 comments on commit d344c34

Please sign in to comment.