From 967e4a6a1a65c8095b30df34408a123b1d418717 Mon Sep 17 00:00:00 2001 From: Lee Mills Date: Sat, 12 Oct 2024 14:26:15 +0100 Subject: [PATCH 1/2] fix: strict types errors --- localgov_directories.install | 2 +- localgov_directories.module | 2 +- .../tests/Kernel/InstallTest.php | 2 +- .../localgov_directories_location.module | 2 +- .../src/ProximitySearchSetup.php | 2 +- .../src/FacetMapping.php | 4 +- src/ConfigurationHelper.php | 4 +- .../LocalGovDirectoriesQueryType.php | 49 ++++++++++--------- tests/src/Functional/FacetsTest.php | 2 +- tests/src/Unit/FacetPreprocessTest.php | 4 +- 10 files changed, 39 insertions(+), 34 deletions(-) diff --git a/localgov_directories.install b/localgov_directories.install index 89c02c2..0650c64 100644 --- a/localgov_directories.install +++ b/localgov_directories.install @@ -192,7 +192,7 @@ function localgov_directories_update_8004() { // Place under title field. $form_group_description = $form_display->getThirdPartySetting('field_group', 'group_description'); if (!empty($form_group_description) && - (($title_position = array_search('title', $form_group_description['children'])) !== FALSE) + (($title_position = array_search('title', $form_group_description['children'], TRUE)) !== FALSE) ) { array_splice($form_group_description['children'], $title_position + 1, 0, 'localgov_directory_title_sort'); $form_display->setThirdPartySetting('field_group', 'group_description', $form_group_description); diff --git a/localgov_directories.module b/localgov_directories.module index c2a2a06..f24690a 100644 --- a/localgov_directories.module +++ b/localgov_directories.module @@ -82,7 +82,7 @@ function localgov_directories_localgov_roles_default() { * Implements hook_modules_installed(). */ function localgov_directories_modules_installed($modules) { - $services = in_array('localgov_services_navigation', $modules); + $services = in_array('localgov_services_navigation', $modules, TRUE); if ($services) { \Drupal::service('config.installer')->installOptionalConfig(NULL, [ 'config' => 'field.storage.node.localgov_services_parent', diff --git a/modules/localgov_directories_db/tests/Kernel/InstallTest.php b/modules/localgov_directories_db/tests/Kernel/InstallTest.php index 0a09a68..aef97b2 100644 --- a/modules/localgov_directories_db/tests/Kernel/InstallTest.php +++ b/modules/localgov_directories_db/tests/Kernel/InstallTest.php @@ -39,7 +39,7 @@ class InstallTest extends KernelTestBase { * {@inheritdoc} */ public function setUp(): void { - parent::setup(); + parent::setUp(); $this->installEntitySchema('node'); $this->installEntitySchema('search_api_index'); diff --git a/modules/localgov_directories_location/localgov_directories_location.module b/modules/localgov_directories_location/localgov_directories_location.module index 018cdaf..c23bf42 100644 --- a/modules/localgov_directories_location/localgov_directories_location.module +++ b/modules/localgov_directories_location/localgov_directories_location.module @@ -70,7 +70,7 @@ function localgov_directories_location_field_config_insert(FieldConfigInterface if (in_array($new_field_name, [ Directory::CHANNEL_SELECTION_FIELD, Directory::TITLE_SORT_FIELD, - ]) && $is_node_entity_type && $index) { + ], TRUE) && $is_node_entity_type && $index) { // The channel selection and title sort fields may have gone missing from // the search index when we added localgov_location. $proximity_search_setup->repairSearchIndex($index); diff --git a/modules/localgov_directories_location/src/ProximitySearchSetup.php b/modules/localgov_directories_location/src/ProximitySearchSetup.php index 37c80cf..a810040 100644 --- a/modules/localgov_directories_location/src/ProximitySearchSetup.php +++ b/modules/localgov_directories_location/src/ProximitySearchSetup.php @@ -83,7 +83,7 @@ public function setupLocationSearch(FieldConfigInterface $field, SearchIndexInte $entity_bundle = $field->getTargetBundle(); $datasrc_cfg = $index_datasrc->getConfiguration(); - $is_bundle_indexed = in_array($entity_bundle, $datasrc_cfg['bundles']['selected']); + $is_bundle_indexed = in_array($entity_bundle, $datasrc_cfg['bundles']['selected'], TRUE); if (!$is_bundle_indexed) { $datasrc_cfg['bundles']['selected'][] = $entity_bundle; $index_datasrc->setConfiguration($datasrc_cfg); diff --git a/modules/localgov_directories_or/src/FacetMapping.php b/modules/localgov_directories_or/src/FacetMapping.php index 04405e8..1763345 100644 --- a/modules/localgov_directories_or/src/FacetMapping.php +++ b/modules/localgov_directories_or/src/FacetMapping.php @@ -76,7 +76,7 @@ public function synchroniseFacetMappings() { ->accessCheck(TRUE) ->execute(); foreach ($facet_query as $facet_type) { - if (in_array($facet_type, $openreferral_facets) && + if (in_array($facet_type, $openreferral_facets, TRUE) && !$mapping_storage->load('localgov_directories_facets.' . $facet_type) ) { $facet_mapping = $mapping_storage->create([ @@ -92,7 +92,7 @@ public function synchroniseFacetMappings() { ]); $facet_mapping->save(); } - if (!in_array($facet_type, $openreferral_facets) && + if (!in_array($facet_type, $openreferral_facets, TRUE) && ($facet_mapping = $mapping_storage->load('localgov_directories_facets.' . $facet_type)) ) { $facet_mapping->delete(); diff --git a/src/ConfigurationHelper.php b/src/ConfigurationHelper.php index f905688..4ea2df9 100644 --- a/src/ConfigurationHelper.php +++ b/src/ConfigurationHelper.php @@ -444,7 +444,7 @@ protected function indexAddBundle(IndexInterface $index, string $entity_type_id, $configuration = $datasource->getConfiguration(); $configuration['bundles']['default'] = FALSE; - if (!in_array($entity_bundle, $configuration['bundles']['selected'])) { + if (!in_array($entity_bundle, $configuration['bundles']['selected'], TRUE)) { $configuration['bundles']['selected'][] = $entity_bundle; } $datasource->setConfiguration($configuration); @@ -469,7 +469,7 @@ protected function indexRemoveBundle(IndexInterface $index, string $entity_type_ $configuration = $datasource->getConfiguration(); $configuration['bundles']['default'] = FALSE; - if (($key = array_search($entity_bundle, $configuration['bundles']['selected'])) !== FALSE) { + if (($key = array_search($entity_bundle, $configuration['bundles']['selected'], TRUE)) !== FALSE) { unset($configuration['bundles']['selected'][$key]); } $datasource->setConfiguration($configuration); diff --git a/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php b/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php index 235b3e9..4be0b43 100644 --- a/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php +++ b/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php @@ -24,29 +24,34 @@ public function execute() { $query = $this->query; // Only alter the query when there's an actual query object to alter. - if (!empty($query)) { - $operator = $this->facet->getQueryOperator(); - $field_identifier = $this->facet->getFieldIdentifier(); - $exclude = $this->facet->getExclude(); - - if ($query->getProcessingLevel() === QueryInterface::PROCESSING_FULL) { - // Set the options for the actual query. - $options = &$query->getOptions(); - $options['search_api_facets'][$field_identifier] = $this->getFacetOptions(); - } + // @phpstan-ignore-next-line. + if (empty($query)) { + return; + } - // Add the filter to the query if there are active values. - $active_items = $this->facet->getActiveItems(); + $operator = $this->facet->getQueryOperator(); + $field_identifier = $this->facet->getFieldIdentifier(); + $exclude = $this->facet->getExclude(); - if (count($active_items)) { + if ($query->getProcessingLevel() === QueryInterface::PROCESSING_FULL) { + // Set the options for the actual query. + $options = &$query->getOptions(); + $options['search_api_facets'][$field_identifier] = $this->getFacetOptions(); + } - $type_storage = \Drupal::entityTypeManager() - ->getStorage('localgov_directories_facets'); - $chosen_facets = $type_storage->loadMultiple($active_items); - foreach ($chosen_facets as $directory_facet) { - $bundle[$directory_facet->bundle()][] = $directory_facet->id(); - } + // Add the filter to the query if there are active values. + $active_items = $this->facet->getActiveItems(); + + if (count($active_items)) { + $bundle = NULL; + $type_storage = \Drupal::entityTypeManager() + ->getStorage('localgov_directories_facets'); + $chosen_facets = $type_storage->loadMultiple($active_items); + foreach ($chosen_facets as $directory_facet) { + $bundle[$directory_facet->bundle()][] = $directory_facet->id(); + } + if ($bundle !== NULL) { $filter = NULL; foreach ($bundle as $bundle_name => $group_items) { unset($filter); @@ -93,7 +98,7 @@ public function build() { // Remove any duplicate facets, or they show up multiple times. $group_facets_filtered = array_filter($group_facets, function ($item) use ($results) { $facet_ids = array_column($results, 'filter'); - if (in_array($item['filter'], $facet_ids)) { + if (in_array($item['filter'], $facet_ids, TRUE)) { return FALSE; } return TRUE; @@ -157,7 +162,7 @@ protected function getResultingFacetsWithOwnFacetGroup(string $filter_tag): arra $cids = $condition->getTags(); // @todo Check that we are only removing facet conditions. - if (in_array($filter_tag, $cids)) { + if (in_array($filter_tag, $cids, TRUE)) { // Store the removed conditions and remove it. $removed_conditions[$cid] = $conditions[$cid]; @@ -187,7 +192,7 @@ protected function getResultingFacetsWithOwnFacetGroup(string $filter_tag): arra ->execute(); $found_facets = $facets['localgov_directory_facets_filter'] ?? []; $found_facets = array_filter($found_facets, function ($item) use ($group_facet_ids) { - if (in_array(intval(trim($item['filter'], '"')), $group_facet_ids)) { + if (in_array(intval(trim($item['filter'], '"')), $group_facet_ids, TRUE)) { return TRUE; } return FALSE; diff --git a/tests/src/Functional/FacetsTest.php b/tests/src/Functional/FacetsTest.php index 5112d61..4aa401b 100644 --- a/tests/src/Functional/FacetsTest.php +++ b/tests/src/Functional/FacetsTest.php @@ -80,7 +80,7 @@ protected function setUp() :void { // Place the facet block. $this->drupalLogin($admin_user); $this->drupalPlaceBlock('facet_block:localgov_directories_facets', []); - $this->drupalLogout($admin_user); + $this->drupalLogout(); // Set up facet types. $facet_types = [ diff --git a/tests/src/Unit/FacetPreprocessTest.php b/tests/src/Unit/FacetPreprocessTest.php index 1ab8834..e006c2a 100644 --- a/tests/src/Unit/FacetPreprocessTest.php +++ b/tests/src/Unit/FacetPreprocessTest.php @@ -65,8 +65,8 @@ public function testFacetTypeSorting() { * * @see DirectoryExtraFieldDisplay::__construct() */ - public function setup(): void { - parent::setup(); + public function setUp(): void { + parent::setUp(); // Facet items. $facet_zero = $this->createMock(LocalgovDirectoriesFacets::class); From 0ebecfbda70ed0ca3ac51d825082c29c8372825c Mon Sep 17 00:00:00 2001 From: Lee Mills Date: Sat, 12 Oct 2024 14:56:53 +0100 Subject: [PATCH 2/2] fix: strict types errors --- .../LocalGovDirectoriesQueryType.php | 62 +++++++++---------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php b/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php index 4be0b43..17f46ce 100644 --- a/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php +++ b/src/Plugin/facets/query_type/LocalGovDirectoriesQueryType.php @@ -25,41 +25,39 @@ public function execute() { // Only alter the query when there's an actual query object to alter. // @phpstan-ignore-next-line. - if (empty($query)) { - return; - } - - $operator = $this->facet->getQueryOperator(); - $field_identifier = $this->facet->getFieldIdentifier(); - $exclude = $this->facet->getExclude(); - - if ($query->getProcessingLevel() === QueryInterface::PROCESSING_FULL) { - // Set the options for the actual query. - $options = &$query->getOptions(); - $options['search_api_facets'][$field_identifier] = $this->getFacetOptions(); - } + if (!empty($query)) { + $operator = $this->facet->getQueryOperator(); + $field_identifier = $this->facet->getFieldIdentifier(); + $exclude = $this->facet->getExclude(); + + if ($query->getProcessingLevel() === QueryInterface::PROCESSING_FULL) { + // Set the options for the actual query. + $options = &$query->getOptions(); + $options['search_api_facets'][$field_identifier] = $this->getFacetOptions(); + } - // Add the filter to the query if there are active values. - $active_items = $this->facet->getActiveItems(); + // Add the filter to the query if there are active values. + $active_items = $this->facet->getActiveItems(); - if (count($active_items)) { - $bundle = NULL; - $type_storage = \Drupal::entityTypeManager() - ->getStorage('localgov_directories_facets'); - $chosen_facets = $type_storage->loadMultiple($active_items); - foreach ($chosen_facets as $directory_facet) { - $bundle[$directory_facet->bundle()][] = $directory_facet->id(); - } + if (count($active_items)) { + $bundle = NULL; + $type_storage = \Drupal::entityTypeManager() + ->getStorage('localgov_directories_facets'); + $chosen_facets = $type_storage->loadMultiple($active_items); + foreach ($chosen_facets as $directory_facet) { + $bundle[$directory_facet->bundle()][] = $directory_facet->id(); + } - if ($bundle !== NULL) { - $filter = NULL; - foreach ($bundle as $bundle_name => $group_items) { - unset($filter); - $filter = $query->createConditionGroup($operator, ['facet:' . $field_identifier . '.' . $bundle_name]); - foreach ($group_items as $value) { - $filter->addCondition($this->facet->getFieldIdentifier(), $value, $exclude ? '<>' : '='); + if ($bundle !== NULL) { + $filter = NULL; + foreach ($bundle as $bundle_name => $group_items) { + unset($filter); + $filter = $query->createConditionGroup($operator, ['facet:' . $field_identifier . '.' . $bundle_name]); + foreach ($group_items as $value) { + $filter->addCondition($this->facet->getFieldIdentifier(), $value, $exclude ? '<>' : '='); + } + $query->addConditionGroup($filter); } - $query->addConditionGroup($filter); } } } @@ -192,7 +190,7 @@ protected function getResultingFacetsWithOwnFacetGroup(string $filter_tag): arra ->execute(); $found_facets = $facets['localgov_directory_facets_filter'] ?? []; $found_facets = array_filter($found_facets, function ($item) use ($group_facet_ids) { - if (in_array(intval(trim($item['filter'], '"')), $group_facet_ids, TRUE)) { + if (in_array(intval(trim($item['filter'], '"')), array_map('intval', $group_facet_ids), TRUE)) { return TRUE; } return FALSE;