From bc82474d9b2417cfe968204082a23ddafa472a39 Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:00:49 -0500 Subject: [PATCH 01/16] USAGOV-2197-phpstan-ssg-postprocessing: First attempt at fixing PhpStan errors --- .../src/Controller/SsgStatController.php | 4 ++-- .../src/Data/PublishedPagesRow.php | 3 ++- .../Drush/Commands/PublishedPagesCommands.php | 15 +++++++------ .../EventSubscriber/PagerPathSubscriber.php | 4 ++-- .../EventSubscriber/TomeEventSubscriber.php | 8 +++---- .../usagov_ssg_postprocessing.module | 22 ++++++++++++------- 6 files changed, 32 insertions(+), 24 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php index 720a5ddc14..4025c17ab8 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php @@ -15,7 +15,7 @@ public function __construct( ) { } - public function content() { + public function content(): array { $date = $this->state()->get('ssg_stat_date'); $msg = $this->state()->get('ssg_stat_msg'); @@ -34,7 +34,7 @@ public function content() { * This is a utility use in order to test what the WAF and proxies will do with wait-timeouts. * See ticket USAGOV-1927. */ - public function siteLagTest() { + public function siteLagTest(): array { $request = $this->requestStack->getCurrentRequest(); $waitParam = $request->query->get('wait'); diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php index 66cea87b69..c8ca7e19fa 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php @@ -45,7 +45,8 @@ public function __construct( public readonly string $pageLanguage, ) {} - private static function getHierarchy(array $data): int { + # see https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type + private static function getHierarchy(array $data): int { $texts = array_filter($data, fn($key) => str_starts_with($key, 'Taxonomy_URL_'), ARRAY_FILTER_USE_KEY); return count(array_unique($texts)); } diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index b3fa6a3e18..25acaa13e5 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -5,6 +5,7 @@ use Drupal\Core\Breadcrumb\ChainBreadcrumbBuilderInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatchInterface; @@ -30,7 +31,7 @@ */ final class PublishedPagesCommands extends DrushCommands { - private array $csvHeader = [ + private array $csvHeader = [ "Hierarchy Level", "Page Type", "Page Path", @@ -71,7 +72,7 @@ public function __construct( parent::__construct(); } - public static function create(ContainerInterface $container) { + public static function create(ContainerInterface $container): LanguageManager { return new static( entityTypeManager: $container->get('entity_type.manager'), configFactory: $container->get('config.factory'), @@ -93,7 +94,7 @@ public static function create(ContainerInterface $container) { name: 'usagov_ssg_postprocessing:published-csv', description: 'Usage description') ] - public function publishedCsv($outfile) { + public function publishedCsv(mixed $outfile): void { $this->output()->writeln('Publishing CSV to ' . $outfile . ''); $out = fopen($outfile, 'w'); @@ -113,7 +114,7 @@ public function publishedCsv($outfile) { fclose($out); } - protected function saveNodeRows($out): void { + protected function saveNodeRows(mixed $out): void { $nids = $this->entityTypeManager ->getStorage('node') ->getQuery() @@ -168,7 +169,7 @@ protected function saveNodeRows($out): void { } } - protected function saveNodeRow($out, $node, $row): void { + protected function saveNodeRow(mixed $out, Node $node, mixed $row): void { $row = array_map(fn($col) => trim($col), $row); fputcsv($out, $row); @@ -187,7 +188,7 @@ protected function saveNodeRow($out, $node, $row): void { } } - protected function saveWizardRows($out): void { + protected function saveWizardRows(mixed $out): void { $tids = $this->entityTypeManager ->getStorage('taxonomy_term') ->getQuery() @@ -263,7 +264,7 @@ protected function getWizardRow(Term $wizard): PublishedPagesRow { return PublishedPagesRow::datalayerForWizard($data, $wizard, $baseURL); } - private function alterDatalayer(array $data): array { + private function alterDatalayer(array $data): array { // Let other modules add to the datalayer payload. $datalayerEvent = new DatalayerAlterEvent($data); $this->dispatcher->dispatch($datalayerEvent, DatalayerAlterEvent::EVENT_NAME); diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/PagerPathSubscriber.php b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/PagerPathSubscriber.php index 6f563fde05..108da4b577 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/PagerPathSubscriber.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/PagerPathSubscriber.php @@ -21,7 +21,7 @@ class PagerPathSubscriber implements EventSubscriberInterface { * @param \Drupal\tome_static\Event\ModifyDestinationEvent $event * The event. */ - public function modifyDestination(ModifyDestinationEvent $event) { + public function modifyDestination(ModifyDestinationEvent $event): void { $destination = $event->getDestination(); $new_destination = $this->modifyUrl($destination); if ($destination != $new_destination) { @@ -35,7 +35,7 @@ public function modifyDestination(ModifyDestinationEvent $event) { * @param \Drupal\tome_static\Event\ModifyHtmlEvent $event * The event. */ - public function modifyHtml(ModifyHtmlEvent $event) { + public function modifyHtml(ModifyHtmlEvent $event): void { $html = $event->getHtml(); $path = $event->getPath(); diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php index f451369f10..66b9f20805 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php @@ -63,7 +63,7 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Lan * @param \Drupal\tome_static\Event\CollectPathsEvent $event * The collect paths event. */ - public function excludeDirectories(CollectPathsEvent $event) { + public function excludeDirectories(CollectPathsEvent $event): void { $excluded_directories = self::getExcludedDirectories(); $paths = $event->getPaths(TRUE); foreach ($paths as $path => $metadata) { @@ -120,7 +120,7 @@ public function excludeDirectories(CollectPathsEvent $event) { * @return array * An array of excluded paths. */ - public static function getExcludedDirectories() { + public static function getExcludedDirectories(): array { $excluded_paths = []; $site_paths = Settings::get('usagov_tome_static_path_exclude_directories', []); if (is_array($site_paths)) { @@ -138,7 +138,7 @@ public static function getExcludedDirectories() { * @param \Drupal\tome_static\Event\ModifyHtmlEvent $event * The event. */ - public function modifyHtml(ModifyHtmlEvent $event) { + public function modifyHtml(ModifyHtmlEvent $event): void { $html = $event->getHtml(); $html5 = new HTML5(); @@ -233,7 +233,7 @@ public function addAgencyIndexes(CollectPathsEvent $event): void { } - private function getLetters(ViewExecutable $view): array { + private function getLetters(ViewExecutable $view): array { $view->execute(); $letters = []; foreach ($view->result as $result) { diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index be67cd3f2b..56c71f7762 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -2,20 +2,24 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Form\FormBase; +use Drupal\Core\Form\FormState; + /** * Implements hook_entity_view_alter(). * + * @param array &$build * From Drupal.org -> "If a module wishes to act on the rendered HTML of the * ntity rather than the structured content array, it may use this hook to add * a #post_render callback." */ -function usagov_ssg_postprocessing_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { +function usagov_ssg_postprocessing_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void { usagov_ssg_postprocessing_remove_shortlink($build); usagov_ssg_postprocessing_modify_canonical_link($build); } -function usagov_ssg_postprocessing_remove_shortlink(array &$attachments) { +function usagov_ssg_postprocessing_remove_shortlink(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { return; } @@ -27,7 +31,7 @@ function usagov_ssg_postprocessing_remove_shortlink(array &$attachments) { } } -function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments) { +function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { return; } @@ -45,9 +49,11 @@ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments) { /** * Implements hook_page_attachments_alter. + * + * @param array &$attachments * We just want to add / to the alternate links on the home pages. */ -function usagov_ssg_postprocessing_page_attachments_alter(array &$attachments) { +function usagov_ssg_postprocessing_page_attachments_alter(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { return; } @@ -70,21 +76,21 @@ function usagov_ssg_postprocessing_page_attachments_alter(array &$attachments) { /** * Static Site Generation Toggle State Form ID */ -function usagov_ssg_postprocessing_get_static_state_form_id() { +function usagov_ssg_postprocessing_get_static_state_form_id(): string { return 'toggle_static_site_generation_form'; } /** * Static Site Generation Toggle State Form Button Name */ -function usagov_ssg_postprocessing_get_static_state_button_name() { +function usagov_ssg_postprocessing_get_static_state_button_name(): string { return 'confirm_toggle'; } /** * Toggle State variable name for sset/sget/sdel */ -function usagov_ssg_postprocessing_get_static_state_var() { +function usagov_ssg_postprocessing_get_static_state_var(): string { return 'usagov.tome_run_disabled'; } @@ -92,7 +98,7 @@ function usagov_ssg_postprocessing_get_static_state_var() { * Implements hook_form_alter(). * Static Site Generation form text updates. */ -function usagov_ssg_postprocessing_form_alter(&$form, &$form_state, $form_id) { +function usagov_ssg_postprocessing_form_alter(FormBase &$form, FormState &$form_state, string $form_id): void { if ($form_id == usagov_ssg_postprocessing_get_static_state_form_id()) { $toggle_state = \Drupal::state()->get(usagov_ssg_postprocessing_get_static_state_var()) ? 1 : 0; $form_state->setValue(usagov_ssg_postprocessing_get_static_state_button_name(), $toggle_state ? TRUE : FALSE); From 6c307ea035b086155165e6a726b3cd3d36a167b6 Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:02:41 -0500 Subject: [PATCH 02/16] USAGOV-2197-phpstan-ssg-postprocessing: Allow CCI build --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index eac7fa38be..65f7779767 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -765,6 +765,7 @@ workflows: - dev - stage - prod + - USAGOV-2197-phpstan-ssg-postprocessing - build-and-push-container: requires: - approve-build-and-push-container @@ -775,6 +776,7 @@ workflows: - dev - stage - prod + - USAGOV-2197-phpstan-ssg-postprocessing - approve-dev-deployment: type: approval requires: From fb2a54f4d32b383fd36c897525a4222a1bce5f19 Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:19:00 -0500 Subject: [PATCH 03/16] USAGOV-2197-phpstan-ssg-postprocessing: Fix syntax errors --- .../src/Controller/SsgStatController.php | 9 +++++++-- .../src/Data/PublishedPagesRow.php | 6 ++++-- .../src/Drush/Commands/PublishedPagesCommands.php | 11 +++++++++-- .../src/EventSubscriber/TomeEventSubscriber.php | 9 ++++++--- .../usagov_ssg_postprocessing.module | 5 +++-- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php index 4025c17ab8..b2e72b2c7b 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php @@ -15,7 +15,10 @@ public function __construct( ) { } - public function content(): array { + /** + * @return array + */ + public function content(): array { $date = $this->state()->get('ssg_stat_date'); $msg = $this->state()->get('ssg_stat_msg'); @@ -30,9 +33,11 @@ public function content(): array { return ['#markup' => $markup, '#cache' => ['max-age' => 0]]; } - /* + /** * This is a utility use in order to test what the WAF and proxies will do with wait-timeouts. * See ticket USAGOV-1927. + * + * @param array $data */ public function siteLagTest(): array { diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php index c8ca7e19fa..c76f31ab69 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php @@ -45,8 +45,10 @@ public function __construct( public readonly string $pageLanguage, ) {} - # see https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type - private static function getHierarchy(array $data): int { + /** + * @param array $data + */ + private static function getHierarchy(array $data): int { $texts = array_filter($data, fn($key) => str_starts_with($key, 'Taxonomy_URL_'), ARRAY_FILTER_USE_KEY); return count(array_unique($texts)); } diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index 25acaa13e5..1c465460fb 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -31,7 +31,10 @@ */ final class PublishedPagesCommands extends DrushCommands { - private array $csvHeader = [ + /** + * @var array $csvHeader + */ + private array $csvHeader = [ "Hierarchy Level", "Page Type", "Page Path", @@ -264,7 +267,11 @@ protected function getWizardRow(Term $wizard): PublishedPagesRow { return PublishedPagesRow::datalayerForWizard($data, $wizard, $baseURL); } - private function alterDatalayer(array $data): array { + /** + * @param array $data + * @return array + */ + private function alterDatalayer(array $data): array { // Let other modules add to the datalayer payload. $datalayerEvent = new DatalayerAlterEvent($data); $this->dispatcher->dispatch($datalayerEvent, DatalayerAlterEvent::EVENT_NAME); diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php index 66b9f20805..7b4c7d0f28 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php @@ -117,10 +117,10 @@ public function excludeDirectories(CollectPathsEvent $event): void { /** * Returns per-site excluded directory paths. * - * @return array + * @return array * An array of excluded paths. */ - public static function getExcludedDirectories(): array { + public static function getExcludedDirectories(): array { $excluded_paths = []; $site_paths = Settings::get('usagov_tome_static_path_exclude_directories', []); if (is_array($site_paths)) { @@ -233,7 +233,10 @@ public function addAgencyIndexes(CollectPathsEvent $event): void { } - private function getLetters(ViewExecutable $view): array { + /** + * return array + */ + private function getLetters(ViewExecutable $view): array { $view->execute(); $letters = []; foreach ($view->result as $result) { diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index 56c71f7762..02a45e146a 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -8,11 +8,12 @@ use Drupal\Core\Form\FormState; /** * Implements hook_entity_view_alter(). - * - * @param array &$build + * * From Drupal.org -> "If a module wishes to act on the rendered HTML of the * ntity rather than the structured content array, it may use this hook to add * a #post_render callback." + * + * @param array &$build */ function usagov_ssg_postprocessing_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void { usagov_ssg_postprocessing_remove_shortlink($build); From 1bf338c53ab982c65449ebf8c4986071f0674205 Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:23:13 -0500 Subject: [PATCH 04/16] USAGOV-2197-phpstan-ssg-postprocessing: Fix syntax error --- .../src/Controller/SsgStatController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php index b2e72b2c7b..67ab3d14b2 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php @@ -39,7 +39,7 @@ public function content(): array { * * @param array $data */ - public function siteLagTest(): array { + public function siteLagTest(): array { $request = $this->requestStack->getCurrentRequest(); $waitParam = $request->query->get('wait'); From a8f4fe1fed87e2f6d27fc0f86feb3a3406ff2f4d Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:26:53 -0500 Subject: [PATCH 05/16] USAGOV-2197-phpstan-ssg-postprocessing: Fixed more syntax errors --- .../src/Drush/Commands/PublishedPagesCommands.php | 2 +- .../src/EventSubscriber/TomeEventSubscriber.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index 1c465460fb..37ada953cf 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -271,7 +271,7 @@ protected function getWizardRow(Term $wizard): PublishedPagesRow { * @param array $data * @return array */ - private function alterDatalayer(array $data): array { + private function alterDatalayer(array $data): array { // Let other modules add to the datalayer payload. $datalayerEvent = new DatalayerAlterEvent($data); $this->dispatcher->dispatch($datalayerEvent, DatalayerAlterEvent::EVENT_NAME); diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php index 7b4c7d0f28..5c8419ce80 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php @@ -234,7 +234,7 @@ public function addAgencyIndexes(CollectPathsEvent $event): void { } /** - * return array + * @return array */ private function getLetters(ViewExecutable $view): array { $view->execute(); From ddedc515163486a0ee230a2354e2abd79550ba5b Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:35:24 -0500 Subject: [PATCH 06/16] USAGOV-2197-phpstan-ssg-postprocessing: Syntax Errors --- .../src/Controller/SsgStatController.php | 6 +++--- .../usagov_ssg_postprocessing.module | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php index 67ab3d14b2..70ff7d78cc 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php @@ -34,9 +34,9 @@ public function content(): array { } /** - * This is a utility use in order to test what the WAF and proxies will do with wait-timeouts. - * See ticket USAGOV-1927. - * + * This is a utility use in order to test what the WAF and proxies will do with wait-timeouts + * See ticket USAGOV-1927 + * * @param array $data */ public function siteLagTest(): array { diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index 02a45e146a..f920dff99b 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -5,14 +5,13 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormState; - /** - * Implements hook_entity_view_alter(). - * + * Implements hook_entity_view_alter() + * * From Drupal.org -> "If a module wishes to act on the rendered HTML of the * ntity rather than the structured content array, it may use this hook to add * a #post_render callback." - * + * * @param array &$build */ function usagov_ssg_postprocessing_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void { @@ -49,10 +48,10 @@ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): v } /** - * Implements hook_page_attachments_alter. + * Implements hook_page_attachments_alter * * @param array &$attachments - * We just want to add / to the alternate links on the home pages. + * We just want to add / to the alternate links on the home pages */ function usagov_ssg_postprocessing_page_attachments_alter(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { From b95c88c77481abded79f2a2af0703f0dd780159e Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 12:40:03 -0500 Subject: [PATCH 07/16] USAGOV-2197-phpstan-ssg-postprocessing: Drupal syntax errors --- .../src/Data/PublishedPagesRow.php | 6 +++--- .../usagov_ssg_postprocessing.module | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php index c76f31ab69..2a17f713ef 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php @@ -45,9 +45,9 @@ public function __construct( public readonly string $pageLanguage, ) {} - /** - * @param array $data - */ + /** + * @param array $data + */ private static function getHierarchy(array $data): int { $texts = array_filter($data, fn($key) => str_starts_with($key, 'Taxonomy_URL_'), ARRAY_FILTER_USE_KEY); return count(array_unique($texts)); diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index f920dff99b..aeb43022fa 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -49,7 +49,7 @@ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): v /** * Implements hook_page_attachments_alter - * + * * @param array &$attachments * We just want to add / to the alternate links on the home pages */ From 2c31a4aea2a60e20b6c2d841b62834edd7922e00 Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 13:48:47 -0500 Subject: [PATCH 08/16] USAGOV-2197-phpstan-ssg-postprocessing: if FormBase is given as the argument type, we receive an error from the core, upstream, that an array has been passed. --- .../usagov_ssg_postprocessing/usagov_ssg_postprocessing.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index aeb43022fa..7468317843 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -98,7 +98,7 @@ function usagov_ssg_postprocessing_get_static_state_var(): string { * Implements hook_form_alter(). * Static Site Generation form text updates. */ -function usagov_ssg_postprocessing_form_alter(FormBase &$form, FormState &$form_state, string $form_id): void { +function usagov_ssg_postprocessing_form_alter(mixed &$form, mixed &$form_state, string $form_id): void { if ($form_id == usagov_ssg_postprocessing_get_static_state_form_id()) { $toggle_state = \Drupal::state()->get(usagov_ssg_postprocessing_get_static_state_var()) ? 1 : 0; $form_state->setValue(usagov_ssg_postprocessing_get_static_state_button_name(), $toggle_state ? TRUE : FALSE); From d6b53a09f31a3675bd50bdc0c5673a9538105d2c Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 27 Jan 2025 20:21:38 -0500 Subject: [PATCH 09/16] USAGOV-2197-phpstan-ssg-postprocessing: Specifying LanguageManager as the return type caused a runtime error, leading to the Drush Command not being available. Changed return type to mixed, alleviating the runtime error. --- .../src/Drush/Commands/PublishedPagesCommands.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index 37ada953cf..24a03f40f4 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -75,7 +75,10 @@ public function __construct( parent::__construct(); } - public static function create(ContainerInterface $container): LanguageManager { + /** + * @return mixed + */ + public static function create(ContainerInterface $container) { return new static( entityTypeManager: $container->get('entity_type.manager'), configFactory: $container->get('config.factory'), From 600c54765eff5690f418754df4dc822531439921 Mon Sep 17 00:00:00 2001 From: arpage Date: Wed, 29 Jan 2025 13:05:18 -0500 Subject: [PATCH 10/16] USAGOV-2197-phpstan-ssg-postprocessing: Change mixed to static, fix incorrect docblock comment. --- .../src/Controller/SsgStatController.php | 4 ++-- .../src/Drush/Commands/PublishedPagesCommands.php | 5 +---- .../usagov_ssg_postprocessing.module | 2 -- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php index 70ff7d78cc..fb68958a13 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Controller/SsgStatController.php @@ -34,10 +34,10 @@ public function content(): array { } /** - * This is a utility use in order to test what the WAF and proxies will do with wait-timeouts + * This is a utility. Use in order to test what the WAF and proxies will do with wait-timeouts * See ticket USAGOV-1927 * - * @param array $data + * @return array */ public function siteLagTest(): array { diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index 24a03f40f4..2501bd23e4 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -75,10 +75,7 @@ public function __construct( parent::__construct(); } - /** - * @return mixed - */ - public static function create(ContainerInterface $container) { + public static function create(ContainerInterface $container): static { return new static( entityTypeManager: $container->get('entity_type.manager'), configFactory: $container->get('config.factory'), diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index 7468317843..bf1d38723e 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -2,8 +2,6 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Form\FormBase; -use Drupal\Core\Form\FormState; /** * Implements hook_entity_view_alter() From 8cbfff6ceb330f97af9ed2be292efbeffa63dd03 Mon Sep 17 00:00:00 2001 From: arpage Date: Wed, 29 Jan 2025 13:53:12 -0500 Subject: [PATCH 11/16] USAGOV-2197-phpstan-ssg-postprocessing: Remove CCI branch exception --- .circleci/config.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 65f7779767..eac7fa38be 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -765,7 +765,6 @@ workflows: - dev - stage - prod - - USAGOV-2197-phpstan-ssg-postprocessing - build-and-push-container: requires: - approve-build-and-push-container @@ -776,7 +775,6 @@ workflows: - dev - stage - prod - - USAGOV-2197-phpstan-ssg-postprocessing - approve-dev-deployment: type: approval requires: From 46af97138a2bc63bfe59931db9c813db571b939e Mon Sep 17 00:00:00 2001 From: arpage Date: Sun, 2 Feb 2025 15:46:22 -0500 Subject: [PATCH 12/16] USAGOV-2197-phpstan-ssg-postprocessing: Update typehints per code review --- .../EventSubscriber/TomeEventSubscriber.php | 2 +- .../usagov_ssg_postprocessing.module | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php index 5c8419ce80..ee6632a8ba 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/EventSubscriber/TomeEventSubscriber.php @@ -234,7 +234,7 @@ public function addAgencyIndexes(CollectPathsEvent $event): void { } /** - * @return array + * @return string[] */ private function getLetters(ViewExecutable $view): array { $view->execute(); diff --git a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module index bf1d38723e..148cd7ea58 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module +++ b/web/modules/custom/usagov_ssg_postprocessing/usagov_ssg_postprocessing.module @@ -10,13 +10,16 @@ use Drupal\Core\Entity\EntityInterface; * ntity rather than the structured content array, it may use this hook to add * a #post_render callback." * - * @param array &$build + * @param array $build */ function usagov_ssg_postprocessing_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void { usagov_ssg_postprocessing_remove_shortlink($build); usagov_ssg_postprocessing_modify_canonical_link($build); } +/** + * @param array $attachments + */ function usagov_ssg_postprocessing_remove_shortlink(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { return; @@ -29,6 +32,9 @@ function usagov_ssg_postprocessing_remove_shortlink(array &$attachments): void { } } +/** + * @param array $attachments + */ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): void { if (!isset($attachments['#attached']['html_head_link'])) { return; @@ -48,7 +54,7 @@ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): v /** * Implements hook_page_attachments_alter * - * @param array &$attachments + * @param array &$attachments * We just want to add / to the alternate links on the home pages */ function usagov_ssg_postprocessing_page_attachments_alter(array &$attachments): void { @@ -93,9 +99,11 @@ function usagov_ssg_postprocessing_get_static_state_var(): string { } /** - * Implements hook_form_alter(). - * Static Site Generation form text updates. - */ + * Implements hook_form_alter(). + * + * @param array $form + * Static Site Generation form text updates. + */ function usagov_ssg_postprocessing_form_alter(mixed &$form, mixed &$form_state, string $form_id): void { if ($form_id == usagov_ssg_postprocessing_get_static_state_form_id()) { $toggle_state = \Drupal::state()->get(usagov_ssg_postprocessing_get_static_state_var()) ? 1 : 0; From 16877201ac74fa32e4f17910c56fa941ea3d77a0 Mon Sep 17 00:00:00 2001 From: arpage Date: Sun, 2 Feb 2025 15:48:34 -0500 Subject: [PATCH 13/16] USAGOV-2197-phpstan-ssg-postprocessing: Testing build via CircleCI --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index eac7fa38be..22fa70881b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -765,6 +765,7 @@ workflows: - dev - stage - prod + - USAGOV-2197-phpstan-ssg-postprocessing - build-and-push-container: requires: - approve-build-and-push-container @@ -775,6 +776,7 @@ workflows: - dev - stage - prod + -USAGOV-2197-phpstan-ssg-postprocessing - approve-dev-deployment: type: approval requires: From 160a3a84a68a8e2c7bb0ef83077c0f2d24a193ac Mon Sep 17 00:00:00 2001 From: arpage Date: Sun, 2 Feb 2025 15:49:27 -0500 Subject: [PATCH 14/16] USAGOV-2197-phpstan-ssg-postprocessing: fix yaml syntax error --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 22fa70881b..65f7779767 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -776,7 +776,7 @@ workflows: - dev - stage - prod - -USAGOV-2197-phpstan-ssg-postprocessing + - USAGOV-2197-phpstan-ssg-postprocessing - approve-dev-deployment: type: approval requires: From 75be8ac7f083df23c5a7d5ed3782a0f74d0f715e Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Mon, 3 Feb 2025 11:19:43 -0500 Subject: [PATCH 15/16] USAGOV-2197-phpstan-ssg-postprocessing: Adding typehint for Taxonomy Breadcrumb coming from PHPStan updates to usagov_twig_vars module --- .../src/TaxonomyDatalayerBuilder.php | 3 +++ .../src/Data/PublishedPagesRow.php | 15 +++++++++++++-- .../src/Drush/Commands/PublishedPagesCommands.php | 8 +++++--- .../src/Form/ToggleStaticSiteGenerationForm.php | 9 +++++++-- .../custom/usagov_wizard/src/WizardDataLayer.php | 2 +- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php index 22c7ba6311..0ac21f5e2d 100644 --- a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php +++ b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php @@ -12,6 +12,7 @@ * * @phpstan-type TaxonomyBreadcrumb array{ * nodeID?: string, + * taxonomyID?: string, * language?: "es"|"en", * homepageTest?: "homepage"|"not_homepage", * basicPagesubType?: null|string, @@ -29,6 +30,8 @@ * Taxonomy_URL_4: string, * Taxonomy_URL_5: string, * Taxonomy_URL_6: string, + * hasBenefitCategory?: bool, + * benefitCategories?: string * } */ class TaxonomyDatalayerBuilder { diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php index 2a17f713ef..f9f9ac4dca 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Data/PublishedPagesRow.php @@ -4,12 +4,14 @@ use Drupal\Core\Language\Language; use Drupal\Core\Url; -use Drupal\node\Entity\Node; +use Drupal\node\NodeInterface; use Drupal\taxonomy\Entity\Term; use Drupal\usa_twig_vars\TaxonomyDatalayerBuilder; /** * Data structure describing the columns of the Published Pages CSV + * + * @phpstan-import-type TaxonomyBreadcrumb from TaxonomyDatalayerBuilder */ final class PublishedPagesRow { @@ -53,6 +55,9 @@ private static function getHierarchy(array $data): int { return count(array_unique($texts)); } + /** + * @return array + */ public function toArray(): array { $array = [ $this->hierarchy, @@ -85,7 +90,10 @@ public function toArray(): array { return $array; } - public static function datalayerForNode(array $data, Node $node, string $baseURL): self { + /** + * @param TaxonomyBreadcrumb $data + */ + public static function datalayerForNode(array $data, NodeInterface $node, string $baseURL): self { $title = $node->getTitle(); // Federal Agency nodes tack on the acronym because the original implementation @@ -161,6 +169,9 @@ public static function datalayerForNode(array $data, Node $node, string $baseURL ); } + /** + * @param TaxonomyBreadcrumb $data + */ public static function datalayerForWizard(array $data, Term $term, string $baseURL): self { if ($heading = $term->get('field_heading')->getValue()) { $title = $heading[0]['value']; diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php index 2501bd23e4..37f486fa60 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Drush/Commands/PublishedPagesCommands.php @@ -5,12 +5,12 @@ use Drupal\Core\Breadcrumb\ChainBreadcrumbBuilderInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\Router; use Drupal\node\Entity\Node; +use Drupal\node\NodeInterface; use Drupal\path_alias\AliasManagerInterface; use Drupal\taxonomy\Entity\Term; use Drupal\usa_twig_vars\Event\DatalayerAlterEvent; @@ -28,6 +28,8 @@ /** * A Drush commandfile. + * + * @phpstan-import-type TaxonomyBreadcrumb from TaxonomyDatalayerBuilder */ final class PublishedPagesCommands extends DrushCommands { @@ -209,7 +211,7 @@ protected function saveWizardRows(mixed $out): void { } } - protected function getNodeRow(Node $node): PublishedPagesRow { + protected function getNodeRow(NodeInterface $node): PublishedPagesRow { $front_uri = $this->configFactory->get('system.site')->get('page.front'); $alias = $this->pathAliasManager->getAliasByPath('/node/' . $node->id()); @@ -248,7 +250,7 @@ protected function getNodeRow(Node $node): PublishedPagesRow { * entity we are exporting that the datalayer module can look up via the * breadcrumb manager. */ - private function getRouteMatchForNode(Node $node): RouteMatchInterface { + private function getRouteMatchForNode(NodeInterface $node): RouteMatchInterface { $route = $this->router->match('/node/' . $node->id()); return new RouteMatch( diff --git a/web/modules/custom/usagov_ssg_postprocessing/src/Form/ToggleStaticSiteGenerationForm.php b/web/modules/custom/usagov_ssg_postprocessing/src/Form/ToggleStaticSiteGenerationForm.php index fdf5f024ed..c6b4e2f1cc 100644 --- a/web/modules/custom/usagov_ssg_postprocessing/src/Form/ToggleStaticSiteGenerationForm.php +++ b/web/modules/custom/usagov_ssg_postprocessing/src/Form/ToggleStaticSiteGenerationForm.php @@ -36,8 +36,11 @@ public function getFormId() { /** * {@inheritdoc} + * + * @param array $form + * @return array */ - public function buildForm(array $form, FormStateInterface $form_state) { + public function buildForm(array $form, FormStateInterface $form_state): array { $toggle_state = $this->state->get(usagov_ssg_postprocessing_get_static_state_var()) ? 'Enable' : 'Disable'; @@ -67,8 +70,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} + * + * @param array $form */ - public function submitForm(array &$form, FormStateInterface $form_state) { + public function submitForm(array &$form, FormStateInterface $form_state): void { $errors = FALSE; diff --git a/web/modules/custom/usagov_wizard/src/WizardDataLayer.php b/web/modules/custom/usagov_wizard/src/WizardDataLayer.php index 830f7ee83c..c7819be3ee 100644 --- a/web/modules/custom/usagov_wizard/src/WizardDataLayer.php +++ b/web/modules/custom/usagov_wizard/src/WizardDataLayer.php @@ -18,7 +18,7 @@ public function __construct( } /** - * @param TaxonomyBreadcrumb $data + * @param array{}|TaxonomyBreadcrumb $data * @return TaxonomyBreadcrumb */ public function getData(array $data): array { From 9492d53e235d0dbd95995f1ab439e0f0e79b990c Mon Sep 17 00:00:00 2001 From: arpage Date: Mon, 3 Feb 2025 11:32:35 -0500 Subject: [PATCH 16/16] USAGOV-2197-phpstan-ssg-postprocessing: Remove testing branches from cci config --- .circleci/config.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 65f7779767..eac7fa38be 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -765,7 +765,6 @@ workflows: - dev - stage - prod - - USAGOV-2197-phpstan-ssg-postprocessing - build-and-push-container: requires: - approve-build-and-push-container @@ -776,7 +775,6 @@ workflows: - dev - stage - prod - - USAGOV-2197-phpstan-ssg-postprocessing - approve-dev-deployment: type: approval requires: