From c408a1bfe85b11156d1804b3d77ee0b7b53a0a9b Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 22 Aug 2024 14:23:56 +1200 Subject: [PATCH] API Strong typing for the view layer --- code/Model/VirtualPage.php | 27 +++++-------------- tests/php/Model/VirtualPageTest.php | 1 - .../php/Model/VirtualPageTest_TestDBField.php | 2 +- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/code/Model/VirtualPage.php b/code/Model/VirtualPage.php index 704c814b95..8c23de8912 100644 --- a/code/Model/VirtualPage.php +++ b/code/Model/VirtualPage.php @@ -401,11 +401,8 @@ public function CMSTreeClasses() /** * Use the target page's class name for fetching templates - as we need to take on its appearance - * - * @param string $suffix - * @return array */ - public function getViewerTemplates($suffix = '') + public function getViewerTemplates(string $suffix = ''): array { $copy = $this->CopyContentFrom(); if ($copy && $copy->exists()) { @@ -418,11 +415,8 @@ public function getViewerTemplates($suffix = '') /** * Allow attributes on the master page to pass * through to the virtual page - * - * @param string $field - * @return mixed */ - public function __get($field) + public function __get(string $field): mixed { if (parent::hasMethod($funcName = "get$field")) { return $this->$funcName(); @@ -436,7 +430,7 @@ public function __get($field) return null; } - public function getField($field) + public function getField(string $field): mixed { if ($this->isFieldVirtualised($field)) { return $this->CopyContentFrom()->getField($field); @@ -484,17 +478,13 @@ public function __call($method, $args) } } - /** - * @param string $field - * @return bool - */ - public function hasField($field) + public function hasField(string $fieldName): bool { - if (parent::hasField($field)) { + if (parent::hasField($fieldName)) { return true; } $copy = $this->CopyContentFrom(); - return $copy && $copy->exists() && $copy->hasField($field); + return $copy && $copy->exists() && $copy->hasField($fieldName); } /** @@ -513,11 +503,8 @@ public function hasMethod($method) /** * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field * on this object. - * - * @param string $field - * @return string|null */ - public function castingHelper($field, bool $useFallback = true) + public function castingHelper(string $field, bool $useFallback = true): ?string { $copy = $this->CopyContentFrom(); if ($copy && $copy->exists() && ($helper = $copy->castingHelper($field, $useFallback))) { diff --git a/tests/php/Model/VirtualPageTest.php b/tests/php/Model/VirtualPageTest.php index 9512cd35e7..63e87b19cb 100644 --- a/tests/php/Model/VirtualPageTest.php +++ b/tests/php/Model/VirtualPageTest.php @@ -29,7 +29,6 @@ class VirtualPageTest extends FunctionalTest VirtualPageTest_NotRoot::class, VirtualPageTest_PageExtension::class, VirtualPageTest_PageWithAllowedChildren::class, - VirtualPageTest_TestDBField::class, VirtualPageTest_VirtualPageSub::class, ]; diff --git a/tests/php/Model/VirtualPageTest_TestDBField.php b/tests/php/Model/VirtualPageTest_TestDBField.php index 5554567ff3..eb00b256f7 100644 --- a/tests/php/Model/VirtualPageTest_TestDBField.php +++ b/tests/php/Model/VirtualPageTest_TestDBField.php @@ -7,7 +7,7 @@ class VirtualPageTest_TestDBField extends DBVarchar implements TestOnly { - public function forTemplate() + public function forTemplate(): string { return strtoupper($this->XML() ?? ''); }