- Issue created by @mparker17
- Status changed to Needs review
almost 2 years ago 10:08pm 29 March 2023
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);
}
}
Fix the typehints in \Drupal\elasticsearch_connector\Event\PrepareIndexEvent
.
Fix the typehints in \Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent
.
None.
None.
None.
Needs review
7.0
Code