Skip to content

Commit e990ac5

Browse files
janlam7Hidde Boomsma
authored andcommitted
Add typehint on getters (#32)
Add typehint on getters
1 parent 3be12f1 commit e990ac5

27 files changed

+81
-79
lines changed

src/Resources/templates/get.php.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
{% if property.collection -%}
1919
* @return {{ property.fullyQualifiedType }}[]|ImmutableCollection
2020
{% else -%}
21-
* @return {{ (property.isComplexType ? property.typeHint : property.type) | phptype }}{{ not property.willGenerateStrict ? '|null' }}
21+
* @return {{ (property.isComplexType ? property.typeHint : property.type) | phptype }}{{ property.isNullable or not property.willGenerateStrict ? '|null' }}
2222
{% endif -%}
2323
*/
24-
{{ property.getGetVisibility() }} function {{ getter }}()
24+
{{ property.getGetVisibility() }} function {{ getter }}(){% if property.type != 'resource' and property.type != 'object' %}: {% if property.collection -%} iterable
25+
{% else -%}{{ property.isNullable or not property.willGenerateStrict ? '?' }}{{ (property.isComplexType ? property.typeHint : property.type) | phptype }}
26+
{% endif %}{% else %}
27+
28+
{% endif %}
2529
{
2630
if (func_num_args() > 0) {
2731
throw new \BadMethodCallException(

test/Generator/ProductTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ public function testGetSystemNameTooManyArguments()
177177
public function testGetDuration()
178178
{
179179
$duration = new Period();
180-
$duration = new \ReflectionProperty(Product::class, 'duration');
181-
$duration->setAccessible(true);
182-
$duration->setValue($this->product, $duration);
180+
$property = new \ReflectionProperty(Product::class, 'duration');
181+
$property->setAccessible(true);
182+
$property->setValue($this->product, $duration);
183183

184184
self::assertSame($duration, $this->product->getDuration());
185185
}

test/Generator/TypesTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ public function typeProvider()
5353

5454
public function getTypeProvider()
5555
{
56-
$values = [['id', 0]];
57-
58-
$class = new \ReflectionClass(Types::class);
56+
$values = [['id', '0']];
57+
$class = new \ReflectionClass(Types::class);
5958

6059
foreach ($class->getProperties() as $property) {
6160
$values[] = [$property->getName(), null, \BadMethodCallException::class, 1];
@@ -64,7 +63,6 @@ public function getTypeProvider()
6463
foreach ($class->getProperties() as $property) {
6564
$values[] = [$property->getName(), null, \LogicException::class];
6665
}
67-
6866
return array_merge($this->typeProvider(), $values);
6967
}
7068

test/Generator/fixtures/expected/ActorMethodsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait ActorMethodsTrait
1717
*
1818
* @return \Hostnet\Component\AccessorGenerator\Generator\fixtures\Movie[]|ImmutableCollection
1919
*/
20-
public function getMovies()
20+
public function getMovies(): iterable
2121
{
2222
if (func_num_args() > 0) {
2323
throw new \BadMethodCallException(

test/Generator/fixtures/expected/AnnotationsMethodsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait AnnotationsMethodsTrait
1717
*
1818
* @return \DateTime
1919
*/
20-
public function getStupid()
20+
public function getStupid(): \DateTime
2121
{
2222
if (func_num_args() > 0) {
2323
throw new \BadMethodCallException(

test/Generator/fixtures/expected/CartMethodsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait CartMethodsTrait
1818
*
1919
* @return Client|null
2020
*/
21-
public function getCustomer()
21+
public function getCustomer(): ?Client
2222
{
2323
if (func_num_args() > 0) {
2424
throw new \BadMethodCallException(

test/Generator/fixtures/expected/CategoryMethodsTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait CategoryMethodsTrait
1717
*
1818
* @return \Hostnet\Component\AccessorGenerator\Generator\fixtures\Category[]|ImmutableCollection
1919
*/
20-
public function getChildren()
20+
public function getChildren(): iterable
2121
{
2222
if (func_num_args() > 0) {
2323
throw new \BadMethodCallException(
@@ -124,9 +124,9 @@ public function removeChild(Category $child)
124124
*
125125
* @throws \BadMethodCallException
126126
*
127-
* @return Category
127+
* @return Category|null
128128
*/
129-
public function getParent()
129+
public function getParent(): ?Category
130130
{
131131
if (func_num_args() > 0) {
132132
throw new \BadMethodCallException(

test/Generator/fixtures/expected/CleanCarsMethodsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait CleanCarsMethodsTrait
1919
*
2020
* @return \Hostnet\Component\AccessorGenerator\Generator\fixtures\Car[]|ImmutableCollection
2121
*/
22-
public function getCars()
22+
public function getCars(): iterable
2323
{
2424
if (func_num_args() > 0) {
2525
throw new \BadMethodCallException(

test/Generator/fixtures/expected/CommentMethodsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ trait CommentMethodsTrait
1414
*
1515
* @throws \BadMethodCallException
1616
*
17-
* @return string
17+
* @return string|null
1818
*/
19-
public function getCol()
19+
public function getCol(): ?string
2020
{
2121
if (func_num_args() > 0) {
2222
throw new \BadMethodCallException(

test/Generator/fixtures/expected/ConstantDefaultMethodsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait ConstantDefaultMethodsTrait
1818
*
1919
* @return int
2020
*/
21-
public function getWeather()
21+
public function getWeather(): int
2222
{
2323
if (func_num_args() > 0) {
2424
throw new \BadMethodCallException(

0 commit comments

Comments
 (0)