Skip to content

Commit

Permalink
!!! TASK: Remove magic __toString methods from Value Objects
Browse files Browse the repository at this point in the history
Related: #4133
  • Loading branch information
bwaidelich committed Apr 1, 2023
1 parent a4a8874 commit 31cb00a
Show file tree
Hide file tree
Showing 38 changed files with 94 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @api
*/
final class ContentDimensionId implements \JsonSerializable, \Stringable
final class ContentDimensionId implements \JsonSerializable
{
/**
* @throws ContentDimensionIdIsInvalid
Expand All @@ -43,9 +43,4 @@ public function jsonSerialize(): string
{
return $this->id;
}

public function __toString(): string
{
return $this->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* A content dimension value in a single ContentDimension; e.g. the value "de" in the dimension "language".
* @api
*/
final class ContentDimensionValue implements \Stringable
final class ContentDimensionValue
{
/**
* @throws ContentDimensionValueIsInvalid
Expand Down Expand Up @@ -64,9 +64,4 @@ public function getConfigurationValue(string $path): mixed

return Arrays::getValueByPath($configuration, $path);
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @internal
*/
final class ContentDimensionValueSpecializationDepth implements \JsonSerializable, \Stringable
final class ContentDimensionValueSpecializationDepth implements \JsonSerializable
{
/**
* @throws ContentDimensionValueSpecializationDepthIsInvalid
Expand Down Expand Up @@ -70,9 +70,4 @@ public function jsonSerialize(): int
{
return $this->depth;
}

public function __toString(): string
{
return (string)$this->depth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
*
* @api
*/
abstract class AbstractDimensionSpacePoint implements
\JsonSerializable,
\Stringable
abstract class AbstractDimensionSpacePoint implements \JsonSerializable
{
protected function __construct(
/**
Expand Down Expand Up @@ -149,7 +147,7 @@ final public function jsonSerialize(): array
/**
* @throws \JsonException
*/
final public function __toString(): string
final public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @see Dimension\ContentDimensionValueSpecializationDepth
* @internal
*/
final class ContentSubgraphVariationWeight implements \JsonSerializable, \Stringable
final class ContentSubgraphVariationWeight implements \JsonSerializable
{
public function __construct(
/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public function jsonSerialize(): array
/**
* @throws \JsonException
*/
public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
final class DimensionSpacePointSet implements
\JsonSerializable,
\Stringable,
\IteratorAggregate,
\ArrayAccess,
\Countable
Expand Down Expand Up @@ -161,7 +160,7 @@ public function getIterator(): \ArrayIterator
/**
* @throws \JsonException
*/
public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function contains(OriginDimensionSpacePoint $point): bool
return isset($this->points[$point->hash]);
}

public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,4 @@ public function equals(self $other): bool
{
return $this->value === $other->value;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,4 @@ public function getEventStreamName(): StreamName
{
return StreamName::fromString($this->eventStreamName);
}

public function __toString(): string
{
return $this->eventStreamName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public function jsonSerialize(): array
];
}

public function __toString(): string
public function __debugInfo(): array
{
return json_encode($this->value, JSON_THROW_ON_ERROR) . ' (' . $this->type . ')';
return [
'type' => $this->type,
'value' => json_encode($this->value, JSON_THROW_ON_ERROR)
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function jsonSerialize(): array
];
}

public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function jsonSerialize(): array
];
}

public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,4 @@ public function getSerializationType(): string
{
return $this->value;
}

public function __toString(): string
{
return $this->value;
}
}
12 changes: 2 additions & 10 deletions Neos.ContentRepository.Core/Classes/NodeType/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ public function getNodeLabelGenerator(): NodeLabelGeneratorInterface
} else {
// TODO
/** @var NodeLabelGeneratorInterface $nodeLabelGenerator */
$nodeLabelGenerator = $this->objectManager->get(NodeLabelGeneratorInterface::class);
$nodeLabelGenerator = $this->objectManager->get(\Neos\ContentRepositoryRegistry\NodeLabel\ExpressionBasedNodeLabelGenerator::class);
//$nodeLabelGenerator = $this->objectManager->get(NodeLabelGeneratorInterface::class);
}
$this->nodeLabelGenerator = $nodeLabelGenerator;
}
Expand Down Expand Up @@ -714,13 +715,4 @@ protected function setFullConfiguration(array $fullConfiguration): void
{
$this->fullConfiguration = $fullConfiguration;
}

/**
* Alias for name.
* @api
*/
public function __toString(): string
{
return $this->name->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @api
*/
final class NodeTypeName implements \JsonSerializable, \Stringable
final class NodeTypeName implements \JsonSerializable
{
public const ROOT_NODE_TYPE_NAME = 'Neos.ContentRepository:Root';

Expand Down Expand Up @@ -55,9 +55,4 @@ public function jsonSerialize(): string
{
return $this->value;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @implements \IteratorAggregate<string,DimensionSpacePointSet>
* @internal no part of public APIs
*/
final class CoverageByOrigin implements \IteratorAggregate, \JsonSerializable, \Stringable
final class CoverageByOrigin implements \IteratorAggregate, \JsonSerializable
{
/**
* The actual coverage.
Expand Down Expand Up @@ -90,7 +90,7 @@ public function jsonSerialize(): array
return $this->coverage;
}

public function __toString(): string
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,24 @@
* It describes the hierarchy path of a node to a root node in a subgraph.
* @api
*/
final class NodePath implements \JsonSerializable, \Stringable
final class NodePath implements \JsonSerializable
{
private string $path;

private function __construct(string $path)
{
if ($path !== '/') {
$pathParts = explode('/', ltrim($path, '/'));
private function __construct(
public readonly string $value
) {
if ($this->value !== '/') {
$pathParts = explode('/', ltrim($this->value, '/'));
foreach ($pathParts as $pathPart) {
if (preg_match(NodeName::PATTERN, $pathPart) !== 1) {
throw new \InvalidArgumentException(sprintf(
'The path "%s" is no valid NodePath because it contains a segment "%s"'
. ' that is no valid NodeName',
$path,
$this->value,
$pathPart
), 1548157108);
}
}
}
$this->path = $path;
}

public static function fromString(string $path): self
Expand All @@ -62,57 +60,49 @@ public static function fromPathSegments(array $pathSegments): self

public function isRoot(): bool
{
return $this->path === '/';
return $this->value === '/';
}

public function isAbsolute(): bool
{
return strpos($this->path, '/') === 0;
return strpos($this->value, '/') === 0;
}

/**
* IMMUTABLE function to create a new NodePath by appending a path segment. Returns a NEW NodePath object
*/
public function appendPathSegment(NodeName $nodeName): self
{
return new self($this->path . '/' . $nodeName);
return new self($this->value . '/' . $nodeName);
}

/**
* @return NodeName[]
*/
public function getParts(): array
{
$pathParts = explode('/', ltrim($this->path, '/'));

return array_map(function (string $pathPart) {
return NodeName::fromString($pathPart);
}, $pathParts);
$pathParts = explode('/', ltrim($this->value, '/'));
return array_map(static fn (string $pathPart) => NodeName::fromString($pathPart), $pathParts);
}

public function getDepth(): int
{
if (!$this->isAbsolute()) {
throw new \RuntimeException(sprintf(
'Depth of relative node path "%s" cannot be determined',
$this->path
$this->value
), 1548162166);
}
return count($this->getParts());
}

public function equals(NodePath $other): bool
{
return (string) $this === (string) $other;
return $this->value === $other->value;
}

public function jsonSerialize(): string
{
return $this->path;
}

public function __toString(): string
{
return $this->path;
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ public function withAdditionalAllowedNodeType(NodeTypeName $nodeTypeName): self
);
}

public function __toString(): string
public function toFilterString(): string
{
$legacyParts = [];
$parts = [];
foreach ($this->explicitlyDisallowedNodeTypeNames as $nodeTypeName) {
$legacyParts[] = '!' . $nodeTypeName;
$parts[] = '!' . $nodeTypeName;
}

foreach ($this->explicitlyAllowedNodeTypeNames as $nodeTypeName) {
$legacyParts[] = (string)$nodeTypeName;
$parts[] = (string)$nodeTypeName;
}

return implode(',', $legacyParts);
return implode(',', $parts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,21 @@ public function matches(NodeTypeName $nodeTypeName): bool
return $this->isWildCardAllowed;
}

public function __toString(): string
public function toFilterString(): string
{
$legacyParts = [];
$parts = [];
if ($this->isWildCardAllowed) {
$legacyParts[] = '*';
$parts[] = '*';
}

foreach ($this->explicitlyDisallowedNodeTypeNames as $nodeTypeName) {
$legacyParts[] = '!' . $nodeTypeName;
$parts[] = '!' . $nodeTypeName;
}

foreach ($this->explicitlyAllowedNodeTypeNames as $nodeTypeName) {
$legacyParts[] = (string)$nodeTypeName;
$parts[] = (string)$nodeTypeName;
}

return implode(',', $legacyParts);
return implode(',', $parts);
}
}
Loading

0 comments on commit 31cb00a

Please sign in to comment.