Skip to content

refactor: remove the code duplicated by the managers, factories and JWE components - #704

Merged
Spomky merged 1 commit into
4.3.xfrom
feature/shared-alias-registry
Aug 29, 2026
Merged

refactor: remove the code duplicated by the managers, factories and JWE components#704
Spomky merged 1 commit into
4.3.xfrom
feature/shared-alias-registry

Conversation

@Spomky

@Spomky Spomky commented Aug 28, 2026

Copy link
Copy Markdown
Member

Closes #689.

Three pieces of copy-pasted logic are extracted into @internal helpers. No BC break: every public method is kept, with the same signature.

1. The alias registries

AlgorithmManagerFactory, HeaderCheckerManagerFactory, ClaimCheckerManagerFactory, JWSSerializerManagerFactory and JWESerializerManagerFactory each implemented their own alias => object map. They now share Jose\Component\Core\Util\AliasedRegistry, a generic @internal trait 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:

factory exception message before after
AlgorithmManagerFactory InvalidArgumentException The algorithm with the alias "foo" is not supported. unchanged
HeaderCheckerManagerFactory InvalidArgumentException The header checker with the alias "foo" is not supported. unchanged
ClaimCheckerManagerFactory InvalidArgumentException The claim checker with the alias "foo" is not supported. unchanged
JWSSerializerManagerFactory UnsupportedSerializerException Unsupported serializer "foo". The JWS serializer with the alias "foo" is not supported.
JWESerializerManagerFactory UnsupportedSerializerException Unsupported serializer "foo". The JWE serializer with the alias "foo" is not supported.

The two serializer factories gain aliases() and their names() is deprecated (it still works and returns the same thing). The four bundle classes that called it use aliases() now.

2. checkDuplicatedHeaderParameters()

Duplicated verbatim in JWSBuilder, JWEBuilder, JWEDecrypter and HeaderCheckerManager. It moves to Jose\Component\Core\Util\HeaderParameterChecker::checkDuplicates(). The InvalidHeaderParameterException and its message are unchanged.

3. Splitting the algorithm manager in the JWE components

JWEBuilder::__construct() and JWEDecrypter::__construct() both split the manager they receive by instanceof. That moves to Jose\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 supported error. Dropping them is now deprecated and the ignored algorithms are named:

Passing algorithms that are neither key encryption nor content encryption algorithms to "Jose\Component\Encryption\JWEBuilder" is deprecated. The following algorithms are currently ignored and will be rejected in 5.0.0: HS256.

The behaviour itself is unchanged; only the deprecation is new.

Left for 5.0.0

  • Remove names() from both serializer factories.
  • Have JWEBuilder and JWEDecrypter accept 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.

@Spomky Spomky self-assigned this Aug 28, 2026
@Spomky Spomky added the feature label Aug 28, 2026
@Spomky Spomky added this to the 4.3.0 milestone Aug 28, 2026
…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
Spomky force-pushed the feature/shared-alias-registry branch from 0e0b700 to 42085dc Compare August 29, 2026 16:44
@Spomky
Spomky merged commit 37b16ff into 4.3.x Aug 29, 2026
3 checks passed
@Spomky
Spomky deleted the feature/shared-alias-registry branch August 29, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove the duplicated code shared by the managers, factories and JWE components

1 participant