Fix typehints in PrepareIndexEvent and PrepareIndexMappingEvent

Created on 29 March 2023, almost 2 years ago
Updated 9 October 2023, over 1 year ago

Problem/Motivation

PHPStan is a PHP Static Analysis tool that I use to detect problems with my code. For example, if I were to use array syntax to set an object property (e.g.: if I call $a['property'] = 'value'; when $a is an object), and try to run that code, PHP would halt with the fatal error "Cannot use object of type \Foo\Bar as array".

However, if I run PHPStan on my code first, and my code uses PHP 8 type declarations and/or @var, @param and @return docblock hints (as per Drupal's API documentation and comment standards ), then PHPStan can use those type hints to predict that fatal error ahead of time, giving me time to fix it before running that code.

(Note that PHPStan has been added to Drupal.org's testbot , so the module maintainers can start using static analysis on this module's code by setting up automated testing on this branch - 8.x-7.x-dev.)

The Elasticsearch Connector module has incorrect or missing @var, @param and @return docblock hints in its \Drupal\elasticsearch_connector\Event\PrepareIndexEvent and \Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent APIs...

  class PrepareIndexMappingEvent extends Event {
    /**
     * Getter for the index params array.
     *
     * @return indexMappingParams  (it interprets "indexMappingParams" as the name of a class - should be "array")
     */
    public function getIndexMappingParams() {
      return $this->indexMappingParams;
    }
  }
# ...
  class PrepareIndexEvent {
    /**
     * Getter for the index config array.
     *
     * @return indexConfig  (it interprets "indexConfig" as the name of a class - should be "array")
     */
    public function getIndexConfig() {
      return $this->indexConfig;
    }
  }

... this means that PHPStan will throw errors on (custom) code like the following...

  class ElasticsearchEventSubscriber implements EventSubscriberInterface {
    public static function getSubscribedEvents() {
      $events[PrepareIndexEvent::PREPARE_INDEX] = ['prepareIndex'];
      $events[PrepareIndexMappingEvent::PREPARE_INDEX_MAPPING] = ['prepareIndexMapping'];
      return $events;
    }
    public function prepareIndex(PrepareIndexEvent $event) {
      $indexConfig = $event->getIndexConfig();
      $indexConfig['body']['settings']['index']['max_result_window'] = 20000;
      $event->setIndexConfig($indexConfig);
    }
    public function prepareIndexMapping(PrepareIndexMappingEvent $event) {
      $mapping = $event->getIndexMappingParams();
      $mapping['body']['properties']['field_example']['analyzer'] = 'my_analyzer';
      $event->setIndexMappingParams($mapping);
    }
  }

Proposed resolution

Fix the typehints in \Drupal\elasticsearch_connector\Event\PrepareIndexEvent.

Fix the typehints in \Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent.

Remaining tasks

  1. Review and feedback
  2. RTBC and feedback
  3. Commit

User interface changes

None.

API changes

None.

Data model changes

None.

📌 Task
Status

Needs review

Version

7.0

Component

Code

Created by

🇨🇦Canada mparker17 UTC-4

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024