10000 [Finder] Make Comparator immutable by derrabus · Pull Request #42137 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Finder] Make Comparator immutable #42137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2021

Conversation

derrabus
Copy link
Member
Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? yes
Tickets N/A
License MIT
Doc PR N/A

The Comparator class exposes setters for its properties $target and $operator. I'd consider this design to be flawed for the following reasons:

  • The $target property should be set before executing the main method test() on an instance of that class. However, we never make sure that this actually happens.
  • The two child classes DateComparator and NumberComparator set both properties via their constructors, after parsing them from a string. It's a bit odd to allow those properties to be overridden via the setters because all validation happens inside those constructors.

This PR proposes to remove those setters and introduce a constructor instead that allows to set both properties once.

@@ -73,7 +73,6 @@ public function __construct(?string $test)
}
}

$this->setTarget($target);
$this->setOperator($matches[1] ?? '==');
parent::__construct($target, $matches[1] ?: '==');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you change ?? to ?: on purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. setOperator() accepted an empty string and I've made the constructor stricter in that regard.

@derrabus derrabus force-pushed the feature/immutable-comparator branch from c937eec to 4f0089b Compare July 15, 2021 21:21
@@ -26,14 +32,22 @@ public function testGetSetOperator()
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->setOperator() throws an \InvalidArgumentException if the operator is not valid.');
}

$comparator = new Comparator();
$comparator = new Comparator('some target');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 tests in one here. Those should be split (which will also allow using expectException to test the exception) instead of this old way of writing tests that does not follow the best practices.

And the test about an invalid operator should also be added for the constructor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -43,9 +57,10 @@ public function testGetSetTarget()
*/
public function testTest($operator, $target, $match, $noMatch)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data provider for this test does not provide full coverage (it tests only one operator). Would it make sense to add other cases ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests for this class are indeed pretty sparse. I think the class is mainly tested by testing its subclasses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derrabus derrabus force-pushed the feature/immutable-comparator branch from 4f0089b to 0ff3971 Compare July 16, 2021 21:21
Tobion added a commit that referenced this pull request Jul 17, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[Finder] Improve ComparatorTest

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | #42137 (comment), #42137 (comment)
| License       | MIT
| Doc PR        | not necessary

Commits
-------

4c44dce [Finder] Improve ComparatorTest
Signed-off-by: Alexander M. Turek <me@derrabus.de>
@fabpot
Copy link
Member
fabpot commented Jul 18, 2021

Thank you @derrabus.

@fabpot fabpot force-pushed the feature/immutable-comparator branch from 0ff3971 to b83ead2 Compare July 18, 2021 08:05
@fabpot fabpot merged commit 15d76e1 into symfony:5.4 Jul 18, 2021
This was referenced Nov 5, 2021
@derrabus derrabus deleted the feature/immutable-comparator branch November 21, 2021 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0