Skip to content

Commit f8d3ffa

Browse files
committed
deprecate $first in HeaderBag::get and fix tests and dependencies
1 parent 6650234 commit f8d3ffa

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,26 @@ public function add(array $headers)
109109
*/
110110
public function get($key, $default = null, $first = true)
111111
{
112-
@trigger_error(sprintf('%s::%s is deprecated since Symfony 4.4, use %s::getFirst() or %s::getAll() instead.', __CLASS__, __METHOD__, __CLASS__, __CLASS__), E_USER_DEPRECATED);
113-
if (true === $first) {
114-
return $this->getFirst($key, $default);
115-
}
112+
$all = $this->getValues($key, $default);
116113

117-
return $this->getAll($key, $default);
118-
}
114+
if (true === $first) {
119115

120-
public function getFirst($key, $default = null)
121-
{
122-
$all = $this->getAll($key, $default);
116+
if (\count($all)) {
117+
return isset($all[0]) ? $all[0] : $default;
118+
}
123119

124-
if (\count($all)) {
125-
return isset($all[0]) ? $all[0] : null;
120+
return $default;
126121
}
127122

128-
return $default;
123+
@trigger_error(sprintf('Passing $first as a third argument is deprecated since Symfony 4.4 and will be removed in Symfony 5.0. %s will return the first header value, use %s::gets() instead to get an array of values.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
124+
125+
return $all;
129126
}
130127

131-
public function getAll($key, $default = null): array
128+
/**
129+
* @return string[]
130+
*/
131+
public function getValues($key, ?string $default = null): array
132132
{
133133
$key = str_replace('_', '-', strtolower($key));
134134
$headers = $this->all();
@@ -198,7 +198,7 @@ public function has($key)
198198
*/
199199
public function contains($key, $value)
200200
{
201-
return \in_array($value, $this->get($key, null, false));
201+
return \in_array($value, $this->getValues($key, null));
202202
}
203203

204204
/**

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ public function hasVary(): bool
10241024
*/
10251025
public function getVary(): array
10261026
{
1027-
if (!$vary = $this->headers->get('Vary', null, false)) {
1027+
if (!$vary = $this->headers->getValues('Vary', null)) {
10281028
return [];
10291029
}
10301030

src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,37 @@ public function testGet()
8888
$bag = new HeaderBag(['foo' => 'bar', 'fuzz' => 'bizz']);
8989
$this->assertEquals('bar', $bag->get('foo'), '->get return current value');
9090
$this->assertEquals('bar', $bag->get('FoO'), '->get key in case insensitive');
91-
$this->assertEquals(['bar'], $bag->get('foo', 'nope', false), '->get return the value as array');
91+
$this->assertEquals(['bar'], $bag->getValues('foo', 'nope'), '->get return the value as array');
9292

9393
// defaults
9494
$this->assertNull($bag->get('none'), '->get unknown values returns null');
9595
$this->assertEquals('default', $bag->get('none', 'default'), '->get unknown values returns default');
96-
$this->assertEquals($bag->getFirst('none', 'default'), $bag->get('none', 'default'), '->get unknown values returns default as array');
9796

98-
$this->assertEquals(['default'], $bag->get('none', 'default', false), '->get unknown values returns default as array');
99-
$this->assertEquals($bag->getAll('none', 'default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
97+
$this->assertEquals(['default'], $bag->getValues('none', 'default'), '->get unknown values returns default as array');
10098

10199
$bag->set('foo', 'bor', false);
102100
$this->assertEquals('bar', $bag->get('foo'), '->get return first value');
103-
$this->assertEquals($bag->getAll('foo', 'nope'), $bag->get('foo', 'nope', false), '->get return all values as array');
104101

105-
$this->assertEquals(['bar', 'bor'], $bag->get('foo', 'nope', false), '->get return all values as array');
102+
$this->assertEquals(['bar', 'bor'], $bag->getValues('foo', 'nope'), '->get return all values as array');
103+
}
104+
105+
/**
106+
* @group legacy
107+
* @expectedDeprecation Passing $first as a third argument is deprecated since Symfony 4.4 and will be removed in Symfony 5.0. Symfony\Component\HttpFoundation\HeaderBag::get will return the first header value, use Symfony\Component\HttpFoundation\HeaderBag::gets() instead to get an array of values.
108+
*/
109+
public function testGetIsEqualToNewMethod()
110+
{
111+
$bag = new HeaderBag(['foo' => 'bar', 'fuzz' => 'bizz']);
112+
$this->assertEquals($bag->get('none', 'default'), $bag->get('none', 'default'), '->get unknown values returns default as array');
113+
$this->assertEquals($bag->getValues('none', 'default'), $bag->get('none', 'default', false), '->get unknown values returns default as array');
106114
}
107115

108116
public function testSetAssociativeArray()
109117
{
110118
$bag = new HeaderBag();
111119
$bag->set('foo', ['bad-assoc-index' => 'value']);
112120
$this->assertSame('value', $bag->get('foo'));
113-
$this->assertEquals(['value'], $bag->get('foo', 'nope', false), 'assoc indices of multi-valued headers are ignored');
121+
$this->assertEquals(['value'], $bag->getValues('foo', 'nope'), 'assoc indices of multi-valued headers are ignored');
114122
}
115123

116124
public function testContains()

src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ public function testCacheControlHeader()
8989

9090
$bag = new ResponseHeaderBag();
9191
$bag->set('Cache-Control', ['public', 'must-revalidate']);
92-
$this->assertCount(1, $bag->get('Cache-Control', null, false));
92+
$this->assertCount(1, $bag->getValues('Cache-Control', null));
9393
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
9494

9595
$bag = new ResponseHeaderBag();
9696
$bag->set('Cache-Control', 'public');
9797
$bag->set('Cache-Control', 'must-revalidate', false);
98-
$this->assertCount(1, $bag->get('Cache-Control', null, false));
98+
$this->assertCount(1, $bag->getValues('Cache-Control', null));
9999
$this->assertEquals('must-revalidate, public', $bag->get('Cache-Control'));
100100
}
101101

@@ -166,7 +166,7 @@ public function testCookiesWithSameNames()
166166
'foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax',
167167
'foo=bar; path=/path/bar; domain=bar.foo; httponly; samesite=lax',
168168
'foo=bar; path=/; httponly; samesite=lax',
169-
], $bag->get('set-cookie', null, false));
169+
], $bag->getValues('set-cookie', null));
170170

171171
$this->assertSetCookieHeader('foo=bar; path=/path/foo; domain=foo.bar; httponly; samesite=lax', $bag);
172172
$this->assertSetCookieHeader('foo=bar; path=/path/bar; domain=foo.bar; httponly; samesite=lax', $bag);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy