Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Make single-field Value Objects stringable #5241

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepository\BehavioralTests\PhpstanRules;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;

/**
* @implements Rule<\PhpParser\Node\Expr\Cast\String_>
*/
class NoStringCastingRule implements Rule
{
/**
* @var string[]
*/
private array $namespacePrefixesWhichShouldBeEnforced = [
'Neos\\ContentRepository\\Core',
'Neos\\ContentGraph',
];

public function getNodeType(): string
{
return \PhpParser\Node\Expr\Cast\String_::class;
}

public function processNode(Node $node, Scope $scope): array
{
assert($node instanceof \PhpParser\Node\Expr\Cast\String_);
$expressionType = $scope->getType($node->expr);
if (!$expressionType instanceof ObjectType || !$this->classNameIsWithinConfiguredNamespaces($expressionType->getClassName())) {
return [];
}
return [
RuleErrorBuilder::message("Casting objects of class {$expressionType->getClassName()} to string is not allowed.")->build(),
];
}

private function classNameIsWithinConfiguredNamespaces(string $className): bool
{
foreach ($this->namespacePrefixesWhichShouldBeEnforced as $namespacePrefix) {
if (str_starts_with($className, $namespacePrefix . '\\')) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function jsonSerialize(): string
{
return $this->value;
}

public function __toString(): string
{
return $this->value;
}
}
5 changes: 5 additions & 0 deletions Neos.ContentRepository.Core/Classes/NodeType/NodeTypeName.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ 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 @@ -178,4 +178,9 @@ public function jsonSerialize(): string
{
return $this->serializeToString();
}

public function __toString(): string
{
return $this->serializeToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ 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 @@ -147,4 +147,9 @@ public function jsonSerialize(): string
{
return $this->serializeToString();
}

public function __toString(): string
{
return $this->serializeToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public function jsonSerialize(): mixed
{
return $this->value;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ private static function hasValidFormat(string $value): bool
{
return preg_match(self::PATTERN, $value) === 1;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ public function equals(NodeName $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 @@ -36,4 +36,9 @@ 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 @@ -36,4 +36,9 @@ 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 @@ -70,4 +70,9 @@ 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 @@ -70,4 +70,9 @@ public function equals(self $other): bool
{
return $this === $other;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ 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 @@ -114,4 +114,9 @@ private static function hasValidFormat(string $value): bool
{
return preg_match(self::PATTERN, $value) === 1;
}

public function __toString(): string
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ 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 @@ -23,4 +23,9 @@ public static function fromString(string $value): self
}
return new self($value);
}

public function __toString(): string
{
return $this->value;
}
}
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ rules:
- Neos\ContentRepository\BehavioralTests\PhpstanRules\ApiOrInternalAnnotationRule
- Neos\ContentRepository\BehavioralTests\PhpstanRules\InternalMethodsNotAllowedOutsideContentRepositoryRule
- Neos\ContentRepository\BehavioralTests\PhpstanRules\DeclareStrictTypesRule
- Neos\ContentRepository\BehavioralTests\PhpstanRules\NoStringCastingRule
Loading