-
-
Notifications
You must be signed in to change notification settings - Fork 3
Binary Flags v3 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Binary Flags v3 #16
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
25c8b2a
Update testing tools
reinder83 eb6f5e3
Update workflow for parallel tests
reinder83 97c0b52
Add named enum flags and split tests
reinder83 af99ca1
Add enum mask conversion guide
reinder83 a678a1e
Document enum conversion example
reinder83 0cd74fd
Run pint
reinder83 dec53a2
Add phpstan templates for enum flags and mask typing
reinder83 1b7164e
Add additional coverage tests for enum and mask edge cases
reinder83 7b526a8
Fix docblock and reducer order
reinder83 b2dee71
Fix enum flag docblock and reducer
reinder83 ca6744c
Correct flag traits docblocks
reinder83 f62be36
Prepare v3.0 release
reinder83 d29cd5b
Merge master into feature/v3.0
reinder83 a1d6163
Use int instead of mixed
reinder83 cfe08a8
Upgrade rector and tighten numeric flags typing
reinder83 d329973
Remove legacy trait phpstan stub
reinder83 e18f54c
Address Copilot review comments
reinder83 45bc816
Fix name for deprecation test
reinder83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,16 @@ | ||
| # Release Notes - v2.1.0 | ||
| # Release Notes - v3.0.0 | ||
|
|
||
| ## Added | ||
| - New enum-backed API: | ||
| - `BinaryEnumFlags` | ||
| - `Traits\InteractsWithEnumFlags` | ||
| - `Flag` enum and `Mask` value object | ||
| - Enum-backed flags now return a `Mask` object from `getMask()`. | ||
| - New `getMaskValue(): int` method for enum-backed flags to persist/interoperate with integer masks. | ||
| - Deprecation warnings for passing `float` values as masks/flags. | ||
| - README migration notice for the upcoming `v3.0.0` integer-only API. | ||
| - `UPGRADE-v3.md` with migration instructions. | ||
| - New primary numeric trait: `Traits\InteractsWithNumericFlags`. | ||
| - `Traits\BinaryFlags` is now deprecated and kept for backward compatibility. | ||
| ## Changed | ||
| - Numeric `BinaryFlags` APIs now accept `int` values only. | ||
| - Passing `float` values to numeric mask/flag methods now raises `TypeError` for `strict_types=1` callers. | ||
| - Non-strict callers should still cast or validate external values before calling the API because PHP scalar coercion can convert `float` to `int` at the call boundary. | ||
| - The numeric iterator and JSON serialization contracts are now documented as `int`-based. | ||
|
|
||
| ## Deprecated | ||
| - Passing `float` to BinaryFlags mask/flag methods is deprecated in `v2.1.0`. | ||
| - Float support will be removed in `v3.0.0`. | ||
| - `Bits::BIT_64` will be removed in `v3.0.0`. | ||
| ## Removed | ||
| - `Bits::BIT_64`. | ||
| - The v2.x float-normalization/deprecation path in `Traits\InteractsWithNumericFlags`. | ||
|
|
||
| ## BIT_64 Notice | ||
| `Bits::BIT_64` is being removed because PHP numbers for bitwise flags are signed. The 64th bit is the sign bit, so it cannot be used reliably as a normal flag. | ||
| `Bits::BIT_64` was removed because PHP numbers for bitwise flags are signed. The 64th bit is the sign bit, so it cannot be used reliably as a normal flag. | ||
|
|
||
| Using integer-compatible bits avoids these issues. | ||
|
|
||
| ## Migration Recommendation | ||
| Cast external/legacy mask and flag values to `int` before calling BinaryFlags methods. | ||
|
|
||
| ```php | ||
| $flags->setMask((int) $mask); | ||
| $flags->addFlag((int) $flag); | ||
| ``` | ||
|
|
||
| ## Enum Migration Example | ||
| ```php | ||
| // Before: numeric PermissionFlags | ||
| class PermissionFlags extends BinaryFlags | ||
| { | ||
| public const CAN_VIEW = Bits::BIT_1; | ||
| public const CAN_BOOK = Bits::BIT_2; | ||
| } | ||
|
|
||
| $flags = new PermissionFlags($storedMask); | ||
| $flags->addFlag(PermissionFlags::CAN_VIEW | PermissionFlags::CAN_BOOK); | ||
| $storedMask = $flags->getMask(); | ||
|
|
||
| // After: enum-backed PermissionFlags | ||
| enum Permission: int | ||
| { | ||
| case CanView = Bits::BIT_1; | ||
| case CanBook = Bits::BIT_2; | ||
| } | ||
|
|
||
| class PermissionFlags extends BinaryEnumFlags | ||
| { | ||
| protected static function getFlagEnumClass(): string | ||
| { | ||
| return Permission::class; | ||
| } | ||
| } | ||
|
|
||
| $flags = new PermissionFlags(Mask::fromInt($storedMask, Permission::class)); | ||
| $flags->addFlag(Permission::CanView); | ||
| $flags->addFlag(Permission::CanBook); | ||
| $storedMask = $flags->getMaskValue(); | ||
| ``` |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Reinder83\BinaryFlags; | ||
|
|
||
| use BackedEnum; | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Reinder83\BinaryFlags; | ||
|
|
||
| /** | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Reinder83\BinaryFlags; | ||
|
|
||
| use BackedEnum; | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Reinder83\BinaryFlags\Traits; | ||
|
|
||
| /** | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Reinder83\BinaryFlags\Traits; | ||
|
|
||
| use BackedEnum; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.