Skip to content

Commit 224209a

Browse files
committed
PhpNamespace::unresolveName() renamed to simplifyName()
1 parent 68233f6 commit 224209a

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ $class->addImplement('Foo\A') // it will resolve to A
600600
->addTrait('Bar\AliasedClass'); // it will resolve to AliasedClass
601601

602602
$method = $class->addMethod('method');
603-
$method->addComment('@return ' . $namespace->unresolveName('Foo\D')); // in comments resolve manually
603+
$method->addComment('@return ' . $namespace->simplifyType('Foo\D')); // in comments resolve manually
604604
$method->addParameter('arg')
605605
->setType('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
606606

src/PhpGenerator/PhpNamespace.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,20 @@ public function getUses(): array
132132
}
133133

134134

135-
public function unresolveType(string $type): string
135+
/** @deprecated use simplifyName() */
136+
public function unresolveName(string $name): string
136137
{
137-
return preg_replace_callback('~[^|&?]+~', function ($m) { return $this->unresolveName($m[0]); }, $type);
138+
return $this->simplifyName($name);
138139
}
139140

140141

141-
public function unresolveName(string $name): string
142+
public function simplifyType(string $type): string
143+
{
144+
return preg_replace_callback('~[^|&?]+~', function ($m) { return $this->simplifyName($m[0]); }, $type);
145+
}
146+
147+
148+
public function simplifyName(string $name): string
142149
{
143150
if (isset(Helpers::KEYWORDS[strtolower($name)]) || $name === '') {
144151
return $name;

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
136136
$this->namespace = $this->resolveTypes ? $namespace : null;
137137
$class->validate();
138138
$resolver = $this->namespace
139-
? [$namespace, 'unresolveType']
139+
? [$namespace, 'simplifyType']
140140
: function ($s) { return $s; };
141141

142142
$traits = [];
@@ -343,7 +343,7 @@ public function printType(?string $type, bool $nullable): string
343343
return '';
344344
}
345345
if ($this->namespace) {
346-
$type = $this->namespace->unresolveType($type);
346+
$type = $this->namespace->simplifyType($type);
347347
}
348348
if ($nullable && strcasecmp($type, 'mixed')) {
349349
$type = strpos($type, '|') === false

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,46 @@ require __DIR__ . '/../bootstrap.php';
1515
$namespace = new PhpNamespace('');
1616

1717
Assert::same('', $namespace->getName());
18-
Assert::same('A', $namespace->unresolveName('A'));
19-
Assert::same('foo\A', $namespace->unresolveName('foo\A'));
18+
Assert::same('A', $namespace->simplifyName('A'));
19+
Assert::same('foo\A', $namespace->simplifyName('foo\A'));
2020

2121
$namespace->addUse('Bar\C');
2222

23-
Assert::same('Bar', $namespace->unresolveName('Bar'));
24-
Assert::same('C', $namespace->unresolveName('bar\C'));
25-
Assert::same('C\D', $namespace->unresolveName('Bar\C\D'));
23+
Assert::same('Bar', $namespace->simplifyName('Bar'));
24+
Assert::same('C', $namespace->simplifyName('bar\C'));
25+
Assert::same('C\D', $namespace->simplifyName('Bar\C\D'));
2626

2727
foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self', 'parent', ''] as $type) {
28-
Assert::same($type, $namespace->unresolveName($type));
28+
Assert::same($type, $namespace->simplifyName($type));
2929
}
3030

3131

3232
$namespace = new PhpNamespace('Foo');
3333

3434
Assert::same('Foo', $namespace->getName());
35-
Assert::same('\A', $namespace->unresolveName('\A'));
36-
Assert::same('\A', $namespace->unresolveName('A'));
37-
Assert::same('A', $namespace->unresolveName('foo\A'));
35+
Assert::same('\A', $namespace->simplifyName('\A'));
36+
Assert::same('\A', $namespace->simplifyName('A'));
37+
Assert::same('A', $namespace->simplifyName('foo\A'));
3838

39-
Assert::same('A', $namespace->unresolveType('foo\A'));
40-
Assert::same('null|A', $namespace->unresolveType('null|foo\A'));
41-
Assert::same('?A', $namespace->unresolveType('?foo\A'));
42-
Assert::same('A&\Countable', $namespace->unresolveType('foo\A&Countable'));
43-
Assert::same('', $namespace->unresolveType(''));
39+
Assert::same('A', $namespace->simplifyType('foo\A'));
40+
Assert::same('null|A', $namespace->simplifyType('null|foo\A'));
41+
Assert::same('?A', $namespace->simplifyType('?foo\A'));
42+
Assert::same('A&\Countable', $namespace->simplifyType('foo\A&Countable'));
43+
Assert::same('', $namespace->simplifyType(''));
4444

4545
$namespace->addUse('Foo');
46-
Assert::same('B', $namespace->unresolveName('Foo\B'));
46+
Assert::same('B', $namespace->simplifyName('Foo\B'));
4747

4848
$namespace->addUse('Bar\C');
4949
Assert::same(['C' => 'Bar\C', 'Foo' => 'Foo'], $namespace->getUses());
5050

51-
Assert::same('\Bar', $namespace->unresolveName('Bar'));
52-
Assert::same('C', $namespace->unresolveName('\bar\C'));
53-
Assert::same('C', $namespace->unresolveName('bar\C'));
54-
Assert::same('C\D', $namespace->unresolveName('Bar\C\D'));
51+
Assert::same('\Bar', $namespace->simplifyName('Bar'));
52+
Assert::same('C', $namespace->simplifyName('\bar\C'));
53+
Assert::same('C', $namespace->simplifyName('bar\C'));
54+
Assert::same('C\D', $namespace->simplifyName('Bar\C\D'));
5555

5656
foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'self', 'parent', ''] as $type) {
57-
Assert::same($type, $namespace->unresolveName($type));
57+
Assert::same($type, $namespace->simplifyName($type));
5858
}
5959

6060

0 commit comments

Comments
 (0)