8000 [ExpressionLanguage] Remove deprecated code paths · symfony/symfony@7050bc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7050bc8

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[ExpressionLanguage] Remove deprecated code paths
1 parent 9e248b8 commit 7050bc8

File tree

6 files changed

+36
-42
lines changed

6 files changed

+36
-42
lines changed

UPGRADE-7.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ DoctrineBridge
4545
* DoctrineBridge now requires `doctrine/event-manager:^2`
4646
* Add parameter `$isSameDatabase` to `DoctrineTokenProvider::configureSchema()`
4747

48+
ExpressionLanguage
49+
------------------
50+
51+
* The `in` and `not in` operators now use strict comparison
52+
4853
Filesystem
4954
----------
5055

src/Symfony/Component/ExpressionLanguage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.0
5+
---
6+
7+
* The `in` and `not in` operators now use strict comparison
8+
49
6.3
510
---
611

src/Symfony/Component/ExpressionLanguage/Node/BinaryNode.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class BinaryNode extends Node
3030
private const FUNCTIONS = [
3131
'**' => 'pow',
3232
'..' => 'range',
33-
'in' => '\\'.self::class.'::inArray',
34-
'not in' => '!\\'.self::class.'::inArray',
33+
'in' => '\\in_array',
34+
'not in' => '!\\in_array',
3535
'contains' => 'str_contains',
3636
'starts with' => 'str_starts_with',
3737
'ends with' => 'str_ends_with',
@@ -71,9 +71,14 @@ public function compile(Compiler $compiler): void
7171
->compile($this->nodes['left'])
7272
->raw(', ')
7373
->compile($this->nodes['right'])
74-
->raw(')')
7574
;
7675

76+
if ('in' === $operator || 'not in' === $operator) {
77+
$compiler->raw(', true');
78+
}
79+
80+
$compiler->raw(')');
81+
7782
return;
7883
}
7984

@@ -100,12 +105,11 @@ public function evaluate(array $functions, array $values): mixed
100105
if (isset(self::FUNCTIONS[$operator])) {
101106
$right = $this->nodes['right']->evaluate($functions, $values);
102107

103-
if ('not in' === $operator) {
104-
return !self::inArray($left, $right);
105-
}
106-
$f = self::FUNCTIONS[$operator];
107-
108-
return $f($left, $right);
108+
return match ($operator) {
109+
'in' => \in_array($left, $right, true),
110+
'not in' => !\in_array($left, $right, true),
111+
default => self::FUNCTIONS[$operator]($left, $right),
112+
};
109113
}
110114

111115
switch ($operator) {
@@ -143,9 +147,9 @@ public function evaluate(array $functions, array $values): mixed
143147
case '<=':
144148
return $left <= $right;
145149
case 'not in':
146-
return !self::inArray($left, $right);
150+
return !\in_array($left, $right, true);
147151
case 'in':
148-
return self::inArray($left, $right);
152+
return \in_array($left, $right, true);
149153
case '+':
150154
return $left + $right;
151155
case '-':
@@ -176,22 +180,6 @@ public function toArray(): array
176180
return ['(', $this->nodes['left'], ' '.$this->attributes['operator'].' ', $this->nodes['right'], ')'];
177181
}
178182

