refactor: remove the code duplicated by the managers, factories and JWE components - #704
Merged
Merged
Conversation
…WE components The five factories of the library (algorithm manager, header and claim checker managers, JWS and JWE serializer managers) all implemented the same "alias => object" registry with five slightly different error messages. They now share an internal `AliasedRegistry` trait, so an unknown alias is reported the same way everywhere. Every public method is kept, including `names()` on the serializer factories, which is deprecated in favour of `aliases()`. The bundle factories are rebased on the same trait. `checkDuplicatedHeaderParameters()` was duplicated verbatim in `JWSBuilder`, `JWEBuilder`, `JWEDecrypter` and `HeaderCheckerManager`. It now lives in the internal `HeaderParameterChecker` helper. `JWEBuilder` and `JWEDecrypter` both split the algorithm manager they receive by `instanceof`. That split moves to the internal `EncryptionAlgorithmManagers` helper, which also reports the algorithms it drops: passing an algorithm that is neither a key encryption nor a content encryption algorithm is now deprecated instead of turning a misconfiguration into a much later "algorithm not supported" error.
Spomky
force-pushed
the
feature/shared-alias-registry
branch
from
August 29, 2026 16:44
0e0b700 to
42085dc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #689.
Three pieces of copy-pasted logic are extracted into
@internalhelpers. No BC break: every public method is kept, with the same signature.1. The alias registries
AlgorithmManagerFactory,HeaderCheckerManagerFactory,ClaimCheckerManagerFactory,JWSSerializerManagerFactoryandJWESerializerManagerFactoryeach implemented their ownalias => objectmap. They now shareJose\Component\Core\Util\AliasedRegistry, a generic@internaltrait holding the map,aliases(),all(), the registration and the lookup. The two bundle factories (Jose\Bundle\JoseFramework\Services\{Claim,Header}CheckerManagerFactory) are rebased on it too.The five error messages become one template,
The <what> with the alias "<alias>" is not supported.. Each factory keeps the exception class it threw before, passed to the shared lookup:AlgorithmManagerFactoryInvalidArgumentExceptionThe algorithm with the alias "foo" is not supported.HeaderCheckerManagerFactoryInvalidArgumentExceptionThe header checker with the alias "foo" is not supported.ClaimCheckerManagerFactoryInvalidArgumentExceptionThe claim checker with the alias "foo" is not supported.JWSSerializerManagerFactoryUnsupportedSerializerExceptionUnsupported serializer "foo".The JWS serializer with the alias "foo" is not supported.JWESerializerManagerFactoryUnsupportedSerializerExceptionUnsupported serializer "foo".The JWE serializer with the alias "foo" is not supported.The two serializer factories gain
aliases()and theirnames()is deprecated (it still works and returns the same thing). The four bundle classes that called it usealiases()now.2.
checkDuplicatedHeaderParameters()Duplicated verbatim in
JWSBuilder,JWEBuilder,JWEDecrypterandHeaderCheckerManager. It moves toJose\Component\Core\Util\HeaderParameterChecker::checkDuplicates(). TheInvalidHeaderParameterExceptionand its message are unchanged.3. Splitting the algorithm manager in the JWE components
JWEBuilder::__construct()andJWEDecrypter::__construct()both split the manager they receive byinstanceof. That moves toJose\Component\Encryption\Util\EncryptionAlgorithmManagers::split().That constructor also silently dropped any algorithm that is neither a key encryption nor a content encryption algorithm, so a misconfiguration only surfaced much later as an
algorithm not supportederror. Dropping them is now deprecated and the ignored algorithms are named:The behaviour itself is unchanged; only the deprecation is new.
Left for 5.0.0
names()from both serializer factories.JWEBuilderandJWEDecrypteraccept two explicit managers instead of one they have to split.Tests
tests/Component/Core/AliasedRegistryTest.php— the five factories list their aliases and reject an unknown alias with the same message and the expected exception class;names()still works and deprecates.tests/Component/Encryption/EncryptionAlgorithmManagerSplitTest.php— the split, and the deprecation of the dropped algorithms, for both the builder and the decrypter.Rebased on 4.3.x after #703, #706, #707 and #709. Local run: PHPUnit 1010 tests green, PHPStan (level max) 0 errors, ECS, Rector and Deptrac green. 7 now-obsolete entries were removed from the PHPStan baseline.