2

I'm using PhpStorm as current IDE.

The only issue I have with it is that it can't find my php-library.

I've installed this: PHP-JS.

And by install, i mean generated the php-js.so file which I've loaded via php5enmod php-js.

The library works:

// This is a valid namespace AND classname
$context = new JS\Context;
$result  = $context->evaluate("var x = 3; var y = 4; var z = x + y; ++z;");
// will output 8

For some (well, not that strange) reason, PhpStorm can't find the JS namespace and consequently no classes from that namespace.

Is there a way I can tell PhpStorm that I'm actually calling an existing method from a valid class from a valid namespace?

I've tried to make it work using docBlock tags but none seems to match what I need.

6
  • 3
    There is NO WAY that PhpStorm will read your binary .so file and detect PHP code/interface from there. You need to do the same as ALL other binary php extensions do (e.g. Phalcon) -- makes stubs (limited PHP version) of your classes (classes/methods/functions/etc with full doc but empty bodies). You can Ctrl+Click on any standard PHP class (e.g. MySQL) and see how it's done yourself. Commented Mar 10, 2016 at 16:14
  • @LazyOne, I'm not expecting PhpStorm to read my .so file. I'm asking how to let it know i'm actually using a valid namespace. And not mark it as invalid Commented Mar 10, 2016 at 16:17
  • @AlexTartan Like LazyOne said, PhpStorm only knows the namespaces when there are stubs for it or full plaintext classes referenced via composer/include. See GitHub repo of JetBrains PhpStorm method stubs: github.com/JetBrains/phpstorm-stubs Commented Mar 10, 2016 at 16:22
  • @AlexTartan Just do as I have said in my last sentence (or follow the link DanFromGermany has provided) -- it's the same stuff. Once you see how it's done there .. you can do exactly the same with your stubs for that PHP-JS library. Then just place them anywhere in your project -- it will be used by IDE only. Commented Mar 10, 2016 at 16:30
  • @LazyOne, Thanks for pointing me in the right direction. I'm gonna make stubs for each class. Commented Mar 10, 2016 at 16:39

1 Answer 1

4

There is no way that PhpStorm will read your binary .so file (.dll on Windows) and extract PHP interfaces from there (and by "PHP interface" I mean classes/functions/constants/etc that extension offers during runtime).

Therefore you would need to do the same as all other binary php extensions would do (e.g. Phalcon's DevTools) -- make stubs files.

Stub file is a limited PHP version of your classes/methods/functions/etc with (optional) full doc but empty bodies. You can Ctrl + Click on any standard PHP class (e.g. MySQL) and see yourself how it's done in PhpStorm (IDE will open corresponding stub file in another editor tab) .. or just browse their stub repository directly (thanks @DanFromGermany for the link -- https://github.com/JetBrains/phpstorm-stubs).

An example of such stub file would be (it covers the code from your example):

<?php

namespace JS;

class Context
{
    /**
     * Evaluate your JS code
     *
     * @param string $param JS code to evaluate
     * @return mixed
     */
    public function evaluate($param) {}
}

Just place such stub files somewhere in your project (or reference any other supported way -- e.g. via PHP | Include paths) -- this code is for IDE only.


Such stub files can be used by any IDE/editor that can parse source .php files and offer classes/functions from there in code completion. Therefore it makes perfect sense to ask developers of that PHP-JS to provide such stubs automatically (just like Phalcon devs do).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.