Skip to content

Commit 041a4c1

Browse files
committed
Deprecate return other type than boolean from TicketFeatures::hasFeature()
1 parent a3d2099 commit 041a4c1

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

Component/TicketFeatures.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
*/
1919
class TicketFeatures
2020
{
21-
private $features;
21+
/**
22+
* @var array<string, bool>
23+
*/
24+
private $features = [];
2225

2326
/**
24-
* @param string $messageClass TicketMessage class
27+
* @param array<string, bool> $features
28+
* @param string $messageClass TicketMessage class
2529
*/
2630
public function __construct(array $features, $messageClass)
2731
{
@@ -34,18 +38,30 @@ public function __construct(array $features, $messageClass)
3438
}
3539

3640
/**
41+
* NEXT_MAJOR: Remove the BC checks and return only boolean values.
42+
*
3743
* Check if feature exists or whether enabled.
3844
*
39-
* @param $feature
45+
* @param string $feature
4046
*
4147
* @return bool|null
4248
*/
4349
public function hasFeature($feature)
4450
{
51+
$args = \func_get_args();
52+
if (isset($args[1]) && 'return_strict_bool' === $args[1]) {
53+
return isset($this->features[$feature]) && $this->features[$feature];
54+
}
55+
4556
if (!isset($this->features[$feature])) {
57+
@trigger_error(sprintf(
58+
'Returning other type than boolean from %s() is deprecated since hackzilla/ticket-bundle 3.x'
59+
.' and will be not allowed in version 4.0.'
60+
), E_USER_DEPRECATED);
61+
4662
return null;
4763
}
4864

49-
return $this->features[$feature];
65+
return (bool) $this->features[$feature];
5066
}
5167
}

Form/Type/TicketMessageType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5959
]
6060
);
6161

62-
if ($this->features->hasFeature('attachment')) {
62+
// NEXT_MAJOR: Remove the argument 2 for `TicketFeatures::hasFeature()`
63+
if ($this->features->hasFeature('attachment', 'return_strict_bool')) {
6364
$builder
6465
->add(
6566
'attachmentFile',

Tests/Component/TicketFeaturesTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ final class TicketFeaturesTest extends WebTestCase
2121
/**
2222
* @dataProvider constructProvider
2323
*
24-
* @param array $features
2524
* @param string $class
2625
*/
27-
public function testConstruct($features, $class)
26+
public function testConstruct(array $features, $class)
2827
{
29-
$obj = new TicketFeatures($features, $class);
30-
31-
$this->assertInstanceOf(TicketFeatures::class, $obj);
28+
$this->assertInstanceOf(TicketFeatures::class, new TicketFeatures($features, $class));
3229
}
3330

3431
public function constructProvider()
3532
{
3633
return [
37-
[[], '\stdClass'],
34+
[[], \stdClass::class],
3835
];
3936
}
4037

@@ -49,13 +46,14 @@ public function testFeatureAttachment(array $features, $class, $compare)
4946
$obj = new TicketFeatures($features, $class);
5047

5148
$this->assertInstanceOf(TicketFeatures::class, $obj);
52-
$this->assertSame($obj->hasFeature('attachment'), $compare);
49+
// NEXT_MAJOR: Remove the argument 2 for `TicketFeatures::hasFeature()`
50+
$this->assertSame($obj->hasFeature('attachment', 'return_strict_bool'), $compare);
5351
}
5452

5553
public function featureAttachmentProvider()
5654
{
5755
return [
58-
[[], TicketMessage::class, null],
56+
[[], TicketMessage::class, false],
5957
[['attachment' => true], TicketMessage::class, false],
6058
[['attachment' => true], TicketMessageWithAttachment::class, true],
6159
];

UPGRADE-3.x.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
UPGRADE FROM 3.5 to 3.6
2+
=======================
3+
4+
## `Hackzilla\Bundle\TicketBundle\Component\TicketFeatures`
5+
6+
Returning other type than boolean from `TicketFeatures::hasFeature()` is deprecated
7+
and will be not allowed in version 4.0.
8+
19
UPGRADE FROM 3.4 to 3.5
210
=======================
311

0 commit comments

Comments
 (0)