8000 [Finder] Make Comparator immutable · symfony/symfony@c937eec · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c937eec

Browse files
committed
[Finder] Make Comparator immutable
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent bb26a36 commit c937eec

File tree

8 files changed

+80
-19
lines changed

8 files changed

+80
-19
lines changed

UPGRADE-5.4.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Cache
66

77
* Deprecate `DoctrineProvider` because this class has been added to the `doctrine/cache` package`
88

9+
Finder
10+
------
11+
12+
* Deprecate `Comparator::setTarget()` and `Comparator::setOperator()`
13+
* Add a constructor to `Comparator` that allows setting target and operator
14+
915
FrameworkBundle
1016
---------------
1117

UPGRADE-6.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ EventDispatcher
6060

6161
* Removed `LegacyEventDispatcherProxy`. Use the event dispatcher without the proxy.
6262

63+
Finder
64+
------
65+
66+
* Remove `Comparator::setTarget()` and `Comparator::setOperator()`
67+
* The `$target` parameter of `Comparator::__construct()` is now mandatory
68+
6369
Form
6470
----
6571

src/Symfony/Component/Finder/CHANGELOG.md

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

4+
5.4.0
5+
-----
6+
7+
* Deprecate `Comparator::setTarget()` and `Comparator::setOperator()`
8+
* Add a constructor to `Comparator` that allows setting target and operator
9+
410
5.0.0
511
-----
612

src/Symfony/Component/Finder/Comparator/Comparator.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,44 @@
1212
namespace Symfony\Component\Finder\Comparator;
1313

1414
/**
15-
* Comparator.
16-
*
1715
* @author Fabien Potencier <fabien@symfony.com>
1816
*/
1917
class Comparator
2018
{
2119
private $target;
2220
private $operator = '==';
2321

22+
public function __construct(string $target = null, string $operator = '==')
23+
{
24+
if (null === $target) {
25+
trigger_deprecation('symfony/finder', '5.4', 'Constructing a "%s" without setting "$target" is deprecated.', __CLASS__);
26+
}
27+
28+
$this->target = $target;
29+
$this->doSetOperator($operator);
30+
}
31+
2432
/**
2533
* Gets the target value.
2634
*
2735
* @return string The target value
2836
*/
2937
public function getTarget()
3038
{
39+
if (null === $this->target) {
40+
trigger_deprecation('symfony/finder', '5.4', 'Calling "%s" without initializing the target is deprecated.', __METHOD__);
41+
}
42+
3143
return $this->target;
3244
}
3345

46+
/**
47+
* @deprecated set the target via the constructor instead
48+
*/
3449
public function setTarget(string $target)
3550
{
51+
trigger_deprecation('symfony/finder', '5.4', '"%s" is deprecated. Set t B5FA he target via the constructor instead.', __METHOD__);
52+
3653
$this->target = $target;
3754
}
3855

@@ -50,18 +67,14 @@ public function getOperator()
5067
* Sets the comparison operator.
5168
*
5269
* @throws \InvalidArgumentException
70+
*
71+
* @deprecated set the operator via the constructor instead
5372
*/
5473
public function setOperator(string $operator)
5574
{
56-
if ('' === $operator) {
57-
$operator = '==';
58-
}
75+
trigger_deprecation('symfony/finder', '5.4', '"%s" is deprecated. Set the operator via the constructor instead.', __METHOD__);
5976

60-
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
61-
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
62-
}
63-
64-
$this->operator = $operator;
77+
$this->doSetOperator('' === $operator ? '==' : $operator);
6578
}
6679

6780
/**
@@ -73,6 +86,10 @@ public function setOperator(string $operator)
7386
*/
7487
public function test($test)
7588
{
89+
if (null === $this->target) {
90+
trigger_deprecation('symfony/finder', '5.4', 'Calling "%s" without initializing the target is deprecated.', __METHOD__);
91+
}
92+
7693
switch ($this->operator) {
7794
case '>':
7895
return $test > $this->target;
@@ -88,4 +105,16 @@ public function test($test)
88105

89106
return $test == $this->target;
90107
}
108+
109+
/**
110+
* @internal To be merged into the constructor in Symfony 6.0
111+
*/
112+
private function doSetOperator(string $operator): void
113+
{
114+
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
115+
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
116+
}
117+
118+
$this->operator = $operator;
119+
}
91120
}

src/Symfony/Component/Finder/Comparator/DateComparator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function __construct(string $test)
4545
$operator = '<';
4646
}
4747

48-
$this->setOperator($operator);
49-
$this->setTarget($target);
48+
parent::__construct($target, $operator);
5049
}
5150
}

src/Symfony/Component/Finder/Comparator/NumberComparator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function __construct(?string $test)
7373
}
7474
}
7575

76-
$this->setTarget($target);
77-
$this->setOperator($matches[1] ?? '==');
76+
parent::__construct($target, $matches[1] ?: '==');
7877
}
7978
}

src/Symfony/Component/Finder/Tests/Comparator/ComparatorTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212
namespace Symfony\Component\Finder\Tests\Comparator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\Finder\Comparator\Comparator;
1617

1718
class ComparatorTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
22+
/**
23+
* @group legacy
24+
*/
1925
public function testGetSetOperator()
2026
{
2127
$comparator = new Comparator();
@@ -26,14 +32,22 @@ public function testGetSetOperator()
2632
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->setOperator() throws an \InvalidArgumentException if the operator is not valid.');
2733
}
2834

29-
$comparator = new Comparator();
35+
$comparator = new Comparator('some target');
36+
37+
$this->expectDeprecation('Since symfony/finder 5.4: "Symfony\Component\Finder\Comparator\Comparator::setOperator" is deprecated. Set the operator via the constructor instead.');
3038
$comparator->setOperator('>');
3139
$this->assertEquals('>', $comparator->getOperator(), '->getOperator() returns the current operator');
3240
}
3341

42+
/**
43+
* @group legacy
44+
*/
3445
public function testGetSetTarget()
3546
{
47+
$this->expectDeprecation('Since symfony/finder 5.4: Constructing a "Symfony\Component\Finder\Comparator\Comparator" without setting "$target" is deprecated.');
3648
$comparator = new Comparator();
49+
50+
$this->expectDeprecation('Since symfony/finder 5.4: "Symfony\Component\Finder\Comparator\Comparator::setTarget" is deprecated. Set the target via the constructor instead.');
3751
$comparator->setTarget(8);
3852
$this->assertEquals(8, $comparator->getTarget(), '->getTarget() returns the target');
3953
}
@@ -43,9 +57,10 @@ public function testGetSetTarget()
4357
*/
4458
public function testTest($operator, $target, $match, $noMatch)
4559
{
46-
$c = new Comparator();
47-
$c->setOperator($operator);
48-
$c->setTarget($target);
60+
$c = new Comparator($target, $operator);
61+
62+
$this->assertSame($target, $c->getTarget());
63+
$this->assertSame($operator, $c->getOperator());
4964

5065
foreach ($match as $m) {
5166
$this->assertTrue($c->test($m), '->test() tests a string against the expression');

src/Symfony/Component/Finder/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.2.5"
19+
"php": ">=7.2.5",
20+
"symfony/deprecation-contracts": "^2.1|^3"
2021
},
2122
"autoload": {
2223
"psr-4": { "Symfony\\Component\\Finder\\": "" },

0 commit comments

Comments
 (0)
0