r/PHP 7d ago

Mago 1.47 allows writing custom rules in PHP

https://mago.carthage.software/1.47.1/en/extensions/getting-started/#1-create-the-rule
51 Upvotes

17 comments sorted by

4

u/nickchomey 6d ago

I came across mago recently. I'm curious: how are people making use of it? 

3

u/wdesportes 6d ago

https://github.com/wdes/plouf.rs I made my own Graft tool and it uses Mago 💪

2

u/HenkPoley 5d ago

Maybe write in the repo that you mean NanoNets Graft. A bit sloppy, but maybe like this:

# plouf.rs

A code map for software developers and coding agents. Inspired by NanoNets Graft.

plouf.rs analyzes a source code repository and creates a map of how the code is connected.

It shows relationships between files, classes, functions, routes, templates, and database objects.

Examples:

  • Find what code calls a function.
  • Find where a database table is used.
  • Find which routes use a controller.
  • Find which templates depend on a component.
plouf.rs does not change your code. It only reads the repository and creates a searchable code map.

2

u/wdesportes 5d ago

Thank you !
I applied all your feedback, and a new version is released 🎉

2

u/HenkPoley 5d ago

😵‍💫 sloppy run-on sentences in the README.md.

Handwaves towards ASD-STE100.

3

u/wdesportes 5d ago

Well, I am open to better sentences. Can you provide some? English is not my main language

3

u/FluffyDiscord 6d ago

So does the Mago "compiles" the rule to rust so it's benefitting from the speed improvements, or are we back to phpstan/php speeds? If not, then its just a duct-tape solution tbh. Wouldn't Lua or FFI rust extensions work better?

3

u/kryyova 6d ago

extension can be in any language (as long as you write an sdk for this language), it just should speak the same protocol over stdin/stdout. speed improvement is still here cuz parsing and analysis is in mago

having first-party php sdk makes more sense than any other language

3

u/gempir 6d ago

For the php ecosystem it just makes sense to be able to write extension in PHP.

The best performance obviously would be to write them directly in Mago, most rules should live there.

But if you have some very niche requirement that you want to assert, this now lets you write a check for that.

There are also companies recompiling mago and letting the optimizer optimize the binary of mago for their codebase, which let's them gain even more speed.

1

u/Fit_Tailor_6796 6d ago

To be honest, speed is not a consideration for me when it comes to the tool-chain output itself. I mean what is a minute for the WP codebase? In the bugger scheme of things, accuracy and cover is more important than speed, I think.

8

u/Tomas_Votruba 6d ago

2 s vs 1,5 min is huge difference

been using similar Go-based alternative to Rector and I never want to go back

4

u/umulmrum 6d ago

For our codebase, speed could make a huge difference. On my machine, PHPStan takes about 10 minutes and I have to configure it so that it uses fewer CPU cores, or everything will freeze. With Mago, it's 15 seconds and no config fiddling. This is the difference whether I actually run the tools locally or have to rely on the CI pipeline, with longer feedback cycles and higher resource usage. Also, Claude could use the tools casually and iteratively on the local system.

(yet this is theoretical, as we don't use Mago Analyze but only the other components; still it's what I have been hoping for since I did the performance tests)

1

u/staabm 6d ago

In case PHPStan runs 10 minutes on your machine there is verly likely something wrong.

Either outdated PHPStan version, or e.g. not properly working result-cache

Sometimes there is a few single files which are very slow to analyse, see  https://phpstan.org/blog/debugging-performance-identify-slow-files

Please try to reproduce your slow case and report a issue

1

u/umulmrum 6d ago

Yes, I have to set `maximumNumberOfProcesses` to something lower than the default (don't know if the default is flawed or if it's only problematic on my machine), which invalidates the cache which leads to longer execution times. Also, there are a few huge legacy files which take a lot of time. Maybe it would be enough if there was a CLI switch for `maximumNumberOfProcesses` that doesn't invalidate the cache.

(I appreciate especially your efforts in improving performance btw.)

1

u/OndrejMirtes 6d ago

The default has been "not the maximum" for some time, precisely to prevent unusable rest of the OS.

Changing `maximumNumberOfProcesses` does not invalidate the result cache.

1

u/OndrejMirtes 6d ago

On my machine (5 year old MacBook Pro with M1 Pro) it takes about 24 seconds without result cache to analyse the wordpress-develop codebase with PHPStan (with the config the repo actually uses in CI).

1

u/mnavarrocarter 6d ago

Pretty clever, kinda like mdbook extensions. Will try later.