Skip to content
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

PHPLIB-1372 Fix RiskyTruthyFalsyComparison #1218

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@

<!-- This is often the result of type checks due to missing native types -->
<DocblockTypeContradiction errorLevel="info" />

<!-- If the result of getenv is falsy, using the default URI is fine -->
<RiskyTruthyFalsyComparison>
<errorLevel type="suppress">
<directory name="examples" />
<directory name="docs/examples" />
</errorLevel>
</RiskyTruthyFalsyComparison>
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/ChangeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private function onIteration(bool $incrementKey): void
*/
private function resume(): void
{
if (! $this->resumeCallable) {
if (null === $this->resumeCallable) {
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
throw new BadMethodCallException('Cannot resume a closed change stream.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private function createCommandDocument(): array
{
$cmd = ['count' => $this->collectionName];

if (! empty($this->filter)) {
if ($this->filter !== []) {
Copy link
Member

@jmikola jmikola Jan 22, 2024

Choose a reason for hiding this comment

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

$filter is an array or an object, and even empty(new stdClass) is true. This might be a necessary bug fix.

The count and distinct commands both use an empty filter by default, so including new stdClass in the outgoing command was inconsistent (and redundant) but had no functional impact on the result.

I don't think we need to go the extra mile to detect empty objects, so if this is the most concise change to silence Psalm that works for me.

$cmd['query'] = (object) $this->filter;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private function createCommandDocument(): array
'key' => $this->fieldName,
];

if (! empty($this->filter)) {
if ($this->filter !== []) {
$cmd['query'] = (object) $this->filter;
}

Expand Down