Skip to content

Commit

Permalink
chore: use static as return type in AbstractBaseType
Browse files Browse the repository at this point in the history
This enabled static analysers to retrieve the correct class name.

Additionally, we prepare TypeInterface for adding return types with v4.0.0 (where missing).

Related: #129
  • Loading branch information
brotkrueml committed Feb 19, 2025
1 parent d8e23c5 commit 3f64e53
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
13 changes: 7 additions & 6 deletions Classes/Core/Model/AbstractBaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* This class provides the logic for both, a single type and a multiple type.
* It is not for use in custom extensions. Use AbstractType instead.
*
* @internal
*/
Expand All @@ -33,7 +34,7 @@ abstract class AbstractBaseType implements TypeInterface
*/
protected array $properties = [];

public function setId($id): self
public function setId($id): static
{
if (! $this->isValidDataTypeForId($id)) {
throw new \InvalidArgumentException(
Expand Down Expand Up @@ -84,7 +85,7 @@ public function hasProperty(string $propertyName): bool
return true;
}

public function getProperty(string $propertyName)
public function getProperty(string $propertyName): mixed
{
$this->checkPropertyExists($propertyName);

Expand All @@ -106,7 +107,7 @@ private function checkPropertyExists(string $propertyName): void
}
}

public function setProperty(string $propertyName, $propertyValue): self
public function setProperty(string $propertyName, $propertyValue): static
{
$propertyValue = $this->stringifyNumericValue($propertyValue);
$this->checkProperty($propertyName, $propertyValue);
Expand Down Expand Up @@ -165,7 +166,7 @@ private function isValidDataTypeForPropertyValue(mixed $propertyValue): bool
|| $propertyValue instanceof EnumerationInterface;
}

public function addProperty(string $propertyName, $propertyValue): self
public function addProperty(string $propertyName, $propertyValue): static
{
$propertyValue = $this->stringifyNumericValue($propertyValue);
$this->checkProperty($propertyName, $propertyValue);
Expand Down Expand Up @@ -194,7 +195,7 @@ public function addProperty(string $propertyName, $propertyValue): self
return $this;
}

public function setProperties(array $properties): self
public function setProperties(array $properties): static
{
foreach ($properties as $propertyName => $propertyValue) {
$this->setProperty($propertyName, $propertyValue);
Expand All @@ -203,7 +204,7 @@ public function setProperties(array $properties): self
return $this;
}

public function clearProperty(string $propertyName): self
public function clearProperty(string $propertyName): static
{
$this->checkPropertyExists($propertyName);

Expand Down
16 changes: 11 additions & 5 deletions Classes/Core/Model/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ interface TypeInterface extends NodeIdentifierInterface
* Set the id.
*
* @param NodeIdentifierInterface|string|null $id The id
* @return self
* @return static
* @todo Declare return type in method with v4.0.0
*/
public function setId($id);

Expand All @@ -33,6 +34,7 @@ public function hasProperty(string $propertyName): bool;
*
* @param string $propertyName The property name
* @return mixed
* @todo Declare return type in method with v4.0.0
*/
public function getProperty(string $propertyName);

Expand All @@ -41,7 +43,8 @@ public function getProperty(string $propertyName);
*
* @param string $propertyName The property name
* @param mixed $propertyValue The value of the property
* @return self
* @return static
* @todo Declare return type in method with v4.0.0
*/
public function setProperty(string $propertyName, mixed $propertyValue);

Expand All @@ -50,7 +53,8 @@ public function setProperty(string $propertyName, mixed $propertyValue);
*
* @param string $propertyName The property name
* @param mixed $propertyValue The property value
* @return self
* @return static
* @todo Declare return type in method with v4.0.0
*/
public function addProperty(string $propertyName, mixed $propertyValue);

Expand All @@ -62,15 +66,17 @@ public function addProperty(string $propertyName, mixed $propertyValue);
* value = property value
*
* @param array<string, mixed> $properties
* @return self
* @return static
* @todo Declare return type in method with v4.0.0
*/
public function setProperties(array $properties);

/**
* Clear a property.
*
* @param string $propertyName The property name
* @return self
* @return static
* @todo Declare return type in method with v4.0.0
*/
public function clearProperty(string $propertyName);

Expand Down
23 changes: 12 additions & 11 deletions Documentation/Developer/Api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,11 @@ class.
Available type model methods
----------------------------

The type models which extend :php:`\Brotkrueml\Schema\Core\Model\AbstractType`
expose the following methods:
The type models which implement :php:`\Brotkrueml\Schema\Core\Model\TypeInterface`
or extend :php:`\Brotkrueml\Schema\Core\Model\AbstractType` expose the following
methods:

.. confval:: setId($id)
.. confval:: setId($id): static
:name: abstracttype-setid

The method sets the unique ID of the model. With the ID, you can
Expand Down Expand Up @@ -322,7 +323,7 @@ expose the following methods:
A previously set ID or null (if not defined).


.. confval:: setProperty($propertyName, $propertyValue)
.. confval:: setProperty($propertyName, $propertyValue): static
:name: abstracttype-setproperty

Call this method to set a property or overwrite a previously one.
Expand All @@ -340,7 +341,7 @@ expose the following methods:
Reference to the model itself.


.. confval:: addProperty($propertyName, $propertyValue)
.. confval:: addProperty($propertyName, $propertyValue): static
:name: abstracttype-addproperty

Call this method if you want to add a value to an existing one. In the
Expand All @@ -364,7 +365,7 @@ expose the following methods:
Reference to the model itself.


.. confval:: setProperties($properties)
.. confval:: setProperties($properties): static
:name: abstracttype-setproperties

Set multiple properties at once.
Expand All @@ -379,7 +380,7 @@ expose the following methods:
Reference to the model itself.


.. confval:: getProperty($propertyName)
.. confval:: getProperty($propertyName): mixed
:name: abstracttype-getproperty

Get the value of a property.
Expand All @@ -394,7 +395,7 @@ expose the following methods:
strings, array of models, null).


.. confval:: hasProperty($propertyName)
.. confval:: hasProperty($propertyName): bool
:name: abstracttype-hasproperty

Check whether the property name exists in a particular model.
Expand All @@ -407,7 +408,7 @@ expose the following methods:
:php:`true`, if the property exists and :php:`false`, otherwise.


.. confval:: clearProperty($propertyName)
.. confval:: clearProperty($propertyName): static
:name: abstracttype-clearproperty

Resets the value of the property (set it to :php:`null`).
Expand All @@ -421,7 +422,7 @@ expose the following methods:
Reference to the model itself.


.. confval:: getPropertyNames()
.. confval:: getPropertyNames(): array
:name: abstracttype-getpropertynames

Get the names of all properties of the model.
Expand All @@ -430,7 +431,7 @@ expose the following methods:
Array of all property names of the model.


.. confval:: getType()
.. confval:: getType(): string|string[]
:name: abstracttype-gettype

Get the type of the model.
Expand Down

0 comments on commit 3f64e53

Please sign in to comment.