phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

Invalid null coalescing operator suggestion for a class consent on a nullable object variable

Open stephenreay opened this issue 1 year ago • 1 comments

Subject Details
Plugin Php Inspections (EA Extended) v5.0.0.0
Language level 7.3

Current behaviour

With a parameter typed as ?SomeClass, class-constant lookups wrapped in an if are suggested to be replaced by null coalescing operator. e.g.:

	function foo(?Something $var = null): void {
		$fragment = null;
		if ($var) {
			$fragment = $var::PREFIX;
		}
		...
	}

The suggested replacement is $var::PREFIX ?? null, but this produces a php error

Expected behaviour

Don't suggest this replacement when the left side of the coalescing operator would be a class constant.

Environment details

IntelliJ IDEA 2023.3 Beta (Ultimate Edition) Build #IU-233.11799.6, built on November 9, 2023 Licensed to IntelliJ IDEA EAP user: Stephen Reay Expiration date: December 9, 2023 Runtime version: 17.0.9+7-b1087.3 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 14.1.1 GC: G1 Young Generation, G1 Old Generation Memory: 4096M Cores: 24 Metal Rendering is ON Registry: debugger.new.tool.window.layout=true ide.experimental.ui=true Non-Bundled Plugins: com.jetbrains.plugins.ini4idea (233.11799.7) rocks.blackcat.vcl (0.7.2) net.sjrx.intellij.plugins.systemdunitfiles (223.230322.126) com.sburlyaev.terminal.plugin (0.6.0) com.intellij.plugin.applescript (0.121) org.jetbrains.plugins.ruby (233.11799.6) com.jetbrains.TabFormat (0.6) org.intellij.plugins.hcl (233.11799.7) com.intellij.apacheConfig (233.11799.7) name.kropp.intellij.makefile (233.11799.7) com.jetbrains.space (233.11799.10) org.jetbrains.plugins.vagrant (233.11799.6) com.jetbrains.php (233.11799.6) org.jetbrains.plugins.phpstorm-remote-interpreter (233.11799.6) com.kalessil.phpStorm.phpInspectionsEA (5.0.0.0) de.espend.idea.php.annotation (9.4.0) com.intellij.php.psalm (233.11799.6) com.intellij.php.tools.quality.phpstan (233.11799.6) com.dwilden.crondescriptor (1.5) pro.bashsupport (3.3.4.233) org.asciidoctor.intellij.asciidoc (0.41.0) mobi.hsz.idea.gitignore (4.5.2) Pythonid (233.11799.6) ru.adelf.idea.dotenv (2023.3) Kotlin: 233-1.9.0-release-358-IJ11799.6

stephenreay avatar Nov 13 '23 06:11 stephenreay

Another case of what seems to be the same issue:

function test(?object $obj = null): string
{
    return $obj ? $obj::class : '-';
}

Reproducible in EA Ultimate 2023.1

FlyingDR avatar Nov 20 '23 11:11 FlyingDR