Skip to content

Commit c45e727

Browse files
authored
PHPLIB-1372 Fix RiskyTruthyFalsyComparison (#1218)
The new RiskyTruthyFalsyComparison errors are introduced by psalm 5.20.0. Disable on example files. Using getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/' is safe as any falsy value could be replaced by the default URI. Replace !empty($filter) by $filter !== [] as $filter is an array or an object, and even empty(new stdClass) is true. This might be a necessary bug fix.
1 parent 647ca72 commit c45e727

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

psalm.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@
3434

3535
<!-- This is often the result of type checks due to missing native types -->
3636
<DocblockTypeContradiction errorLevel="info" />
37+
38+
<!-- If the result of getenv is falsy, using the default URI is fine -->
39+
<RiskyTruthyFalsyComparison>
40+
<errorLevel type="suppress">
41+
<directory name="examples" />
42+
<directory name="docs/examples" />
43+
</errorLevel>
44+
</RiskyTruthyFalsyComparison>
3745
</issueHandlers>
3846
</psalm>

src/ChangeStream.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private function onIteration(bool $incrementKey): void
262262
*/
263263
private function resume(): void
264264
{
265-
if (! $this->resumeCallable) {
265+
if ($this->resumeCallable === null) {
266266
throw new BadMethodCallException('Cannot resume a closed change stream.');
267267
}
268268

src/Operation/Count.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private function createCommandDocument(): array
187187
{
188188
$cmd = ['count' => $this->collectionName];
189189

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

src/Operation/Distinct.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function createCommandDocument(): array
182182
'key' => $this->fieldName,
183183
];
184184

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

0 commit comments

Comments
 (0)