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

Support function_exists #3576

Closed
szepeviktor opened this issue Jul 7, 2020 · 14 comments
Closed

Support function_exists #3576

szepeviktor opened this issue Jul 7, 2020 · 14 comments

Comments

@szepeviktor
Copy link
Contributor

szepeviktor commented Jul 7, 2020

Support question

if (php version >= 7.4) call_a_function()

How to tell PHPStan to stop analyzing the code inside that conditional?

e.g. https://github.com/voku/portable-utf8/blob/0381b87156c4925e33d497eed05fca5cf5f627e1/src/voku/helper/UTF8.php#L8350-L8358

@ondrejmirtes
Copy link
Member

Why? What’s the effect of analyzing such function?

@szepeviktor
Copy link
Contributor Author

This.

 ------ -------------------------------------------------------------------- 

  Line   voku/helper/UTF8.php                                                

 ------ -------------------------------------------------------------------- 

  8313   Function mb_str_split not found.                                    

 ------ -------------------------------------------------------------------- 

@ondrejmirtes
Copy link
Member

Can you show the real code that’s causing this?

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jul 7, 2020

From the link above:

            if (Bootup::is_php('7.4')) {
                /**
                 * @psalm-suppress ImpureFunctionCall - why?
                 */
                $return = \mb_str_split($input, $length);
                if ($return !== false) {
                    return $return;
                }
            }

Test fails under PHP 7.3

@szepeviktor
Copy link
Contributor Author

mb_str_split was introduced in PHP 7.4
https://www.php.net/manual/en/function.mb-str-split.php

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jul 7, 2020

I'm not too keen on skipping analysing code like this, but:

  1. You should run PHPStan with the highest supported PHP version. That way you'll also be able to find other problems in your code you might not know about.
  2. I plan to decouple these in the future - so you'd be able to run PHPStan on PHP 7.1 but check the sources like if they're written for PHP 7.4 or 8.0 even.
  3. This should be solvable with function_exists() instead of opaque call like Bootup::is_php('7.4').

I'm gonna keep this open as a feature request for function_exists() support.

@ondrejmirtes ondrejmirtes changed the title Many libraries provide PHP version-depentent function calls Support function_exists Jul 7, 2020
@szepeviktor
Copy link
Contributor Author

Thank you.

@alexander-schranz
Copy link

I have a similar issue which maybe would be easier to support:

if (!\function_exists('tld_extract')) {
     throw new \LogicException(\sprintf(
          'The method %s::%s is deprecated and should not longer be used, if you need it you need to install "layershifter/tld-extract". But we recommend as soon as possible update to a newer library like "jeremykendall/php-domain-parser". ',
          __CLASS__,
          __METHOD__
     ));
}

return tld_extract($host);

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jan 16, 2021

Whether a 3rd-party function exists is not related to the source code, it comes from the actual PHP environment.
So cannot be statically proven.

Skipping 1 line in analysis could cause an error in run-time.
BTW I suggest you to test function existence in your bootstrap process instead.

There is something similar in PHPStan: https://phpstan.org/config-reference#constants
It is also weird. Using a constant as a variable input...

@mvorisek
Copy link
Contributor

Here is a demo: https://phpstan.org/r/f74cc05e-4fc0-4b58-be29-9caa9b7f31ea

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src@28e8c11

@szepeviktor
Copy link
Contributor Author

Thank you.

@alexander-schranz
Copy link

@ondrejmirtes thank you 👍

@github-actions
Copy link

github-actions bot commented Dec 8, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants