mglaman / drupal-check

Check Drupal code for deprecations and discover bugs via static analysis
GNU General Public License v2.0
331 stars 67 forks source link

#280: allow custom configuration #281

Closed Dropa closed 1 year ago

Dropa commented 2 years ago

Provides ability to create custom file like custom-phpstan.neon

# See https://phpstan.org/user-guide/ignoring-errors#generate-an-ignoreerrors-entry for formatting
parameters:
    ignoreErrors:
        - '#^Access to an undefined property Drupal\\Core\\Field\\FieldItemListInterface<Drupal\\Core\\Field\\FieldItemInterface>\:\:\$date\.$#'

and use it with

drupal-check -i custom-phpstan.neon web/modules/custom
Dropa commented 2 years ago

To clarify, the example can be reached two ways, either

$date = $entity->get('field_datefield')->date;

and ignore with

- '#^Access to an undefined property Drupal\\Core\\Field\\FieldItemListInterface<Drupal\\Core\\Field\\FieldItemInterface>\:\:\$date\.$#'

but it could make more sense to be precise when you accept the error, meaning the code would need to be

/** @var \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem $dateField */
$dateField = $entity->get('field_datefield');
$date = $dateField->date;

and then ignore it with

- '#^Access to an undefined property Drupal\\datetime\\Plugin\\Field\\FieldType\\DateTimeItem\:\:\$date.#'
mglaman commented 1 year ago

Folks should be using PHPStan directly to overcome these issues.

Dropa commented 1 year ago

Could you elaborate? What exactly is drupal-check acceptable way of writing line

$date = $entity->get('field_datefield')->date;

There are a lot of cases where drupal-check prints out errors where you're really not doing anything wrong, such as implementing hooks where original API didn't specify variable types, calling method or property of known(/provided) field etc.

Also, drupal-check some times fails to load certain vendor module files, I assume that's because of different PSR, but once again, allowing custom phpstan rules (including given vendor files) to be added to the ones already provided nicely working solution.

mglaman commented 1 year ago

I closed this for the request of "allow custom configuration"

Can you open a discussion at https://github.com/mglaman/phpstan-drupal/discussions for ways to write:

$date = $entity->get('field_datefield')->date;

That way others can join in. There isn't one straight forward way.

You can always implement types in your hooks, it doesn't matter. And you should be doing so.

fails to load certain vendor module files

That is a bug you should open for phpstan-drupal, it's not drupal-check which is merely a wrapper.