Refactor: Add BleedingEdgeToggle::withBleedingEdge() and use it instead of manual set/restore in tests#5915
Merged
staabm merged 2 commits intoJun 22, 2026
Conversation
…nual set/restore in tests - Add `BleedingEdgeToggle::withBleedingEdge(bool, callable)` that sets the toggle for the duration of the callback and restores the previous value in a `finally`, so the global toggle is never observably mutated outside the call. - Reject callbacks that return a `Generator` with a `ShouldNotHappenException`: holding the toggle across a `yield` would leak it into unrelated tests, so data providers must build their data sets inside the callback. - Replace the manual `isBleedingEdge()` backup + `setBleedingEdge()` restore (including `try`/`finally` blocks) in `TypeCombinatorTest`, `ConstantArrayTypeTest` and `ConstantArrayTypeBuilderTest` with `withBleedingEdge()`. - Restructure the `dataAccepts` and `dataGetArraySize` data providers so the yielded data sets are constructed inside `withBleedingEdge()` callbacks and returned, then `yield from`-ed, instead of holding the toggle across `yield`. - Add `BleedingEdgeToggleTest` covering toggling, restoration (including on exception), return value passing, the no-yield guard, and data-set production.
BleedingEdgeToggle::withBleedingEdge() and use it instead of manual set/restore in testsBleedingEdgeToggle::withBleedingEdge() and use it instead of manual set/restore in tests
VincentLanglet
approved these changes
Jun 22, 2026
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.
Summary
Several tests manually back up the global bleeding-edge toggle with
BleedingEdgeToggle::isBleedingEdge(), callsetBleedingEdge(), and restore the previous value afterwards — sometimes via ad-hoctry/finallyblocks, sometimes by restoring at the end of a data provider thatyields while the toggle is held. This is repetitive and, in data providers, fragile: holding the toggle across ayieldcan leak it into unrelated tests.This PR introduces a single helper,
BleedingEdgeToggle::withBleedingEdge(), that encapsulates the set/restore handling, and rewrites every manual set/restore site to use it.Changes
src/DependencyInjection/BleedingEdgeToggle.php: addwithBleedingEdge(bool $bleedingEdge, callable $callback). It backs up the current value, sets the toggle, runs the callback, and restores the previous value in afinally. It throwsShouldNotHappenExceptionif the callback returns aGenerator, because that would mean the toggle is held acrossyield.tests/PHPStan/Type/TypeCombinatorTest.php: replace the four identical backup/setBleedingEdge(true)/restore blocks intestUnion,testUnionInversed,testIntersect,testIntersectInversedwith awithBleedingEdge(true, ...)call that resolves the type strings inside the callback.tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php:testIsSuperTypeOf,testEqualsTreatsLegacyNullAndSealedMarkerAsEqual,testSealedness: replacetry/finallyset/restore withwithBleedingEdge(), returning the constructed types from the callback and asserting on them afterwards.dataAccepts,dataGetArraySize: build the data sets insidewithBleedingEdge()callbacks andyield fromthe returned arrays, instead of holding the toggle acrossyield.tests/PHPStan/Type/Constant/ConstantArrayTypeBuilderTest.php:testGetArraySealedEmptyStaysConstantArrayTypenow constructs its array viawithBleedingEdge(true, ...).tests/PHPStan/DependencyInjection/BleedingEdgeToggleTest.php(new): unit tests for the helper.src/DependencyInjection/ContainerFactory.phpis intentionally left untouched: it performs a one-way set when building the container, not a set/restore, so it is not a candidate forwithBleedingEdge().Root cause
This is a refactor, not a bug fix. The repeated
isBleedingEdge()backup +setBleedingEdge()restore pattern was scattered across tests. The data-provider variants additionally restored the toggle only when the generator was fully consumed, so the toggle stayed mutated while PHPUnit held the generator open betweenyields.withBleedingEdge()centralizes the set/restore in afinallyand forbids returning aGenerator, forcing data providers to construct their objects while the toggle is set and return them, eliminating the leak.Test
BleedingEdgeToggleTestverifies: the toggle is set during the callback and restored afterwards (both when previously disabled and enabled), the callback's return value is propagated, the previous value is restored even when the callback throws, returning aGeneratorthrowsShouldNotHappenExceptionwhile still restoring the toggle, and data sets are produced while the toggle is set.TypeCombinatorTest,ConstantArrayTypeTest,ConstantArrayTypeBuilderTest) continue to pass, confirming behavior is preserved.Fixes phpstan/phpstan#14859