From 516a934d2f440e33d1c82f27d8d873a7cf7a5bce 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 a65cd1f275..25e7ac5267 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 2185ef1764..3284121cf0 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() ?? ''); }