Skip to content

Commit

Permalink
bug #52040 [Cache] Fix ArrayAdapter::freeze() return type (fancyweb)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

[Cache] Fix ArrayAdapter::freeze() return type

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #51867
| License       | MIT

Enums are not serialized and this is good: they don't need to be frozen since they are immutable.
The fix is to widen the return type.

Commits
-------

d52c315 [Cache] Fix ArrayAdapter::freeze() return type
  • Loading branch information
nicolas-grekas committed Oct 13, 2023
2 parents 21599a4 + d52c315 commit cf53ed1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private function generateItems(array $keys, float $now, \Closure $f): \Generator
}
}

private function freeze($value, string $key): string|int|float|bool|array|null
private function freeze($value, string $key): string|int|float|bool|array|\UnitEnum|null
{
if (null === $value) {
return 'N;';
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Tests\Fixtures\TestEnum;

/**
* @group time-sensitive
Expand Down Expand Up @@ -91,4 +92,14 @@ public function testMaxItems()
$this->assertTrue($cache->hasItem('buz'));
$this->assertTrue($cache->hasItem('foo'));
}

public function testEnum()
{
$cache = new ArrayAdapter();
$item = $cache->getItem('foo');
$item->set(TestEnum::Foo);
$cache->save($item);

$this->assertSame(TestEnum::Foo, $cache->getItem('foo')->get());
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/Cache/Tests/Fixtures/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Fixtures;

enum TestEnum
{
case Foo;
}

0 comments on commit cf53ed1

Please sign in to comment.