Phpstorm: How to Add Method Stubs from a Pecl Library That PHPstorm Doesn't Currently Support

PhpStorm: How to add method stubs from a PECL library that PhpStorm doesn't currently support?

Just place such stub file(s) anywhere in your project (or reference in any other supported way, e.g. as Settings | Languages & Frameworks | PHP | Include path) -- IDE will use it for code completion (and similar) purposes only (so you can exclude them from uploading/VCS etc).

In long term -- you may submit the PR to the already mentioned PhpStorm stubs repository and it may become part of the standard PhpStorm distribution on next release.

How to load php DS namespace in PhpStorm

You need stub files in order for PhpStorm to understand what DS (a PHP extension written in C) offers (classes/functions/etc).

Stub file is basically a PHP version of that -- just "declaration/documentation" part -- e.g. function declarations but with empty bodies. You can either make your own ... or find some existing ones (if somebody else did it already, of course).

A bit more on stub files in general:

  • https://stackoverflow.com/a/15775431/783119
  • https://stackoverflow.com/a/30329412/783119

So .. basically what you need is php-ds/polyfill -- "You should also include the polyfill in your project for compatibility and IDE integration" .

IDE will read such files and will understand what real DS extension has to offer/what it does (type of parameters/returns/etc).

IntelliSense support for COM interfaces contained in a type library

As suggested by LazyOne, the manual approach can be as simple as writing a stub declaration and dumping the php file somewhere in the project tree or search path (like an apposite stubs directory):

<?php
/** allows observing the server lock/object counts and unloadability state of a COM module that uses the
* Zrbj.COM.ComServerLocking infrastructure
* @property-read int $Revision revision of the observer module implementation
* @property-read string $ServerDLL executable which houses the code for this observer object
*/
interface ISrvDllObserver
{
/** server lock count information (ICounterInfo) for this object's own server module */
public function GetCountsForOwnModule (): ICounterInfo;

/** server lock count information (ICounterInfo) for a loaded COM module that uses Zrbj.COM.ComServerLocking.
* @param string $module_name basename without file extension is sufficient unless there are multiple loaded
* modules with the same basename; use path and/or file extension to disambiguate
*/
public function GetCountsForLoadedModule (string $module_name): ICounterInfo;
}

From that point on IntelliSense works perfectly.

The manual process may give perfect results, but it is exceedingly laborious. I remembered a scriptable object for processing type libraries that shipped with FoxPro and earlier versions of Visual Studio, tlbinf32.dll (+ tlbinf32.chm). The ProgID to start with is TLI.TLIApplication.

The only extant reference to this tool that I could find at microsoft.com is this old article:

Visual Basic: Inspect COM Components Using the TypeLib Information Object Library

The tool still ships with Visual Studio, but it is now called vstlbinf.dll and no longer documented. vstlbinf.dll still refers to the old tlbinf32.chm as its help file but that doesn't get shipped anymore. The old documentation is still useful for understanding the object model, even though it refers to a version that is two decades older. (Note: it may be necessary to register the DLL manually.)

So I took Delphi and set about taming TLI.TLIApplication. It turned out not to be a whole lot easier than working with ITypeLib, ITypeInfo etc. directly, but the advantage is that TLI.TLIApplication can be used from any scripting language, including PHP itself.

