Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PHPStorm treat a function as defined?

Tags:

ide

phpstorm

The double_metaphone() function is defined in a PECL extension and as such PHPStorm cannot see it being defined. I wouldn't like to see any warnings about this. I assume I could make PHPStorm treat this function call as defined through some kind of annotation but I don't know how to make this happen.

like image 937
László Monda Avatar asked Oct 12 '25 20:10

László Monda


1 Answers

You need what is called "stub files":

  1. Create a .php file and place it anywhere in your project (be it project itself... or as an External Library (Settings | PHP | Include paths) -- it does not matter, as long as PhpStorm can see it in this project).

  2. Add that function definition as it would be done in PHP itself: describe all parameters, return type etc. and just leave the body of the function empty.

    The documentation is optional: it's just the more doc you have the more useful it will be for PhpStorm and you: the IDE can warn you about invalid parameter type, incorrect return type usage, suggest variables of appropriate types when using code completion for that function etc.

  3. That's it

That's exactly how ALL known PHP functions/classes/etc are done in PhpStorm in the first place: just Ctrl + Click on any standard function/class/constant and see it yourself.

An example: how standard bin2hex function is defined (back in 2013):

<?php
/**
 * (PHP 4, PHP 5)<br/>
 * Convert binary data into hexadecimal representation
 * @link http://php.net/manual/en/function.bin2hex.php
 *
 * @param string $str A character.
 * @return string the hexadecimal representation of the given string.
 */
function bin2hex ($str) {}

You can see all current PhpStorm stubs (and other helper files that IDE uses for PHP completion) in this official repo: https://github.com/JetBrains/phpstorm-stubs

like image 164
LazyOne Avatar answered Oct 15 '25 21:10

LazyOne