povils / phpmnd

PHP Magic Number Detector
MIT License
550 stars 46 forks source link

Ignore line or array specific key declaration #147

Open FlorentTorregrosa opened 2 years ago

FlorentTorregrosa commented 2 years ago

Hello,

First, thanks for the tool!

I have added it to my development stack, and use it against Drupal contrib modules that I maintain.

Is there a way to ignore a specific line with a comment? Like PHPStan, PHPCS, etc. do.

And is there a way to ignore numbers in keyed array for specific keys?

Because in Drupal, forms are declared with arrays and I think it would be an overburden to have to declare a constant for each weight or size of every element of each form.

Example of form https://git.drupalcode.org/project/qwantsearch/-/blob/8.x-1.x/src/Form/Settings.php#L124

Also, there are still procedural parts in Drupal, such as for declaring custom tables https://git.drupalcode.org/project/i18n_sso/-/blob/8.x-1.x/i18n_sso.install#L40. Ok we can create an interface and call it in the procedural parts.

So would it be possible to have a new option to have a way to ignore array keys:

#weight
#size

Thanks for any reply.

Best regards,

sidz commented 2 years ago

Hi @FlorentTorregrosa

Nope it is not possible to ignore a specific line of code in this moment.

FlorentTorregrosa commented 2 years ago

Hi @sidz,

Thanks for the quick reply. Ok, so I didn't miss something :)

exussum12 commented 2 years ago

You can ignore every instance by using intval(10) instead of just 10. It's not ideal as you wouldn't know by looking at intval(10) that it's used to ignore a check for phpmnd.

Sounds a good feature request though :)

FlorentTorregrosa commented 2 years ago

Yes and also it adds unneeded function calls.

I also tried to use string, the Drupal form API is enough permissive to do that, but I don't want to do that too. Typing is more and more enforced and changing the values type just to satisfy a code quality tool is a non-sense in my opinion.

Thanks for your feedback :)

exussum12 commented 2 years ago

Yeah agreed. I think the same about comments to ignore lines tbh. The array key whitelist is not a bad idea.

There is also a check in phpmnd for numeric strings which is good to enable as well. Which stops just hiding numbers by converting to a string

povils commented 2 years ago

I like the feature to ignore specific array keys :) I see how it could be useful

MGatner commented 2 years ago

I would love to add this tool to my default workflow but it runs against 30+ projects and being able to "exempt" individual lines would be absolutely crucial. The array keys bit is not a deal-breaker for me, especially since you could always do this:

$weight = 10; // @phpmnd-ignore-line
$data = [
    'weight' => $weight,
];

I would like to put forth a bounty on the line exemption comment feature - anyone willing to match? @exussum12 Can you provide a fair amount or expected time to implement?