Jump to content

Topic on Project:Support desk

Lwangaman (talkcontribs)

Would it be possible to have some official stubs at least for Mediawiki core? I have made an attempt here: https://github.com/JohnRDOrazio/mediawiki-stubs, however I have not been very successful in getting it to work in VSCode with intelephense. Perhaps someone more experienced with PHPStan or `nikic/php-parser` or similar tools could provide a repository with official stubs that can be used with VSCode intelephense? It would make development of plugins or code contributions to core that much easier...

And perhaps this should be another topic, but along the sames lines of development environment: when using composer to add the mediawiki codesniffer rules to my project, i.e.

        "require-dev": {
                "mediawiki/mediawiki-codesniffer": "43.0.0",
                "mediawiki/mediawiki-phan-config": "0.14.0",
                "mediawiki/minus-x": "1.1.3",
                "php-parallel-lint/php-console-highlighter": "1.0.0",
                "php-parallel-lint/php-parallel-lint": "1.4.0"
        }

I get the following error:

phpcs: Trait "MediaWiki\Sniffs\PHPUnit\PHPUnitTestTrait" not found in {path-to-project}/vendor/mediawiki/mediawiki-codesniffer/MediaWiki/Sniffs/PHPUnit/SetMethodsSniff.php on line 15

What am I missing to get this to work nicely with VSCode?

I have a .phpcs.xml file in the root folder of my project:

<?xml version="1.0"?>
<ruleset>
    <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
    <file>.</file>
    <arg name="extensions" value="php"/>
    <arg name="encoding" value="UTF-8"/>
</ruleset>
MarkAHershberger (talkcontribs)

I'm not using VSCode, but I do use intelephense and I'm having some success. Since it works for me without stubs, I'm curious what you mean about stubs? That said I do use "intelephense.environment.includePaths" to point to the mediawiki core when I'm working on extensions.

Does that help?

Lwangaman (talkcontribs)

A stubs file is a PHP file containing all of the function / method / constant signatures for a codebase together with their Doc Blocks, so that even without having a local copy of the Mediawiki core, you won't get "undefined method" or similar errors in the Code editor, and you get information about the class or method or constant when you hover over it (so you can see which parameters a method expects for example, or which methods are available on a class instance).

I think that the trouble I was having may be due to the memory limit imposed by intelephense (see https://github.com/bmewburn/vscode-intelephense/issues/3083#issuecomment-2394779492). My stubs file is 9MB, so `intelephense.files.maxSize` needs to be set to a value greater than 9MB. I haven't gotten around to testing this yet, but I wouldn't be surprised if that were the issue. My attempt at producing the stubs file is fairly rudimentary, and winds up being more bloated than necessary (classes within the same namespace should be grouped together within the same namespace block instead of being each in their own namespace block, for example). 9MB seems a bit much for a stubs file, and it could perhaps be broken down into multiple stubs files.

An example of a similar stubs file for WordPress: https://raw.githubusercontent.com/php-stubs/wordpress-stubs/refs/heads/master/wordpress-stubs.php. This stubs file is 4.66MB.

Lwangaman (talkcontribs)

I have finally fixed generation of the MediaWiki intelephense stubs, after spending a whole day to fine tune the generation script. The updated stubs: https://github.com/JohnRDOrazio/mediawiki-stubs/tree/main/stubs. I have also published the generation script on packagist: https://packagist.org/packages/johnrdorazio/mediawikistubs.

I manually added the stubs to the VSCode intelephense `node_modules` folder and enabled `mediawiki` as an entry in the extension's `package.json`, and I now finally have intelligent recognition of core classes, methods, definitions, etc. I requested that the stubs be added by default to the extension stubs, see https://github.com/bmewburn/vscode-intelephense/issues/3083#issuecomment-2455722065.

Reply to "Mediawiki PHP stubs"