mago icon indicating copy to clipboard operation
mago copied to clipboard

feature: allow creating custom linting rules using PHP.

Open azjezz opened this issue 8 months ago • 3 comments

azjezz avatar Apr 04 '25 20:04 azjezz

This would be epic indeed. Do you have the same in mind for analyzer? One of the things I love doing most is creating PHPStan rules to steer a project in a certain direction. Having the full type context is really good to have in order to not have a rule producing false positives.

ruudk avatar Aug 29 '25 07:08 ruudk

for linter, you won't have type information, that's just how the linter is.

But i think if we managed to get this implemented, we would also be able to implement custom heuristic checks for the analyzer using PHP, i.e ability to post process statements, expressions, class-like, and function-like nodes after they have been fully analyzed, which would give you access to the type information.

azjezz avatar Aug 29 '25 11:08 azjezz

from discord:

would be pretty nice for organizations to be able to create their own rules using php something along the lines of:

<?php

use Mago\Ast\Node;
use Mago\Ast\ExitConstruct;
use Mago\Reporting\Issue;

Mago\Linter\rule('no-exit', function(Node $node): ?Issue {
   if (!$node instanceof ExitConstruct) {
     return null;
   }

   return Issue::error("we said no exit!")->withAnnotation(
     Annotation::primary($node->span())->withMessage("Here!")
   );
});
[linter]
custom-rules = ["rules/no-exit.php"]

azjezz avatar Aug 29 '25 11:08 azjezz