API Strong typing for the view layer (#2994)

This commit is contained in:
Guy Sartorelli 2024-08-28 10:54:32 +12:00 committed by GitHub
parent 48c64e310b
commit b40269c3c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 22 deletions

View File

@ -401,11 +401,8 @@ class VirtualPage extends Page
/** /**
* Use the target page's class name for fetching templates - as we need to take on its appearance * 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(); $copy = $this->CopyContentFrom();
if ($copy && $copy->exists()) { if ($copy && $copy->exists()) {
@ -418,11 +415,8 @@ class VirtualPage extends Page
/** /**
* Allow attributes on the master page to pass * Allow attributes on the master page to pass
* through to the virtual page * 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")) { if (parent::hasMethod($funcName = "get$field")) {
return $this->$funcName(); return $this->$funcName();
@ -436,7 +430,7 @@ class VirtualPage extends Page
return null; return null;
} }
public function getField($field) public function getField(string $field): mixed
{ {
if ($this->isFieldVirtualised($field)) { if ($this->isFieldVirtualised($field)) {
return $this->CopyContentFrom()->getField($field); return $this->CopyContentFrom()->getField($field);
@ -484,17 +478,13 @@ class VirtualPage extends Page
} }
} }
/** public function hasField(string $fieldName): bool
* @param string $field
* @return bool
*/
public function hasField($field)
{ {
if (parent::hasField($field)) { if (parent::hasField($fieldName)) {
return true; return true;
} }
$copy = $this->CopyContentFrom(); $copy = $this->CopyContentFrom();
return $copy && $copy->exists() && $copy->hasField($field); return $copy && $copy->exists() && $copy->hasField($fieldName);
} }
/** /**
@ -513,11 +503,8 @@ class VirtualPage extends Page
/** /**
* Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field * Return the "casting helper" (a piece of PHP code that when evaluated creates a casted value object) for a field
* on this object. * 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(); $copy = $this->CopyContentFrom();
if ($copy && $copy->exists() && ($helper = $copy->castingHelper($field, $useFallback))) { if ($copy && $copy->exists() && ($helper = $copy->castingHelper($field, $useFallback))) {

View File

@ -29,7 +29,6 @@ class VirtualPageTest extends FunctionalTest
VirtualPageTest_NotRoot::class, VirtualPageTest_NotRoot::class,
VirtualPageTest_PageExtension::class, VirtualPageTest_PageExtension::class,
VirtualPageTest_PageWithAllowedChildren::class, VirtualPageTest_PageWithAllowedChildren::class,
VirtualPageTest_TestDBField::class,
VirtualPageTest_VirtualPageSub::class, VirtualPageTest_VirtualPageSub::class,
]; ];

View File

@ -7,7 +7,7 @@ use SilverStripe\ORM\FieldType\DBVarchar;
class VirtualPageTest_TestDBField extends DBVarchar implements TestOnly class VirtualPageTest_TestDBField extends DBVarchar implements TestOnly
{ {
public function forTemplate() public function forTemplate(): string
{ {
return strtoupper($this->XML() ?? ''); return strtoupper($this->XML() ?? '');
} }