From 464fe7f2f07e2c3bd3ed466cee177f1ce5dea48c Mon Sep 17 00:00:00 2001 From: Darren Oh <2293701+darrenoh@users.noreply.github.com> Date: Thu, 7 Mar 2024 12:29:23 -0500 Subject: [PATCH 1/2] Fix more errors after PHPStan update --- phpstan-baseline.neon | 4 ++-- src/Form/FormBuilder.php | 1 + src/Theme/HookPreprocess.php | 2 +- src/Theme/Registry.php | 2 ++ src/functions/common.php | 8 +++++++- src/functions/form.php | 6 +++++- tests/src/Integration/Database/DrupalWriteRecordTest.php | 2 ++ 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2e417066..f79d1693 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -531,12 +531,12 @@ parameters: path: src/Routing/HookMenuRoutes.php - - message: "#^Cannot access offset '\\#title' on mixed\\.$#" + message: "#^Cannot access offset '\\#id' on mixed\\.$#" count: 1 path: src/Theme/HookPreprocess.php - - message: "#^Offset '\\#id' might not exist on array\\\\|string\\.$#" + message: "#^Cannot access offset '\\#title' on mixed\\.$#" count: 1 path: src/Theme/HookPreprocess.php diff --git a/src/Form/FormBuilder.php b/src/Form/FormBuilder.php index 7c6c1db9..49ef7788 100644 --- a/src/Form/FormBuilder.php +++ b/src/Form/FormBuilder.php @@ -12,6 +12,7 @@ class FormBuilder extends CoreFormBuilder { /** * @param FormStateInterface|mixed[] $form_state + * @param-out FormStateInterface $form_state * @return mixed[] */ public function buildForm(mixed $form_arg, FormStateInterface|array &$form_state): array diff --git a/src/Theme/HookPreprocess.php b/src/Theme/HookPreprocess.php index 4695687a..53d2d925 100644 --- a/src/Theme/HookPreprocess.php +++ b/src/Theme/HookPreprocess.php @@ -11,7 +11,7 @@ use Retrofit\Drupal\Entity\WrappedConfigEntity; /** - * @phpstan-type Variables array> + * @phpstan-type Variables array * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps */ final class HookPreprocess diff --git a/src/Theme/Registry.php b/src/Theme/Registry.php index 94ad7606..b5447c4a 100644 --- a/src/Theme/Registry.php +++ b/src/Theme/Registry.php @@ -20,6 +20,7 @@ final class Registry extends CoreRegistry { /** * @param RegistryData $cache + * @param-out array $cache */ protected function processExtension(array &$cache, $name, $type, $theme, $path): void { @@ -72,6 +73,7 @@ protected function processExtension(array &$cache, $name, $type, $theme, $path): /** * @param array $cache + * @param-out array $cache */ protected function postProcessExtension(array &$cache, ActiveTheme $theme): void { diff --git a/src/functions/common.php b/src/functions/common.php index 956d6df6..c14c5399 100644 --- a/src/functions/common.php +++ b/src/functions/common.php @@ -9,7 +9,6 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; use Drupal\Core\Asset\LibraryDiscoveryInterface; -use Drupal\Core\Database\Query\Merge; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\RevisionableInterface; @@ -131,6 +130,7 @@ function entity_language(string $entity_type, EntityInterface $entity): ?string /** * @param array $elements + * @param-out array $elements * @return array */ function element_children(array &$elements, bool $sort = false): array @@ -147,6 +147,7 @@ function drupal_get_path(string $type, string $name): string /** * @param array $element + * @param-out array $element * @param string[]|null $children_keys */ function drupal_render_children(array &$element, array $children_keys = null): string @@ -165,6 +166,7 @@ function drupal_render_children(array &$element, array $children_keys = null): s /** * @param array $elements + * @param-out array $elements * @phpstan-ignore-next-line */ function drupal_render(array &$elements): MarkupInterface|string @@ -348,6 +350,7 @@ function drupal_clean_css_identifier(string $identifier, array $filter = [ /** * @param array $array + * @param-out array $array * @param array $parents */ function &drupal_array_get_nested_value(array &$array, array $parents, ?bool &$key_exists = null): mixed @@ -357,6 +360,7 @@ function &drupal_array_get_nested_value(array &$array, array $parents, ?bool &$k /** * @param array $array + * @param-out array $array * @param array $parents */ function drupal_array_set_nested_value(array &$array, array $parents, mixed $value, bool $force = false): void @@ -402,6 +406,7 @@ function drupal_get_query_array(string $query): array function drupal_goto(string $path = '', array $options = [], int $http_response_code = 302): void { \Drupal::moduleHandler()->alter('drupal_goto', $path, $options, $http_response_code); + assert(is_string($path) && is_array($options) && is_int($http_response_code)); $url = \Drupal::pathValidator()->getUrlIfValidWithoutAccessCheck($path); if ($url !== false) { $url->mergeOptions($options); @@ -426,6 +431,7 @@ function drupal_json_encode(mixed $var): string /** * @param array|object $record + * @param-out array|object $record * @param string[] $primary_keys */ function drupal_write_record(string $table, array|object &$record, array|string $primary_keys = []): int|false diff --git a/src/functions/form.php b/src/functions/form.php index 2af7a847..6dd6d643 100644 --- a/src/functions/form.php +++ b/src/functions/form.php @@ -4,16 +4,20 @@ use Drupal\Core\Form\FormStateInterface; use Retrofit\Drupal\Form\DrupalGetForm; +use Retrofit\Drupal\Form\FormBuilder; /** * @param mixed[] $form_state + * @param-out FormStateInterface $form_state * @return mixed[] */ function drupal_build_form(string $form_id, array &$form_state): array { $form_object = \Drupal::classResolver(DrupalGetForm::class); $form_object->setFormId($form_id); - return \Drupal::formBuilder()->buildForm($form_object, $form_state); + $form_builder = \Drupal::formBuilder(); + assert($form_builder instanceof FormBuilder); + return $form_builder->buildForm($form_object, $form_state); } function drupal_form_submit(string $form_id, FormStateInterface $form_state): void diff --git a/tests/src/Integration/Database/DrupalWriteRecordTest.php b/tests/src/Integration/Database/DrupalWriteRecordTest.php index bf0947e0..14da1142 100644 --- a/tests/src/Integration/Database/DrupalWriteRecordTest.php +++ b/tests/src/Integration/Database/DrupalWriteRecordTest.php @@ -30,11 +30,13 @@ public function testWritingRecord(): void 'timestamp' => time(), ]; $result = drupal_write_record('watchdog', $record); + assert(is_array($record)); self::assertEquals(1, $result); self::assertArrayHasKey('wid', $record); self::assertEquals(1, $record['wid']); $record['message'] = 'test2'; $result = drupal_write_record('watchdog', $record, ['wid']); + assert(is_array($record)); self::assertEquals(2, $result); self::assertArrayHasKey('wid', $record); self::assertEquals(1, $record['wid']); From fbe835e513b74cad045dcc46e2e710868874cd9d Mon Sep 17 00:00:00 2001 From: Darren Oh <2293701+darrenoh@users.noreply.github.com> Date: Wed, 6 Dec 2023 22:39:47 -0500 Subject: [PATCH 2/2] Add function node_types_rebuild() --- src/functions/node.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/functions/node.php b/src/functions/node.php index 388932b6..ca5cae03 100644 --- a/src/functions/node.php +++ b/src/functions/node.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Drupal\Core\Cache\Cache; +use Drupal\Core\Config\StorageCacheInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\node\Entity\NodeType; use Drupal\node\NodeTypeInterface; @@ -72,3 +74,12 @@ function node_type_delete(string $type): void $nodeType = NodeType::load($type); $nodeType?->delete(); } + +function node_types_rebuild(): void +{ + $tags = \Drupal::config('node_type')->getCacheTags(); + $tags[] = 'config:node_type_list'; + Cache::invalidateTags($tags); + $storage = \Drupal::service('config.storage'); + $storage->resetListCache(); +}