Skip to content

Commit

Permalink
USAGOV-2197 phpstan ssg postprocessing (#2201)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerida authored Feb 3, 2025
2 parents 3abd378 + b8ed2db commit 8ba84c7
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*
* @phpstan-type TaxonomyBreadcrumb array{
* nodeID?: string,
* taxonomyID?: string,
* language?: "es"|"en",
* homepageTest?: "homepage"|"not_homepage",
* basicPagesubType?: null|string,
Expand All @@ -29,6 +30,8 @@
* Taxonomy_URL_4: string,
* Taxonomy_URL_5: string,
* Taxonomy_URL_6: string,
* hasBenefitCategory?: bool,
* benefitCategories?: string
* }
*/
class TaxonomyDatalayerBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public function __construct(
) {
}

public function content() {
/**
* @return array<mixed>
*/
public function content(): array {
$date = $this->state()->get('ssg_stat_date');
$msg = $this->state()->get('ssg_stat_msg');

Expand All @@ -30,11 +33,13 @@ public function content() {
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.
/**
* This is a utility. Use in order to test what the WAF and proxies will do with wait-timeouts
* See ticket USAGOV-1927
*
* @return array<mixed>
*/
public function siteLagTest() {
public function siteLagTest(): array {

$request = $this->requestStack->getCurrentRequest();
$waitParam = $request->query->get('wait');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -45,11 +47,17 @@ public function __construct(
public readonly string $pageLanguage,
) {}

/**
* @param array<mixed> $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));
}

/**
* @return array<mixed>
*/
public function toArray(): array {
$array = [
$this->hierarchy,
Expand Down Expand Up @@ -82,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
Expand Down Expand Up @@ -158,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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand All @@ -27,9 +28,14 @@

/**
* A Drush commandfile.
*
* @phpstan-import-type TaxonomyBreadcrumb from TaxonomyDatalayerBuilder
*/
final class PublishedPagesCommands extends DrushCommands {

/**
* @var array<string> $csvHeader
*/
private array $csvHeader = [
"Hierarchy Level",
"Page Type",
Expand Down Expand Up @@ -71,7 +77,7 @@ public function __construct(
parent::__construct();
}

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'),
Expand All @@ -93,7 +99,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('<info>Publishing CSV to ' . $outfile . '</info>');

$out = fopen($outfile, 'w');
Expand All @@ -113,7 +119,7 @@ public function publishedCsv($outfile) {
fclose($out);
}

protected function saveNodeRows($out): void {
protected function saveNodeRows(mixed $out): void {
$nids = $this->entityTypeManager
->getStorage('node')
->getQuery()
Expand Down Expand Up @@ -168,7 +174,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);
Expand All @@ -187,7 +193,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()
Expand All @@ -205,7 +211,7 @@ protected function saveWizardRows($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());

Expand Down Expand Up @@ -244,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(
Expand All @@ -263,6 +269,10 @@ protected function getWizardRow(Term $wizard): PublishedPagesRow {
return PublishedPagesRow::datalayerForWizard($data, $wizard, $baseURL);
}

/**
* @param array<mixed> $data
* @return array<mixed>
*/
private function alterDatalayer(array $data): array {
// Let other modules add to the datalayer payload.
$datalayerEvent = new DatalayerAlterEvent($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -117,10 +117,10 @@ public function excludeDirectories(CollectPathsEvent $event) {
/**
* Returns per-site excluded directory paths.
*
* @return array
* @return array<mixed>
* 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)) {
Expand All @@ -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();

Expand Down Expand Up @@ -233,6 +233,9 @@ public function addAgencyIndexes(CollectPathsEvent $event): void {

}

/**
* @return string[]
*/
private function getLetters(ViewExecutable $view): array {
$view->execute();
$letters = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public function getFormId() {

/**
* {@inheritdoc}
*
* @param array<string, mixed> $form
* @return array<string, mixed>
*/
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';

Expand Down Expand Up @@ -67,8 +70,10 @@ public function buildForm(array $form, FormStateInterface $form_state) {

/**
* {@inheritdoc}
*
* @param array<string, mixed> $form
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state): void {

$errors = FALSE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;

/**
* 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<string, mixed> $build
*/
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) {
/**
* @param array<string, mixed> $attachments
*/
function usagov_ssg_postprocessing_remove_shortlink(array &$attachments): void {
if (!isset($attachments['#attached']['html_head_link'])) {
return;
}
Expand All @@ -27,7 +32,10 @@ function usagov_ssg_postprocessing_remove_shortlink(array &$attachments) {
}
}

function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments) {
/**
* @param array<string, mixed> $attachments
*/
function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments): void {
if (!isset($attachments['#attached']['html_head_link'])) {
return;
}
Expand All @@ -44,10 +52,12 @@ function usagov_ssg_postprocessing_modify_canonical_link(array &$attachments) {
}

/**
* Implements hook_page_attachments_alter.
* We just want to add / to the alternate links on the home pages.
* Implements hook_page_attachments_alter
*
* @param array<string, mixed> &$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;
}
Expand All @@ -70,29 +80,31 @@ 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';
}

/**
* Implements hook_form_alter().
* Static Site Generation form text updates.
*/
function usagov_ssg_postprocessing_form_alter(&$form, &$form_state, $form_id) {
* Implements hook_form_alter().
*
* @param array<string, mixed> $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;
$form_state->setValue(usagov_ssg_postprocessing_get_static_state_button_name(), $toggle_state ? TRUE : FALSE);
Expand Down
2 changes: 1 addition & 1 deletion web/modules/custom/usagov_wizard/src/WizardDataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
}

/**
* @param TaxonomyBreadcrumb $data
* @param array{}|TaxonomyBreadcrumb $data
* @return TaxonomyBreadcrumb
*/
public function getData(array $data): array {
Expand Down

0 comments on commit 8ba84c7

Please sign in to comment.