Here`s a sample of the information that can be ripped from a COM type library for use in a PHP stub:

<?php
// 'k:\VS2019\Community\Common7\IDE\vstlbinf.dll' (2021-04-21 10:05:35)
// processed 2021-05-02 22:39:10 by Zrbj.COM.PhpStubs.pas rev. 2021-05-02
//
// Library: TLI
// Version: 1.0
// LIBID : {8B217740-717D-11CE-AB5B-D41203C10000}
// Comment: TypeLib Information
// 32 interface(s) and 3 coclass(es)
//
// coclasses:
// * {8B217752-717D-11CE-AB5B-D41203C10000} -> _SearchHelper (ProgID TLI.SearchHelper)
// 'Helper object for GetMembersWithSubString and multiple TypeLibs'
// * {8B217746-717D-11CE-AB5B-D41203C10000} -> _TypeLibInfo (ProgID TLI.TypeLibInfo)
// 'TypeLib information'
// * {8B21775E-717D-11CE-AB5B-D41203C10000} -> _TLIApplication (ProgID TLI.TLIApplication)
// 'TLIApplication object'

/// {8B21774B-717D-11CE-AB5B-D41203C10000} dual nonextensible dispatchable
/** VarType information for parameters and return types
* @property-read TypeInfo $TypeInfo Type information for VT_PTR VarType
* @property-read int $TypeInfoNumber TypeInfo number for 0 VarType (Cheaper than TypeInfo property)
* @property-read variant $TypedVariant Get a variant with this VarType
* @property-read bool $IsExternalType Is TypeInfo external to this library
* @property-read TypeLibInfo $TypeLibInfoExternal External typelib. Same as TypeInfo.Parent.
* @property-read int $PointerLevel Dereferencing level of type
* @property-read int $VarType VarType of Parameter
* @property-read int $ElementPointerLevel Dereferencing level for type of an array element
*/
interface VarTypeInfo
{
/** Get bounds for VT_VECTOR array. LBound in column 1, UBound in column 2. */
public function ArrayBounds (int $Bounds): int;
}

/// {8B217749-717D-11CE-AB5B-D41203C10000} dual nonextensible dispatchable
/** Parameter Information
* @property-read string $Name Name of the object
* @property-read bool $Optional Optional Parameter
* @property-read VarTypeInfo $VarTypeInfo VarTypeInfo object for this parameter
* @property-read bool $Default Default Parameter
* @property-read variant $DefaultValue Default value
* @property-read bool $HasCustomData Check if custom data is available
* @property-read CustomDataCollection $CustomDataCollection Custom data GUIDs and Values
* @property-read int $Flags Parameter Flags
*/
interface ParameterInfo
{
}

...

Conspicuously absent are parameter comments, because there is no such thing in Microsoft IDL or type libraries. But on the whole the result of processing type libraries into PHP stubs seems quite satisfactory, and it certainly makes working with COM objects in PHP a lot easier.

How to enable autocomplete in PhpStorm for Datatrax php-driver

For all future visitors of this question. Here is the answer.

Checkout that repository.

PHP-Cassandra-Stub (for DataStax php-driver)

Is there a way to let PhpStorm know that a method returns nothing but to throw an exception?

ATM -- nope.

https://youtrack.jetbrains.com/issue/WI-10673 might be a solution (once it will be implemented).

Right now even declaring throw_function() with @throws Exception does not help (as throwing an exception is just one of the possible scenarios and not an obligation).

Watch that and the following related tickets (star/vote/comment) to get notified on any progress:

  • https://youtrack.jetbrains.com/issue/WI-7462
  • https://youtrack.jetbrains.com/issue/WI-6562

Right now I simply suggest to rewrite the code in a following more straightforward and easier-to-read fashion:

if (!$condition) {
throw_exception();
}

$foo = 'bar';

echo $foo;

If condition is not met then exit happens sooner (due to the thrown exception) and the code below will simply not be executed. It's much easier to read and understand this way (to follow the code execution flow).

PhpStorm class list

For code completion PhpStorm collects entries from the code in your project (code written in PHP) and from own stubs for PHP core functions and other common PHP extensions (as those extensions are compiled binary files (.dll/.so file) usually written in C).

You can Ctrl + Click on that class in the IDE and it will take you to the class declaration, which will be one of the stub files.

Imagick is a native PHP extension and you have to make sure that you actually have it installed in your PHP to have your code working properly.

https://www.php.net/manual/en/imagick.installation.php


You can tell PhpStorm to only include stub entries for what you actually have installed in your current PHP Interpreter. This way you will not have entries in Code Completion for classes/functions that come from not-available-in-your-setup extensions.

Check Settings/Preferences | Languages & Frameworks | PHP, "PHP Runtime Tab" tab for that.

https://www.jetbrains.com/help/phpstorm/php.html#php-runtime-tab

How to have eclipse resolve php classes in MongoDB\BSON namespace?

Reading this post and not finding anything newer on the subject I arrived to the conclusion that developers of the driver don't provide the php sources and leave it to the IDE developers to provide STUBs for the different fuctions of the driver in a way the IDE can recognize the functions and provide syntax checking and documentations popups.

Following this question, I finally ended up finding a JetBrains GitHub where there are many different stubs for PHPStrom, this one among them.

So I copied the file and added it to my project as part of the sources. And that solved my problem.



Related Topics



Leave a reply



Submit