Skip to content

Commit 38c1b8b

Browse files
ClassMetadata::isDataTypeSpecificationAnObject() implemented.
1 parent 7c7524b commit 38c1b8b

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/Metadata/ClassMetadata.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ public function fullyQualifiedDataTypeSpecification(?string $originalSpecificati
132132
return implode('|', $specifications);
133133
}
134134

135+
/**
136+
* Returns if the data type specification corresponds to an object.
137+
*
138+
* Union types and interfaces are not supported.
139+
*/
140+
public function isDataTypeSpecificationAnObject(?string $originalSpecification) : bool
141+
{
142+
$fullyQualified = $this->fullyQualifiedDataTypeSpecification($originalSpecification);
143+
144+
if ('[]' === substr($fullyQualified, -2)) {
145+
$fullyQualified = substr($fullyQualified, 0, -2);
146+
}
147+
148+
if (
149+
'self' === $fullyQualified ||
150+
'$this' === $fullyQualified
151+
) {
152+
return true;
153+
}
154+
155+
return class_exists($fullyQualified);
156+
}
157+
135158
public function serialize() : string
136159
{
137160
return serialize(

tests/PhpUnit/Metadata/ClassMetadataTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace ScaleUpStack\Metadata\Tests\PhpUnit\Metadata;
1414

1515
use ScaleUpStack\Annotations\Annotations;
16+
use ScaleUpStack\Metadata\Generator\FeatureAnalyzer;
1617
use ScaleUpStack\Metadata\Metadata\ClassMetadata;
1718
use ScaleUpStack\Metadata\Tests\Resources\ClassForTesting;
1819
use ScaleUpStack\Metadata\Tests\Resources\TestCase;
@@ -140,6 +141,9 @@ public function provides_short_and_fully_qualified_data_type_specifications() :
140141
['ClassMetadata[]', 'ScaleUpStack\Metadata\ClassMetadata[]'],
141142

142143
['int[]|\DateTime', 'int[]|DateTime'],
144+
145+
['self', 'self'],
146+
['$this', '$this'],
143147
];
144148
}
145149

@@ -170,5 +174,46 @@ public function it_transforms_a_data_type_specification_into_a_fully_qualified_s
170174
// then the specification was transformed to a fully qualified data type specification
171175
$this->assertSame($expectedLongSpecification, $longSpecification);
172176
}
173-
}
174177

178+
public function provides_data_type_specifications_and_if_they_are_an_object() : array
179+
{
180+
return [
181+
['bool', false],
182+
['int[]', false],
183+
[\DateTime::class, true],
184+
[\DateTime::class . '[]', true],
185+
[\DateTime::class . '|int', false], // union types are not supported; TODO: is this ok?
186+
['ClassMetadata', true],
187+
[null, false],
188+
['', false],
189+
['self', true],
190+
['$this', true],
191+
[\DateTimeInterface::class, false], // interfaces are not supported
192+
[FeatureAnalyzer::class, false],
193+
];
194+
}
195+
196+
/**
197+
* @test
198+
* @dataProvider provides_data_type_specifications_and_if_they_are_an_object
199+
* @covers ::isDataTypeSpecificationAnObject()
200+
*/
201+
public function it_checks_if_a_data_type_specification_is_an_object(?string $specification, bool $expectedIsObject)
202+
{
203+
// given some ClassMetadata
204+
$classMetadata = new ClassMetadata(
205+
ClassForTesting::class,
206+
[
207+
'ScaleUpStack\Metadata\Metadata\ClassMetadata',
208+
],
209+
new Annotations()
210+
);
211+
// and a data type specification as provided by the test's parameter
212+
213+
// when checking if the data type specification is an object
214+
$isObject = $classMetadata->isDataTypeSpecificationAnObject($specification);
215+
216+
// then the result is as expected as provided by the test's parameter
217+
$this->assertSame($expectedIsObject, $isObject);
218+
}
219+
}

0 commit comments

Comments
 (0)