Skip to content

Commit 1dc2aa4

Browse files
Fix type checking bug in Decorator::typeAllowsScope() causes primitive type hints to fail scope validation (#153)
* Fix type checking bug in `Decorator::typeAllowsScope()` causes primitive type hints to fail scope validation * Fix code styling * Add test * Fix code styling * fix `float/double` Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: crynobone <172966+crynobone@users.noreply.github.com> Co-authored-by: Tim MacDonald <hello@timacdonald.me> Co-authored-by: timacdonald <24803032+timacdonald@users.noreply.github.com>
1 parent 443935c commit 1dc2aa4

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

src/Drivers/Decorator.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,22 @@ protected function typeAllowsScope($type, $scope, $function)
248248
}
249249

250250
if ($type instanceof ReflectionNamedType) {
251-
if ($type->getName() === 'mixed') {
251+
$typeName = $type->getName();
252+
253+
if ($typeName === 'mixed') {
252254
return true;
253255
}
254256

255257
return match (gettype($scope)) {
256-
'boolean',
257-
'integer',
258-
'double',
258+
'boolean' => in_array($typeName, ['boolean', 'bool']),
259+
'integer' => in_array($typeName, ['integer', 'int']),
260+
'double' => in_array($typeName, ['double', 'float']),
259261
'string',
260262
'array',
261263
'resource',
262-
'resource (closed)' => gettype($scope) === $type->getName(),
264+
'resource (closed)' => gettype($scope) === $typeName,
263265
'NULL' => $this->canHandleNullScope($function),
264-
'object' => $scope instanceof ($type->getName()),
266+
'object' => $scope instanceof ($typeName),
265267
'unknown type' => false,
266268
};
267269
}

tests/Feature/ArrayDriverTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,61 @@ public function test_can_retrieve_scalar_values_without_in_memory_cache(): void
12131213
$this->assertEquals($expectedValue, $retrieved);
12141214
}
12151215
}
1216+
1217+
public function test_it_handles_integer_scopes_correctly()
1218+
{
1219+
Feature::define(IntScopeFeature::class);
1220+
1221+
$this->assertSame([
1222+
IntScopeFeature::class => true,
1223+
], Feature::for(1)->all());
1224+
1225+
$this->assertSame([
1226+
IntScopeFeature::class => false,
1227+
], Feature::for(10)->all());
1228+
}
1229+
1230+
public function test_it_can_handles_double_scopes_correctly()
1231+
{
1232+
Feature::define(DoubleScopeFeature::class);
1233+
1234+
$this->assertSame([
1235+
DoubleScopeFeature::class => true,
1236+
], Feature::for(1.1)->all());
1237+
1238+
$this->assertSame([
1239+
DoubleScopeFeature::class => true,
1240+
], Feature::for(1.10)->all());
1241+
1242+
$this->assertSame([
1243+
DoubleScopeFeature::class => false,
1244+
], Feature::for(1.11)->all());
1245+
1246+
$this->assertSame([
1247+
DoubleScopeFeature::class => false,
1248+
], Feature::for(10.00)->all());
1249+
}
1250+
1251+
public function test_it_can_handles_float_scopes_correctly()
1252+
{
1253+
Feature::define(FloatScopeFeature::class);
1254+
1255+
$this->assertSame([
1256+
FloatScopeFeature::class => true,
1257+
], Feature::for(1.1)->all());
1258+
1259+
$this->assertSame([
1260+
FloatScopeFeature::class => true,
1261+
], Feature::for(1.10)->all());
1262+
1263+
$this->assertSame([
1264+
FloatScopeFeature::class => false,
1265+
], Feature::for(1.11)->all());
1266+
1267+
$this->assertSame([
1268+
FloatScopeFeature::class => false,
1269+
], Feature::for(10.00)->all());
1270+
}
12161271
}
12171272

12181273
class MyFeature
@@ -1298,3 +1353,27 @@ class FeatureDependency
12981353
{
12991354
//
13001355
}
1356+
1357+
class IntScopeFeature
1358+
{
1359+
public function resolve(int $scope): bool
1360+
{
1361+
return in_array($scope, [1, 2, 3], true);
1362+
}
1363+
}
1364+
1365+
class DoubleScopeFeature
1366+
{
1367+
public function resolve(float $scope): bool
1368+
{
1369+
return in_array($scope, [1.10, 2.20, 3.30], true);
1370+
}
1371+
}
1372+
1373+
class FloatScopeFeature
1374+
{
1375+
public function resolve(float $scope): bool
1376+
{
1377+
return in_array($scope, [1.1, 2.2, 3.3], true);
1378+
}
1379+
}

0 commit comments

Comments
 (0)