Skip to content

Commit a25342f

Browse files
committed
require php 8.3
1 parent f27a295 commit a25342f

File tree

6 files changed

+68
-42
lines changed

6 files changed

+68
-42
lines changed

Diff for: .github/workflows/php.yml

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
name: PHP Composer
22

33
on:
4-
push:
5-
branches: [ "master" ]
6-
pull_request:
7-
branches: [ "master" ]
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
branches: ["master"]
88

99
permissions:
10-
contents: read
10+
contents: read
1111

1212
jobs:
13-
build:
13+
build:
14+
runs-on: ubuntu-latest
1415

15-
runs-on: ubuntu-latest
16+
steps:
17+
- name: "Checkout"
18+
uses: actions/checkout@v3
1619

17-
steps:
18-
- name: "Checkout"
19-
uses: actions/checkout@v3
20+
- name: "Install PHP"
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: "8.3"
24+
tools: composer
2025

21-
- name: "Install PHP"
22-
uses: shivammathur/setup-php@v2
23-
with:
24-
php-version: '8.2'
25-
tools: composer
26+
- name: Install dependencies
27+
run: composer install --no-interaction
2628

27-
- name: Install dependencies
28-
run: composer install --no-interaction
29-
30-
- name: Run test suite
31-
run: composer test
29+
- name: Run test suite
30+
run: composer test

Diff for: README.md

