Skip to content

Commit

Permalink
TASK: Docs and readability of Node::getProperty
Browse files Browse the repository at this point in the history
When seeing code like

```
$this->properties[$propertyName]
```

i always assume it will throw if the key not exits.
In our case the property collection will just return null instead.

To make this more obvious
A: i like to add a nullish-coalescing
or
B: use offsetGet similar to hasProperty uses offsetExist

Also the doc comment was adjusted, as its actually the same from the old cr and we dont have those funny content object dingsis anymore
  • Loading branch information
mhsdesign committed Sep 3, 2023
1 parent 71e64ed commit fcdfae7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(
*
* To read the serialized properties, call properties->serialized().
*
* @return PropertyCollectionInterface Property values, indexed by their name
* @var PropertyCollectionInterface Property values, indexed by their name
*/
public readonly PropertyCollectionInterface $properties,
public readonly ?NodeName $nodeName,
Expand All @@ -74,18 +74,15 @@ public function __construct(
}

/**
* Returns the specified property.
*
* If the node has a content object attached, the property will be fetched
* there if it is gettable.
* Returns the specified property, or null if it does not exist (or was set to null -> unset)
*
* @param string $propertyName Name of the property
* @return mixed value of the property
* @api
*/
public function getProperty(string $propertyName): mixed
{
return $this->properties[$propertyName];
return $this->properties->offsetGet($propertyName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public function offsetGet($offset): mixed
{
if (!isset($this->deserializedPropertyValuesRuntimeCache[$offset])) {
$serializedProperty = $this->serializedPropertyValues->getProperty($offset);
$this->deserializedPropertyValuesRuntimeCache[$offset] = $serializedProperty === null
? null
: $this->propertyConverter->deserializePropertyValue($serializedProperty);
if ($serializedProperty === null) {
return null;
}
$this->deserializedPropertyValuesRuntimeCache[$offset] =
$this->propertyConverter->deserializePropertyValue($serializedProperty);
}

return $this->deserializedPropertyValuesRuntimeCache[$offset];
Expand Down

0 comments on commit fcdfae7

Please sign in to comment.