phpro / grumphp

A PHP code-quality tool
MIT License
4.15k stars 431 forks source link

Is it possible to write extension for phpro/grumphp-shim? #1154

Closed mrmishmash closed 4 weeks ago

mrmishmash commented 4 weeks ago
Q A
Version n/a
Bug? no
New feature? no
Question? yes
Documentation? no
Related tickets n/a

We are using phpro/grumphp-shim in our project. I want to create an extension and use it. But I cannot use the ExtensionInterface since all the files are packaged in the .phar file.

Is it possible at all to write an extension for the grumphp-shim version of the project?

Thank you for any help.

veewee commented 4 weeks ago

Hello

Yes it is possible. The extension interface is indeed located inside the phar. But it is not scoped.

Therefore, You can just use it.

Here is an example extension that works on both shim and regular:

https://github.com/phpro/grumphp-combined-coverage-extension/tree/main

mrmishmash commented 4 weeks ago

Hello @veewee

Thank you for the suggestion. I've got it working after realizing that actually I needed to just use ConfigOptionsResolver::fromClosure() instead of fromOptionsResolver() which is mentioned in the v2 release.

So now I got it working locally in my repo, but when I run it in another repo that requires my package through composer, I get:

TypeError thrown in context with message "GrumPHP\Runner\TaskHandler\TaskHandler::{closure}(): Argument #1 ($task) must be of type GrumPHP\Task\TaskInterface, __PHP_Incomplete_Class given

Not sure what I'm doing wrong, thought it was limited to my package but e.g. I installed this one https://github.com/indykoning/grumphp-prettier and ran it and get the same thing.

Is this some kind of misconfiguration on my part? Sorry to spam this issue ticket, but would be really nice if I could get past this. Found nothing on the internet so far.

veewee commented 4 weeks ago

Hello

It seems like an autoloading issue at first sight. Haven't really bumped into it, so I don't know what exactly could be wrong.

If you follow the example I mentioned above, you should be fine though. We're using that one on a daily basis in our team in combination with grumhp-shim

mrmishmash commented 4 weeks ago

Okay so I turned off 'parallel' and then I got:

Task{closure}(): Return value must be of type Symfony\Component\Process\Process, _HumbugBox46f787f58329\Symfony\Component\Process\Process returned

I was using the Phpcs Task as reference from the GrumPHP repository. That uses a closure:

$process = TmpFileUsingProcessRunner::run(function (string $tmpFile) use ($config): Process {

Turns out I cannot return a Process from the closure since in my code it's got the original namespace and in the shim that's changed due to the packer. Okay, so I just changed that to mixed, not ideal but maybe I'm not doing something correctly like I was with the ConfigOptionsResolver.

I see someone already had the exact same issue https://github.com/phpro/grumphp/issues/1087 (regarding the __PHP_Incomplete_Class) which I guess could be an auto loading issue but it works well enough in the non-shimmed version. Very puzzling.

I'll keep trying.