Skip to content

Commit 173dd9d

Browse files
committed
added setType()/getType()/hasStrictTypes()/hasBracketedSyntax()/isReturnNullable() as replacement for setTypeHint()/getTypeHint()/getStrictTypes()/getBracketedSyntax()/getReturnNullable()
1 parent 7676b10 commit 173dd9d

16 files changed

+76
-40
lines changed

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ $method = $class->addMethod('count')
104104

105105
$method->addParameter('items', []) // $items = []
106106
->setReference() // &$items = []
107-
->setTypeHint('array'); // array &$items = []
107+
->setType('array'); // array &$items = []
108108
```
109109

110110
It results in:
@@ -138,7 +138,7 @@ $method = $class->addMethod('getValue')
138138
->setBody('return count($this->items);');
139139

140140
$method->addParameter('id')
141-
->setTypeHint('int') // scalar type hint
141+
->setType('int') // scalar type hint
142142
->setNullable(); // nullable type hint
143143

144144
echo $class;
@@ -431,7 +431,7 @@ $class->addImplement('Foo\A') // it will resolve to A
431431
$method = $class->addMethod('method');
432432
$method->addComment('@return ' . $namespace->unresolveName('Foo\D')); // in comments resolve manually
433433
$method->addParameter('arg')
434-
->setTypeHint('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
434+
->setType('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
435435

436436
echo $namespace;
437437

src/PhpGenerator/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
107107
{
108108
$param = new Parameter($from->getName());
109109
$param->setReference($from->isPassedByReference());
110-
$param->setTypeHint($from->hasType() ? $from->getType()->getName() : null);
110+
$param->setType($from->hasType() ? $from->getType()->getName() : null);
111111
$param->setNullable($from->hasType() && $from->getType()->allowsNull());
112112
if ($from->isDefaultValueAvailable()) {
113113
$param->setDefaultValue($from->isDefaultValueConstant()

src/PhpGenerator/Parameter.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Parameter
2626
private $reference = false;
2727

2828
/** @var string|null */
29-
private $typeHint;
29+
private $type;
3030

3131
/** @var bool */
3232
private $nullable = false;
@@ -57,16 +57,31 @@ public function isReference(): bool
5757
/**
5858
* @return static
5959
*/
60-
public function setTypeHint(?string $hint): self
60+
public function setType(?string $type): self
6161
{
62-
$this->typeHint = $hint;
62+
$this->type = $type;
6363
return $this;
6464
}
6565

6666

67+
public function getType(): ?string
68+
{
69+
return $this->type;
70+
}
71+
72+
73+
/** @deprecated use setType() */
74+
public function setTypeHint(?string $type): self
75+
{
76+
$this->type = $type;
77+
return $this;
78+
}
79+
80+
81+
/** @deprecated use getType() */
6782
public function getTypeHint(): ?string
6883
{
69-
return $this->typeHint;
84+
return $this->type;
7085
}
7186

7287

src/PhpGenerator/PhpFile.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ public function setStrictTypes(bool $on = true): self
9898
}
9999

100100

101+
public function hasStrictTypes(): bool
102+
{
103+
return $this->strictTypes;
104+
}
105+
106+
107+
/** @deprecated use hasStrictTypes() */
101108
public function getStrictTypes(): bool
102109
{
103110
return $this->strictTypes;

src/PhpGenerator/PhpNamespace.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public function setBracketedSyntax(bool $state = true): self
7070
}
7171

7272

73+
public function hasBracketedSyntax(): bool
74+
{
75+
return $this->bracketedSyntax;
76+
}
77+
78+
79+
/** @deprecated use hasBracketedSyntax() */
7380
public function getBracketedSyntax(): bool
7481
{
7582
return $this->bracketedSyntax;

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function printNamespace(PhpNamespace $namespace): string
149149
$body = ($uses ? $uses . "\n\n" : '')
150150
. implode("\n", $classes);
151151

152-
if ($namespace->getBracketedSyntax()) {
152+
if ($namespace->hasBracketedSyntax()) {
153153
return 'namespace' . ($name ? " $name" : '') . "\n{\n"
154154
. $this->indent($body)
155155
. "}\n";
@@ -172,7 +172,7 @@ public function printFile(PhpFile $file): string
172172
"<?php\n"
173173
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '')
174174
. "\n"
175-
. ($file->getStrictTypes() ? "declare(strict_types=1);\n\n" : '')
175+
. ($file->hasStrictTypes() ? "declare(strict_types=1);\n\n" : '')
176176
. implode("\n\n", $namespaces)
177177
) . "\n";
178178
}
@@ -226,8 +226,8 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
226226
$list = $function->getParameters();
227227
foreach ($list as $param) {
228228
$variadic = $function->isVariadic() && $param === end($list);
229-
$hint = $param->getTypeHint();
230-
$params[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($hint) : $hint) . ' ' : '')
229+
$type = $param->getType();
230+
$params[] = ($type ? ($param->isNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($type) : $type) . ' ' : '')
231231
. ($param->isReference() ? '&' : '')
232232
. ($variadic ? '...' : '')
233233
. '$' . $param->getName()
@@ -246,7 +246,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
246246
protected function printReturnType($function, ?PhpNamespace $namespace): string
247247
{
248248
return $function->getReturnType()
249-
? ': ' . ($function->getReturnNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType())
249+
? ': ' . ($function->isReturnNullable() ? '?' : '') . ($this->resolveTypes && $namespace ? $namespace->unresolveName($function->getReturnType()) : $function->getReturnType())
250250
: '';
251251
}
252252
}

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ public function setReturnNullable(bool $state = true): self
172172
}
173173

174174

175+
public function isReturnNullable(): bool
176+
{
177+
return $this->returnNullable;
178+
}
179+
180+
181+
/** @deprecated use isReturnNullable() */
175182
public function getReturnNullable(): bool
176183
{
177184
return $this->returnNullable;

tests/PhpGenerator/ClassType.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ $m->addParameter('mode', new PhpLiteral('self::ORDER'));
8989
Assert::false($m->isFinal());
9090
Assert::true($m->isStatic());
9191
Assert::true($m->getReturnReference());
92-
Assert::false($m->getReturnNullable());
92+
Assert::false($m->isReturnNullable());
9393
Assert::null($m->getReturnType());
9494
Assert::same('protected', $m->getVisibility());
9595

@@ -103,7 +103,7 @@ $method->addParameter('item');
103103

104104
$method->addParameter('res', null)
105105
->setReference(true)
106-
->setTypeHint('array');
106+
->setType('array');
107107

108108
sameFile(__DIR__ . '/expected/ClassType.expect', (string) $class);
109109

tests/PhpGenerator/Method.longParams.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $method = (new Method('create'))
1111
->setBody('return null;');
1212

1313
for ($name = 'a'; $name < 'm'; $name++) {
14-
$method->addParameter($name)->setTypeHint('string');
14+
$method->addParameter($name)->setType('string');
1515
}
1616

1717
same(

tests/PhpGenerator/Method.scalarParameters.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ interface Foo
2222
}
2323

2424
$method = Method::from(Foo::class . '::scalars');
25-
Assert::same('string', $method->getParameters()['a']->getTypeHint());
25+
Assert::same('string', $method->getParameters()['a']->getType());
2626

2727
$method = Method::from(Foo::class . '::scalars');
28-
Assert::same('bool', $method->getParameters()['b']->getTypeHint());
28+
Assert::same('bool', $method->getParameters()['b']->getType());
2929

3030
$method = Method::from(Foo::class . '::scalars');
31-
Assert::same('int', $method->getParameters()['c']->getTypeHint());
31+
Assert::same('int', $method->getParameters()['c']->getType());
3232

3333
$method = Method::from(Foo::class . '::scalars');
34-
Assert::same('float', $method->getParameters()['d']->getTypeHint());
34+
Assert::same('float', $method->getParameters()['d']->getType());
3535

3636

3737
// generating methods with scalar type hints
3838

3939
$method = (new Method('create'))
4040
->setBody('return null;');
41-
$method->addParameter('a')->setTypeHint('string');
42-
$method->addParameter('b')->setTypeHint('bool');
41+
$method->addParameter('a')->setType('string');
42+
$method->addParameter('b')->setType('bool');
4343

4444
same(
4545
'function create(string $a, bool $b)

0 commit comments

Comments
 (0)