vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Calls to defined(), function_exists, method_exists should disable undefined symbol checks

Open bmewburn opened this issue 5 years ago • 23 comments

https://www.php.net/manual/en/function.defined.php

The same could be said for class_exists, function_exists, method_exists etc. https://www.php.net/manual/en/function.class-exists.php

This would be a way of controlling false diagnostics using PHP code rather than relying on non-standard @suppress or @noinspect phpdoc annotations.

assert(class_exists('Foo'));

bmewburn avatar Jan 11 '20 02:01 bmewburn

class_exists will suppress also method calls to this class? Idea is interesting, but will result for sure with angry bug reports :) I mean, following example is ridicules and probably @suppress/@noinspect would be way better.

function a():void{
  assert(class_exists('Foo'));
  $f = new Foo;
  assert(method_exists('Foo', 'test'));
  $f->test();
}

Just as example psalm assumes mixed in such cases https://psalm.dev/r/a5a6a62123 (and can't deal with this example)

KapitanOczywisty avatar Jan 11 '20 03:01 KapitanOczywisty

class_exists, rand Send error "undefined function" because this occurs?

LeninZapata avatar Feb 07 '20 18:02 LeninZapata

@LeninZapata no, if you are seeing errors on these then it is unrelated. Please open a new ticket and include the settings you are using.

bmewburn avatar Feb 07 '20 21:02 bmewburn

This error flag is clearly wrong:

stracker-phil avatar Apr 08 '20 13:04 stracker-phil

any news on this?

rilex037 avatar May 17 '20 03:05 rilex037

Same problem when working with WooCommerce and using the contant WOOCOMMERCE_VERSION.

niikoo avatar Jun 29 '20 14:06 niikoo

any progress??

andrevandal avatar Dec 10 '20 13:12 andrevandal

@bmewburn any updates?

dinhquochan avatar Dec 15 '20 06:12 dinhquochan

This setting will do the job (dirty way): "intelephense.diagnostics.undefinedConstants": false

rodrigobutta avatar Feb 04 '21 16:02 rodrigobutta

This error flag is clearly wrong:

You can use the constant function

constant("DISABLE_NAG_NOTICES")

image

CyrusJoudieh avatar Dec 30 '21 13:12 CyrusJoudieh

any progress? (for following)

fabienwnklr avatar Jan 17 '22 12:01 fabienwnklr

@fabienwnklr I suggest that you use the PHP built in function "constant" to check for existence of constants.

CyrusJoudieh avatar Jan 17 '22 13:01 CyrusJoudieh

@CyrusJoudieh Yeah i read this, but i use many constant in my project, i think it's very time-consuming..

fabienwnklr avatar Jan 17 '22 13:01 fabienwnklr

@fabienwnklr I suggest that you use the PHP built in function "constant" to check for existence of constants.

That doesn't resolve the case for function_exists() and the others.

Unifex avatar Jul 19 '22 03:07 Unifex

Send error "undefined function" because this occurs?

facing same issue on recent updates

Screenshot 2023-01-04 at 17 59 21

Intelephese version v1.9.1

feryardiant avatar Jan 04 '23 10:01 feryardiant

Any updates on this?

feryardiant avatar Feb 01 '23 11:02 feryardiant

Came here also wondering if there is any good solution for this.

Firzenizer avatar Feb 16 '23 07:02 Firzenizer

Send error "undefined function" because this occurs?

facing same issue on recent updates

Screenshot 2023-01-04 at 17 59 21

Intelephese version v1.9.1

@feryardiant I think this is a separate issue where the random stubs aren't in the default set anymore. If you add "random" to the intelephense.stubs setting that should fix your issue.

Virakal avatar Feb 16 '23 07:02 Virakal

What is suggested here?

if (defined('WP_ENV')) {
  if (WP_ENV === 'development') {
    // Undefined constant 'WP_ENV'.intelephense(1011)
  } else {
    // The constant is not defined
  }
} else {

}

Same error when using constant('WP_ENV') as the condition.

james0r avatar Nov 02 '23 04:11 james0r

Same error when using constant('WP_ENV') as the condition.

The "workaround" is to use the constant('WP_ENV') call instead of the direct constant WP_ENV in the equality statement. But then, there is no validation by intelephense whether the constant really exists or not. Might as well use "intelephense.diagnostics.undefinedConstants": false in the settings, which does the same thing overall.

There's no real solution until this bug is dealt with.

goozak avatar Nov 02 '23 13:11 goozak

Please solve this bug, it's still opened after 3 years!

migliori avatar Nov 05 '23 06:11 migliori