179-
/**
180-
* @internal to be replaced by an inline strict call to in_array() in version 7.0
181-
*/
182-
public static function inArray($value, array $array): bool
183-
{
184-
if (false === $key = array_search($value, $array)) {
185-
return false;
186-
}
187-
188-
if (!\in_array($value, $array, true)) {
189-
trigger_deprecation('symfony/expression-language', '6.3', 'The "in" operator will use strict comparisons in Symfony 7.0. Loose match found with key "%s" for value %s. Normalize the array parameter so it only has the expected types or implement loose matching in your own expression function.', $key, json_encode($value));
190-
}
191-
192-
return true;
193-
}
194-
195183
private function evaluateMatches(string $regexp, ?string $str): int
196184
{
197185
set_error_handler(function ($t, $m) use ($regexp) {

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function testOperatorCollisions()
269269
$expressionLanguage = new ExpressionLanguage();
270270
$expression = 'foo.not in [bar]';
271271
$compiled = $expressionLanguage->compile($expression, ['foo', 'bar']);
272-
$this->assertSame('\Symfony\Component\ExpressionLanguage\Node\BinaryNode::inArray($foo->not, [0 => $bar])', $compiled);
272+
$this->assertSame('\in_array($foo->not, [0 => $bar], true)', $compiled);
273273

274274
$result = $expressionLanguage->evaluate($expression, ['foo' => (object) ['not' => 'test'], 'bar' => 'test']);
275275
$this->assertTrue($result);

src/Symfony/Component/ExpressionLanguage/Tests/Node/BinaryNodeTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\ExpressionLanguage\Tests\Node;
1313

14-
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1514
use Symfony\Component\ExpressionLanguage\Compiler;
1615
use Symfony\Component\ExpressionLanguage\Node\ArrayNode;
1716
use Symfony\Component\ExpressionLanguage\Node\BinaryNode;
@@ -21,8 +20,6 @@
2120

2221
class BinaryNodeTest extends AbstractNodeTestCase
2322
{
24-
use ExpectDeprecationTrait;
25-
2623
public static function getEvaluateData(): array
2724
{
2825
$array = new ArrayNode();
@@ -116,10 +113,10 @@ public static function getCompileData(): array
116113
['pow(5, 2)', new BinaryNode('**', new ConstantNode(5), new ConstantNode(2))],
117114
['("a" . "b")', new BinaryNode('~', new ConstantNode('a'), new ConstantNode('b'))],
118115

119-
['\Symfony\Component\ExpressionLanguage\Node\BinaryNode::inArray("a", [0 => "a", 1 => "b"])', new BinaryNode('in', new ConstantNode('a'), $array)],
120-
['\Symfony\Component\ExpressionLanguage\Node\BinaryNode::inArray("c", [0 => "a", 1 => "b"])', new BinaryNode('in', new ConstantNode('c'), $array)],
121-
['!\Symfony\Component\ExpressionLanguage\Node\BinaryNode::inArray("c", [0 => "a", 1 => "b"])', new BinaryNode('not in', new ConstantNode('c'), $array)],
122-
['!\Symfony\Component\ExpressionLanguage\Node\BinaryNode::inArray("a", [0 => "a", 1 => "b"])', new BinaryNode('not in', new ConstantNode('a'), $array)],
116+
['\in_array("a", [0 => "a", 1 => "b"], true)', new BinaryNode('in', new ConstantNode('a'), $array)],
117+
['\in_array("c", [0 => "a", 1 => "b"], true)', new BinaryNode('in', new ConstantNode('c'), $array)],
118+
['!\in_array("c", [0 => "a", 1 => "b"], true)', new BinaryNode('not in', new ConstantNode('c'), $array)],
119+
['!\in_array("a", [0 => "a", 1 => "b"], true)', new BinaryNode('not in', new ConstantNode('a'), $array)],
123120

124121
['range(1, 3)', new BinaryNode('..', new ConstantNode(1), new ConstantNode(3))],
125122

@@ -219,17 +216,17 @@ public function testCompileMatchesWithInvalidRegexpAsExpression()
219216
}
220217

221218
/**
222-
* @group legacy
219+
* @testWith [1]
220+
* ["true"]
223221
*/
224-
public function testInOperatorStrictness()
222+
public function testInOperatorStrictness(mixed $value)
225223
{
226224
$array = new ArrayNode();
227-
$array->addElement(new ConstantNode('a'));
225+
$array->addElement(new ConstantNode('1'));
228226
$array->addElement(new ConstantNode(true));
229227

230-
$node = new BinaryNode('in', new ConstantNode('b'), $array);
228+
$node = new BinaryNode('in', new ConstantNode($value), $array);
231229

232-
$this->expectDeprecation('Since symfony/expression-language 6.3: The "in" operator will use strict comparisons in Symfony 7.0. Loose match found with key "1" for value "b". Normalize the array parameter so it only has the expected types or implement loose matching in your own expression function.');
233-
$this->assertTrue($node->evaluate([], []));
230+
$this->assertFalse($node->evaluate([], []));
234231
}
235232
}

src/Symfony/Component/ExpressionLanguage/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
],
1818
"require": {
1919
"php": ">=8.2",
20-
"symfony/deprecation-contracts": "^2.5|^3",
2120
"symfony/cache": "^6.4|^7.0",
2221
"symfony/service-contracts": "^2.5|^3"
2322
},

0 commit comments

Comments
 (0)
0