Skip to content

Commit

Permalink
Apply phpstan level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
cicnavi committed Jan 16, 2025
1 parent e45656d commit 71b51de
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 25 deletions.
15 changes: 8 additions & 7 deletions src/Codebooks/MetadataPolicyOperatorsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public static function validateGeneralParameterOperationRules(array $parameterOp
}

/**
* @param array<string,mixed> $parameterOperations
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
*/
public static function validateSpecificParameterOperationRules(array $parameterOperations): void
Expand All @@ -316,13 +317,13 @@ public static function validateSpecificParameterOperationRules(array $parameterO

// No special resolving rules for operator 'value', continue with 'add'.
if ($metadataPolicyOperatorEnum === MetadataPolicyOperatorsEnum::Add) {
/** @var array $operatorValue We ensured this is array. */
/** @var array<mixed> $operatorValue We ensured this is array. */
// If add is combined with subset_of, the values of add MUST be a subset of the values of
// subset_of.
if (
in_array(MetadataPolicyOperatorsEnum::SubsetOf->value, $parameterOperatorKeys, true)
) {
/** @var array $superset We ensured this is array. */
/** @var array<mixed> $superset We ensured this is array. */
$superset = $parameterOperations[
MetadataPolicyOperatorsEnum::SubsetOf->value
];
Expand All @@ -346,7 +347,7 @@ public static function validateSpecificParameterOperationRules(array $parameterO
true,
)
) {
/** @var array $subset We ensured this is array. */
/** @var array<mixed> $subset We ensured this is array. */
$subset = $parameterOperations[
MetadataPolicyOperatorsEnum::SupersetOf->value
];
Expand All @@ -366,7 +367,7 @@ public static function validateSpecificParameterOperationRules(array $parameterO
if (
in_array(MetadataPolicyOperatorsEnum::OneOf->value, $parameterOperatorKeys, true)
) {
/** @var array $superset We ensured this is array. */
/** @var array<mixed> $superset We ensured this is array. */
$superset = $parameterOperations[
MetadataPolicyOperatorsEnum::OneOf->value
];
Expand All @@ -386,7 +387,7 @@ public static function validateSpecificParameterOperationRules(array $parameterO
if (
in_array(MetadataPolicyOperatorsEnum::SubsetOf->value, $parameterOperatorKeys, true)
) {
/** @var array $superset We ensured this is array. */
/** @var array<mixed> $superset We ensured this is array. */
$superset = $parameterOperations[
MetadataPolicyOperatorsEnum::SubsetOf->value
];
Expand All @@ -410,7 +411,7 @@ public static function validateSpecificParameterOperationRules(array $parameterO
true,
)
) {
/** @var array $subset We ensured this is array. */
/** @var array<mixed> $subset We ensured this is array. */
$subset = $parameterOperations[
MetadataPolicyOperatorsEnum::SupersetOf->value
];
Expand Down Expand Up @@ -440,7 +441,7 @@ public static function validateSpecificParameterOperationRules(array $parameterO
true,
)
) {
/** @var array $subset We ensured this is array. */
/** @var array<mixed> $subset We ensured this is array. */
$subset = $parameterOperations[
MetadataPolicyOperatorsEnum::SupersetOf->value
];
Expand Down
13 changes: 11 additions & 2 deletions src/Federation/EntityStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getExpirationTime(): int

/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @return array[]
* @return array{keys:array<array<string,mixed>>}
* @psalm-suppress MixedReturnTypeCoercion
*/
public function getJwks(): array
Expand All @@ -98,6 +98,12 @@ public function getJwks(): array
throw new JwsException('Invalid JWKS encountered: ' . var_export($jwks, true));
}

$jwks[ClaimsEnum::Keys->value] = array_map(
$this->helpers->arr()->ensureStringKeys(...),
$jwks[ClaimsEnum::Keys->value],
);

/** @var array{keys:array<array<string,mixed>>} $jwks */
return $jwks;
}

Expand Down Expand Up @@ -153,6 +159,7 @@ public function getAuthorityHints(): ?array
}

/**
* @return ?array<string,mixed>
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\EntityStatementException
*/
Expand All @@ -172,12 +179,13 @@ public function getMetadata(): ?array
throw new EntityStatementException('Invalid Metadata claim.');
}

return $metadata;
return $this->helpers->arr()->ensureStringKeys($metadata);
}

/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\EntityStatementException
* @phpstan-ignore missingType.iterableValue (We will ensure proper format in policy resolver.)
*/
public function getMetadataPolicy(): ?array
{
Expand Down Expand Up @@ -273,6 +281,7 @@ public function isConfiguration(): bool

/**
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @phpstan-ignore missingType.iterableValue (Format is validated later.)
*/
public function verifyWithKeySet(?array $jwks = null, int $signatureIndex = 0): void
{
Expand Down
3 changes: 3 additions & 0 deletions src/Federation/EntityStatement/TrustMarkClaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function getTrustMark(): TrustMark
return $this->trustMark;
}

/**
* @return array<string,mixed>
*/
public function getOtherClaims(): array
{
return $this->otherClaims;
Expand Down
18 changes: 10 additions & 8 deletions src/Federation/MetadataPolicyApplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ public function __construct(
}

/**
* @param array $resolvedMetadataPolicy Resolved (validated) metadata policy.
* @param array<string,array<string,mixed>> $resolvedMetadataPolicy Resolved (validated) metadata policy.
* @param array<string,mixed> $metadata
* @return array<string,mixed> Metadata with applied policies.
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
*/
public function for(
array $resolvedMetadataPolicy,
array $metadata,
): array {
/**
* @var string $policyParameterName
* @var array<string,mixed> $policyOperations
*/
foreach ($resolvedMetadataPolicy as $policyParameterName => $policyOperations) {
foreach (MetadataPolicyOperatorsEnum::cases() as $metadataPolicyOperatorEnum) {
if (!array_key_exists($metadataPolicyOperatorEnum->value, $policyOperations)) {
continue;
}
/** @psalm-suppress MixedAssignment */
$operatorValue = $policyOperations[$metadataPolicyOperatorEnum->value];
/** @psalm-suppress MixedAssignment */
/** @psalm-suppress MixedAssignment, MixedArgumentTypeCoercion */
$metadataParameterValueBeforePolicy = $this->resolveParameterValueBeforePolicy(
$metadata,
$policyParameterName,
Expand Down Expand Up @@ -99,7 +97,7 @@ public function for(
$policyParameterName,
);

/** @var array $operatorValue */
/** @var array<mixed> $operatorValue Set bc of phpstan */
if (!in_array($metadataParameterValueBeforePolicy, $operatorValue, true)) {
throw new MetadataPolicyException(
sprintf(
Expand Down Expand Up @@ -152,7 +150,7 @@ public function for(
$policyParameterName,
);

/** @var array $operatorValue */
/** @var array<mixed> $operatorValue Set bc of phpstan */
if (
!$metadataPolicyOperatorEnum->isValueSupersetOf(
$metadataParameterValueBeforePolicy,
Expand Down Expand Up @@ -190,9 +188,13 @@ public function for(
}
}

/** @var array<string,mixed> $metadata */
return $metadata;
}

/**
* @param array<string,mixed> $metadata
*/
protected function resolveParameterValueBeforePolicy(array $metadata, string $parameter): mixed
{
/** @psalm-suppress MixedAssignment */
Expand Down
4 changes: 3 additions & 1 deletion src/Federation/MetadataPolicyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
* @return array<string,array<string,array<string,mixed>>>
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
* @psalm-suppress MixedAssignment
* @phpstan-ignore missingType.iterableValue (We validate it here)
*/
public function ensureFormat(array $metadataPolicies): array
{
Expand Down Expand Up @@ -55,7 +56,7 @@ public function ensureFormat(array $metadataPolicies): array
/**
* @param array<array<string,array<string,array<string,mixed>>>> $metadataPolicies
* @param string[] $criticalMetadataPolicyOperators
*
* @return array<string,array<string,mixed>>
* @throws \SimpleSAML\OpenID\Exceptions\MetadataPolicyException
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
*/
Expand All @@ -72,6 +73,7 @@ public function for(
/** @psalm-suppress MixedAssignment We'll check if $nextPolicy is array type. */
if (
(!array_key_exists($entityTypeEnum->value, $metadataPolicy)) ||
/** @phpstan-ignore booleanNot.alwaysFalse (Let's check for validity here.) */
(!is_array($nextPolicy = $metadataPolicy[$entityTypeEnum->value]))
) {
continue;
Expand Down
6 changes: 4 additions & 2 deletions src/Federation/RequestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,22 @@ public function getExpirationTime(): int
}

/**
* @return ?string[]
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\RequestObjectException
*/
public function getTrustChain(): ?array
{
$claimKey = ClaimsEnum::TrustChain->value;
/** @psalm-suppress MixedAssignment */
$trustChain = $this->getPayloadClaim(ClaimsEnum::TrustChain->value) ?? null;
$trustChain = $this->getPayloadClaim($claimKey) ?? null;

if (is_null($trustChain)) {
return null;
}

if (is_array($trustChain)) {
return $trustChain;
return $this->ensureNonEmptyStrings($trustChain, $claimKey);
}

throw new RequestObjectException(
Expand Down
10 changes: 8 additions & 2 deletions src/Federation/TrustChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class TrustChain implements JsonSerializable
/**
* Resolved metadata policy per entity type.
*
* @var array[]
* @var array<string,array<string,array<string,mixed>>>
*/
protected array $resolvedMetadataPolicy = [];

/**
* Resolved metadata (after applying resolved policy) per entity type.
*
* @var array<string,null|array>
* @var array<string,null|array<string,mixed>>
*/
protected array $resolvedMetadata = [];

Expand Down Expand Up @@ -122,6 +122,7 @@ public function getResolvedTrustAnchor(): EntityStatement
}

/**
* @return ?array<string,mixed>
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @throws \SimpleSAML\OpenID\Exceptions\JwsException
* @throws \SimpleSAML\OpenID\Exceptions\OpenIdException
Expand Down Expand Up @@ -433,17 +434,22 @@ protected function resolveMetadataFor(EntityTypesEnum $entityTypeEnum): void
// to it.
/** @psalm-suppress RiskyTruthyFalsyComparison */
if (empty($this->resolvedMetadataPolicy[$entityTypeEnum->value])) {
/** @var array<string,mixed> $leafMetadataEntityType */
$this->resolvedMetadata[$entityTypeEnum->value] = $leafMetadataEntityType;
return;
}

// Policy application to leaf metadata.
/** @var array<string,mixed> $leafMetadataEntityType */
$this->resolvedMetadata[$entityTypeEnum->value] = $this->metadataPolicyApplicator->for(
$this->resolvedMetadataPolicy[$entityTypeEnum->value],
$leafMetadataEntityType,
);
}

/**
* @return \SimpleSAML\OpenID\Federation\EntityStatement[]
*/
public function getEntities(): array
{
return $this->entities;
Expand Down
1 change: 1 addition & 0 deletions src/Federation/TrustChainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public function for(string $entityId, array $validTrustAnchorIds): TrustChainBag

/**
* @throws \SimpleSAML\OpenID\Exceptions\TrustChainException
* @phpstan-ignore missingType.iterableValue (We validate it here)
*/
protected function validateStart(string $entityId, array $validTrustAnchorIds): void
{
Expand Down
4 changes: 4 additions & 0 deletions src/Helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

class Arr
{
/**
* @phpstan-ignore missingType.iterableValue (We can handle mixed type)
*/
public function ensureArrayDepth(array &$array, int|string ...$keys): void
{
if (count($keys) > 99) {
Expand All @@ -30,6 +33,7 @@ public function ensureArrayDepth(array &$array, int|string ...$keys): void

/**
* @return array<string,mixed>
* @phpstan-ignore missingType.iterableValue (We can handle mixed type)
*/
public function ensureStringKeys(array $array): array
{
Expand Down
1 change: 1 addition & 0 deletions src/Helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function isValid(string $url): bool

/**
* Add (new) params to URL while preserving existing ones (if any).
* @param array<string,mixed> $params
*/
public function withParams(string $url, array $params): string
{
Expand Down
3 changes: 3 additions & 0 deletions src/Jwks/Factories/JwksFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class JwksFactory
{
/**
* @phpstan-ignore missingType.iterableValue (JWKS array is validated later)
*/
public function fromKeyData(array $jwks): JwksDecorator
{
return new JwksDecorator(JWKSet::createFromKeyData($jwks));
Expand Down
4 changes: 4 additions & 0 deletions src/Jwks/JwksDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function jwks(): JWKSet
return $this->jwks;
}

/**
* @return array{keys:array<array<string,mixed>>}
* @psalm-suppress MixedReturnTypeCoercion, MixedReturnTypeCoercion
*/
public function jsonSerialize(): array
{
return [
Expand Down
10 changes: 7 additions & 3 deletions src/Jwks/JwksFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
}

/**
* @return array{keys:array <string,mixed>}
* @return array{keys:array<array<string,mixed>>}
* @throws \SimpleSAML\OpenID\Exceptions\JwksException
*/
protected function decodeJwksJson(string $jwksJson): array
Expand Down Expand Up @@ -63,8 +63,12 @@ protected function decodeJwksJson(string $jwksJson): array
throw new JwksException($message);
}

$jwks[ClaimsEnum::Keys->value] = $this->helpers->arr()->ensureStringKeys($jwks[ClaimsEnum::Keys->value]);
/** @var array{keys:array<string,mixed>} $jwks */
$jwks[ClaimsEnum::Keys->value] = array_map(
$this->helpers->arr()->ensureStringKeys(...),
$jwks[ClaimsEnum::Keys->value],
);

/** @var array{keys:array<array<string,mixed>>} $jwks */
return $jwks;
}

Expand Down

0 comments on commit 71b51de

Please sign in to comment.