Skip to content

Commit

Permalink
API Strong typing for the view layer
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 23, 2024
1 parent eb400ca commit 516a934
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
27 changes: 7 additions & 20 deletions code/Model/VirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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))) {
Expand Down
1 change: 0 additions & 1 deletion tests/php/Model/VirtualPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class VirtualPageTest extends FunctionalTest
VirtualPageTest_NotRoot::class,
VirtualPageTest_PageExtension::class,
VirtualPageTest_PageWithAllowedChildren::class,
VirtualPageTest_TestDBField::class,
VirtualPageTest_VirtualPageSub::class,
];

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Model/VirtualPageTest_TestDBField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class VirtualPageTest_TestDBField extends DBVarchar implements TestOnly
{
public function forTemplate()
public function forTemplate(): string
{
return strtoupper($this->XML() ?? '');
}
Expand Down

0 comments on commit 516a934

Please sign in to comment.