Skip to content

Commit 08db2fd

Browse files
committed
Merge branch 'main' into neos-9
* main: TASK: Use non nullable returns when default value is present
2 parents 4461bc9 + 991265a commit 08db2fd

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

Classes/Domain/NodePropertySpecification.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
readonly class NodePropertySpecification
1111
{
1212
public function __construct(
13-
readonly string $propertyName,
14-
readonly string $propertyType
13+
public string $propertyName,
14+
public string $propertyType,
15+
public mixed $defaultValue,
1516
) {
1617
}
1718
}

Classes/Domain/NodePropertySpecificationCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
/**
1616
* @var NodePropertySpecification[]
1717
*/
18-
public array $properties;
18+
public array $items;
1919

2020
public function __construct(
2121
NodePropertySpecification ...$properties
2222
) {
23-
$this->properties = $properties;
23+
$this->items = $properties;
2424
}
2525

2626
/**
2727
* @return \Generator<int, NodePropertySpecification>
2828
*/
2929
public function getIterator(): \Generator
3030
{
31-
yield from $this->properties;
31+
yield from $this->items;
3232
}
3333
}

Classes/Factory/NodeTypeObjectFileFactory.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PackageFactory\NodeTypeObjects\Factory;
66

77
use Neos\Utility\Unicode\Functions as UnicodeFunctions;
8+
use PackageFactory\NodeTypeObjects\Domain\NodePropertySpecification;
89
use PackageFactory\NodeTypeObjects\Domain\NodeTypeObjectFile;
910
use PackageFactory\NodeTypeObjects\Domain\NodeTypeObjectSpecification;
1011

@@ -14,10 +15,13 @@ public function createNodeTypeObjectPhpCodeFromNode(
1415
NodeTypeObjectSpecification $specification,
1516
): NodeTypeObjectFile {
1617
$propertyAccessors = '';
18+
$internalPropertyAccessors = '';
1719
foreach ($specification->properties as $property) {
1820
$propertyType = $property->propertyType;
21+
$propertyDefaultValue = $property->defaultValue;
1922
$propertyName = $property->propertyName;
20-
if (str_starts_with($property->propertyName, '_')) {
23+
$propertyIsInternal = str_starts_with($property->propertyName, '_');
24+
if ($propertyIsInternal) {
2125
$methodName = 'getInternal' . UnicodeFunctions::ucfirst(substr($propertyName, 1));
2226
} else {
2327
$methodName = 'get' . UnicodeFunctions::ucfirst($propertyName);
@@ -47,28 +51,59 @@ public function createNodeTypeObjectPhpCodeFromNode(
4751
$annotationType = '\\' . $annotationType;
4852
}
4953

50-
if ($annotationType) {
51-
$propertyAccessors .= <<<EOL
54+
$returnType = ($propertyDefaultValue === null) ? '?' . $phpType : $phpType;
55+
56+
$defaultReturn = match(true) {
57+
($propertyType === 'DateTime' && is_string($propertyDefaultValue)) => 'new \DateTime(\'' . $propertyDefaultValue . '\')',
58+
default => var_export($propertyDefaultValue, true),
59+
};
5260

61+
$typeCheck = match($phpType) {
62+
'null' => 'is_null($value)',
63+
'string' => 'is_string($value)',
64+
'int' => 'is_int($value)',
65+
'float' => 'is_float($value)',
66+
'bool' => 'is_bool($value)',
67+
'array' => 'is_array($value)',
68+
default => '$value instanceof ' . $phpType,
69+
};
70+
71+
if ($annotationType) {
72+
$propertyAccessor = <<<EOL
5373
5474
/**
5575
* @return ?$annotationType;
5676
*/
57-
public function $methodName(): ?$phpType
77+
public function $methodName(): $returnType
5878
{
59-
return \$this->node->getProperty('$propertyName');
79+
\$value = \$this->node->getProperty('$propertyName');
80+
if ($typeCheck) {
81+
return \$value;
82+
}
83+
return $defaultReturn;
6084
}
85+
6186
EOL;
6287
} else {
63-
$propertyAccessors .= <<<EOL
64-
88+
$propertyAccessor = <<<EOL
6589
66-
public function $methodName(): ?$phpType
90+
public function $methodName(): $returnType
6791
{
68-
return \$this->node->getProperty('$propertyName');
92+
\$value = \$this->node->getProperty('$propertyName');
93+
if ($typeCheck) {
94+
return \$value;
95+
}
96+
return $defaultReturn;
6997
}
98+
7099
EOL;
71100
}
101+
102+
if ($propertyIsInternal) {
103+
$internalPropertyAccessors .= $propertyAccessor;
104+
} else {
105+
$propertyAccessors .= $propertyAccessor;
106+
}
72107
}
73108

74109
$nodeTypeName = $specification->nodeTypeName;
@@ -106,7 +141,13 @@ public static function fromNode(Node \$node): self
106141
throw new \Exception("unsupported nodetype " . \$node->nodeTypeName->value);
107142
}
108143
return new self(\$node);
109-
}$propertyAccessors
144+
}
145+
146+
// property accessors
147+
$propertyAccessors
148+
149+
// internal property accessors
150+
$internalPropertyAccessors
110151
}
111152
112153
EOL;

Classes/Factory/NodeTypeSpecificationFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public function createFromPackageKeyAndNodeType(
4141
foreach ($nodeType->getProperties() as $propertyName => $propertyConfiguration) {
4242
$propertySpecifications[] = new NodePropertySpecification(
4343
$propertyName,
44-
$nodeType->getPropertyType($propertyName)
44+
$nodeType->getPropertyType($propertyName),
45+
$propertyConfiguration['defaultValue'] ?? null
4546
);
4647
}
4748

0 commit comments

Comments
 (0)