+38-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![GitHub license](https://img.shields.io/github/license/PabloJoan/feature.svg)](https://github.com/PabloJoan/feature/blob/master/LICENSE)
22

3-
Requires PHP 8.2 and above.
3+
Requires PHP 8.3 and above.
44

55
# Installation
66

@@ -33,7 +33,7 @@ $featureConfigs = [
3333

3434
$features = new Features($featureConfigs);
3535

36-
$features->isEnabled(featureName: 'foo'); // true
36+
$features->isEnabled(featureName: 'foo'); // true
3737
$features->getEnabledVariant(featureName: 'foo'); // 'variant1'
3838
```
3939

@@ -50,27 +50,34 @@ A feature can be completely enabled, completely disabled, or something in
5050
between and can comprise a number of related variants.
5151

5252
The two main API entry points are:
53+
5354
```php
5455
$features->isEnabled(featureName: 'my_feature')
5556
```
57+
5658
which returns true when `my_feature` is enabled and, for multi-variant features:
59+
5760
```php
5861
$features->getEnabledVariant(featureName: 'my_feature')
5962
```
63+
6064
which returns the name of the particular variant which should be used.
6165

6266
The single argument to each of these methods is the name of the
6367
feature to test.
6468

6569
A typical use of `$features->isEnabled` for a single-variant feature
6670
would look something like this:
71+
6772
```php
6873
if ($features->isEnabled(featureName: 'my_feature')) {
6974
// do stuff
7075
}
7176
```
77+
7278
For a multi-variant feature, we can determine the appropriate code to run for
7379
each variant with something like this:
80+
7481
```php
7582
switch ($features->getEnabledVariant(featureName: 'my_feature')) {
7683
case 'foo':
@@ -81,8 +88,10 @@ each variant with something like this:
8188
break;
8289
}
8390
```
91+
8492
If a feature is bucketed by id, then we pass the id string to
8593
`$features->isEnabled` and `$features->getEnabledVariant` as a second parameter
94+
8695
```php
8796
$isMyFeatureEnabled = $features->isEnabled(
8897
featureName: 'my_feature',
@@ -95,30 +104,38 @@ If a feature is bucketed by id, then we pass the id string to
95104
);
96105
```
97106

98-
99107
## Configuration cookbook
100108

101109
There are a number of common configurations so before I explain the complete
102110
syntax of the feature configuration stanzas, here are some of the more common
103111
cases along with the most concise way to write the configuration.
104112

105113
### A totally enabled feature:
114+
106115
```php
107116
$server_config['foo'] = ['variants' => ['enabled' => 100]];
108117
```
118+
109119
### A totally disabled feature:
120+
110121
```php
111122
$server_config['foo'] = ['variants' => ['enabled' => 0]];
112123
```
124+
113125
### Feature with winning variant turned on for everyone
126+
114127
```php
115128
$server_config['foo'] = ['variants' => ['blue_background' => 100]];
116129
```
130+
117131
### Single-variant feature ramped up to 1% of users.
132+
118133
```php
119134
$server_config['foo'] = ['variants' => ['enabled' => 1]];
120135
```
136+
121137
### Multi-variant feature ramped up to 1% of users for each variant.
138+
122139
```php
123140
$server_config['foo'] = [
124141
'variants' => [
@@ -128,31 +145,41 @@ cases along with the most concise way to write the configuration.
128145
],
129146
];
130147
```
148+
131149
### Enabled for 10% of regular users.
150+
132151
```php
133152
$server_config['foo'] = [
134153
'variants' => ['enabled' => 10]
135154
];
136155
```
156+
137157
### Feature ramped up to 1% of requests, bucketing at random rather than by id
158+
138159
```php
139160
$server_config['foo'] = [
140161
'variants' => ['enabled' => 1],
141162
'bucketing' => 'random'
142163
];
143164
```
165+
144166
### Feature ramped up to 40% of requests, bucketing by id rather than at random
167+
145168
```php
146169
$server_config['foo'] = [
147170
'variants' => ['enabled' => 40],
148171
'bucketing' => 'id'
149172
];
150173
```
174+
151175
### Single-variant feature in 50/50 A/B test
176+
152177
```php
153178
$server_config['foo'] = ['variants' => ['enabled' => 50]];
154179
```
180+
155181
### Multi-variant feature in A/B test with 20% of users seeing each variant (and 40% left in control group).
182+
156183
```php
157184
$server_config['foo'] = [
158185
'variants' => [
@@ -162,6 +189,7 @@ cases along with the most concise way to write the configuration.
162189
],
163190
];
164191
```
192+
165193
## Configuration details
166194

167195
Each feature’s config stanza controls when the feature is enabled and what
@@ -173,7 +201,7 @@ keys, the most important of which is `'variants'`.
173201
The value of the `'variants'` property an array whose keys are names of variants
174202
and whose values are the percentage of requests that should see each variant.
175203

176-
The remaining feature config property is `'bucketing'`. Bucketing specifies
204+
The remaining feature config property is `'bucketing'`. Bucketing specifies
177205
how users are bucketed when a feature is enabled for only a percentage of users.
178206
The default value, `'random'`, causes each request to be bucketed independently,
179207
meaning that the same user will be in different buckets on different requests.
@@ -189,12 +217,12 @@ There are a few ways to misuse the Feature API or misconfigure a feature that
189217
may be detected. (Some of these are not currently detected but may be in the
190218
future.)
191219

192-
1. Setting the percentage value of a variant in `'variants'` to a value less
193-
than 0 or greater than 100.
220+
1. Setting the percentage value of a variant in `'variants'` to a value less
221+
than 0 or greater than 100.
194222

195-
2. Setting `'variants'` such that the sum of the variant percentages is
196-
greater than 100.
223+
2. Setting `'variants'` such that the sum of the variant percentages is
224+
greater than 100.
197225

198-
3. Setting `'variants'` to a non-array value.
226+
3. Setting `'variants'` to a non-array value.
199227

200-
4. Setting `'bucketing'` to any value that is not `'id'` or `'random'`.
228+
4. Setting `'bucketing'` to any value that is not `'id'` or `'random'`.

Diff for: composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
"toggle"
2323
],
2424
"require": {
25-
"php": ">=8.2"
25+
"php": ">=8.3"
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "*"
2929
},
3030
"scripts": {
31-
"test": "php ./vendor/bin/phpunit --stop-on-failure --fail-on-warning --fail-on-risky -v tests/"
31+
"test": "php ./vendor/bin/phpunit --stop-on-failure --fail-on-warning --fail-on-risky tests/"
3232
},
3333
"autoload": {
3434
"psr-4": {

Diff for: src/Bucketing/Id.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
* hexdec('ffffffff') is the largest possible outcome
1111
* of hash('crc32c', $idToHash)
1212
*/
13-
private const TOTAL = 4294967295;
14-
private const HASH_ALGO = 'crc32c';
13+
private const int TOTAL = 4294967295;
14+
private const string HASH_ALGO = 'crc32c';
1515

1616
/**
1717
* Convert Id string to a Hex
1818
* Convert Hex to Dec int
19-
* Get a percentage float
19+
* Get a percentage int
2020
*/
21-
public function strToIntHash(string $idToHash = ''): float
21+
public function strToIntHash(string $idToHash): int
2222
{
2323
$hex = hash(self::HASH_ALGO, $idToHash);
2424
$dec = hexdec($hex);
2525

2626
$x = $dec / self::TOTAL;
27-
return $x * 100;
27+
return (int) round($x * 100);
2828
}
2929
}

Diff for: src/Bucketing/Random.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ public function __construct()
1616
$this->randomizer = new Randomizer(new Xoshiro256StarStar());
1717
}
1818

19-
public function strToIntHash(string $idToHash = ''): float
19+
public function strToIntHash(string $idToHash): int
2020
{
21-
$decimal = $this->randomizer->getInt(0, PHP_INT_MAX) / PHP_INT_MAX;
22-
return $decimal * 100;
21+
return $this->randomizer->getInt(0, 100);
2322
}
2423
}

Diff for: src/Bucketing/Type.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
interface Type
88
{
99
/**
10-
* A hash number between 0 and 100 based on an id string
10+
* A hash that maps the given string to a number between 0 and 100
1111
* unless we are bucketing completely at random
1212
*/
13-
public function strToIntHash(string $idToHash = ''): float;
13+
public function strToIntHash(string $idToHash): int;
1414
}

0 commit comments

Comments
 (0)