Weak comparison detection

Open
#8 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
tooling

Research direction

Start with the related PHPStan issue #791 and the current weak-comparison rule coverage. Reproduce both PHP examples, then trace the rule that evaluates nullable values in boolean conditions. Done means reporting the nullable integer case while leaving the nullable DateTime case without an error.

Written by the indexing model from the issue text.

Description

Related: https://github.com/phpstan/phpstan/issues/791

A weak comparison error should be thrown for such case:

<?php declare(strict_type=1);

function getCount(): ?int
{
	return 0 === rand(1, 5) % 5 ? 42 : null;
}

if (!getCount()) {
	echo 'test';
}

Because 0 and null will go to the same result.

But this case must not rise an error:

 <?php declare(strict_type=1);

function getUpdatedAt(): ?\DateTime
{
	return 0 === rand(1, 5) % 5 ? new \DateTime('now') : null;
}

if (!getUpdatedAt()) {
	echo 'test';
}

Because it's currently fine, no confusable value can be return here.

Dominant language
PHP
Stars
709
Forks
62
Avg merge
5d 19h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-strict-rules

All issues in phpstan/phpstan-strict-rules

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.