Skip to content

Commit 2faccad

Browse files
committed
Merge branch 'type-alias' of https://github.com/vonglasow/zend-code
* 'type-alias' of https://github.com/vonglasow/zend-code: Remove method unneeded and refactor code. Remove unneeded method and rename property Allow to define if the type is an alias or not.
2 parents 202a97d + 57b8f04 commit 2faccad

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

src/Generator/ParameterGenerator.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static function fromArray(array $array)
9696
{
9797
if (!isset($array['name'])) {
9898
throw new Exception\InvalidArgumentException(
99-
'Paramerer generator requires that a name is provided for this object'
99+
'Parameter generator requires that a name is provided for this object'
100100
);
101101
}
102102

@@ -163,14 +163,31 @@ public function __construct(
163163
}
164164

165165
/**
166-
* @param string $type
166+
* @param mixed $type
167+
* @throws Exception\InvalidArgumentException
167168
* @return ParameterGenerator
168169
*/
169170
public function setType($type)
170171
{
171-
$this->type = TypeGenerator::fromTypeString($type);
172+
if (is_array($type) && !isset($type['name'])) {
173+
throw new Exception\InvalidArgumentException(
174+
'Type generator requires that a name is provided for this object'
175+
);
176+
}
172177

173-
return $this;
178+
if (is_array($type)) {
179+
$this->type = TypeGenerator::fromTypeString($type['name'], true);
180+
return $this;
181+
}
182+
183+
if (is_string($type)) {
184+
$this->type = TypeGenerator::fromTypeString($type);
185+
return $this;
186+
}
187+
188+
throw new Exception\InvalidArgumentException(
189+
'Unknown parameter type passed in'
190+
);
174191
}
175192

176193
/**

src/Generator/TypeGenerator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class TypeGenerator implements GeneratorInterface
2323
*/
2424
private $type;
2525

26+
/**
27+
* @var string;
28+
*/
29+
private $aliased = false;
30+
2631
/**
2732
* @var string[]
2833
*
@@ -44,7 +49,7 @@ final class TypeGenerator implements GeneratorInterface
4449
*
4550
* @throws InvalidArgumentException
4651
*/
47-
public static function fromTypeString($type)
52+
public static function fromTypeString($type, $aliased = false)
4853
{
4954
list($wasTrimmed, $trimmedType) = self::trimType($type);
5055

@@ -69,6 +74,7 @@ public static function fromTypeString($type)
6974

7075
$instance->type = $trimmedType;
7176
$instance->isInternalPhpType = self::isInternalPhpType($trimmedType);
77+
$instance->aliased = (boolean) $aliased;
7278

7379
return $instance;
7480
}
@@ -86,7 +92,7 @@ public function generate()
8692
return strtolower($this->type);
8793
}
8894

89-
return '\\' . $this->type;
95+
return ($this->aliased) ? $this->type : '\\' . $this->type;
9096
}
9197

9298
/**

test/Generator/TypeGeneratorTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,87 @@ public function testRejectsInvalidTypeString(string $typeString)
7979
TypeGenerator::fromTypeString($typeString);
8080
}
8181

82+
/**
83+
* @dataProvider validTypeArrayProvider
84+
*
85+
* @param string $typeArray
86+
* @param string $expectedReturnType
87+
*/
88+
public function testFromValidStringAlias(array $typeArray, string $expectedReturnType)
89+
{
90+
$generator = TypeGenerator::fromTypeString($typeArray['name'], $typeArray['alias']);
91+
92+
self::assertSame($expectedReturnType, $generator->generate());
93+
}
94+
95+
public function validTypeArrayProvider()
96+
{
97+
return [
98+
[['name' => 'foo', 'alias' => true], 'foo'],
99+
[['name' => 'foo', 'alias' => false], '\\foo'],
100+
[['name' => '\\foo', 'alias' => false], '\\foo'],
101+
[['name' => '\\foo', 'alias' => true], 'foo'],
102+
[['name' => 'a\\b\\c', 'alias' => false], '\\a\\b\\c'],
103+
[['name' => 'a\\b\\c', 'alias' => true], 'a\\b\\c'],
104+
[['name' => 'array', 'alias' => false], 'array'],
105+
[['name' => 'array', 'alias' => true], 'array'],
106+
[['name' => 'Array', 'alias' => false], 'array'],
107+
[['name' => 'Array', 'alias' => true], 'array'],
108+
[['name' => 'ARRAY', 'alias' => false], 'array'],
109+
[['name' => 'ARRAY', 'alias' => true], 'array'],
110+
[['name' => 'callable', 'alias' => false], 'callable'],
111+
[['name' => 'callable', 'alias' => true], 'callable'],
112+
[['name' => 'Callable', 'alias' => false], 'callable'],
113+
[['name' => 'Callable', 'alias' => true], 'callable'],
114+
[['name' => 'CALLABLE', 'alias' => false], 'callable'],
115+
[['name' => 'CALLABLE', 'alias' => true], 'callable'],
116+
[['name' => 'string', 'alias' => false], 'string'],
117+
[['name' => 'string', 'alias' => true], 'string'],
118+
[['name' => 'String', 'alias' => false], 'string'],
119+
[['name' => 'String', 'alias' => true], 'string'],
120+
[['name' => 'STRING', 'alias' => false], 'string'],
121+
[['name' => 'STRING', 'alias' => true], 'string'],
122+
[['name' => 'int', 'alias' => false], 'int'],
123+
[['name' => 'int', 'alias' => true], 'int'],
124+
[['name' => 'Int', 'alias' => false], 'int'],
125+
[['name' => 'Int', 'alias' => true], 'int'],
126+
[['name' => 'INT', 'alias' => false], 'int'],
127+
[['name' => 'INT', 'alias' => true], 'int'],
128+
[['name' => 'float', 'alias' => false], 'float'],
129+
[['name' => 'float', 'alias' => true], 'float'],
130+
[['name' => 'Float', 'alias' => false], 'float'],
131+
[['name' => 'Float', 'alias' => true], 'float'],
132+
[['name' => 'FLOAT', 'alias' => false], 'float'],
133+
[['name' => 'FLOAT', 'alias' => true], 'float'],
134+
[['name' => 'bool', 'alias' => false], 'bool'],
135+
[['name' => 'bool', 'alias' => true], 'bool'],
136+
[['name' => 'Bool', 'alias' => false], 'bool'],
137+
[['name' => 'Bool', 'alias' => true], 'bool'],
138+
[['name' => 'BOOL', 'alias' => false], 'bool'],
139+
[['name' => 'BOOL', 'alias' => true], 'bool'],
140+
[['name' => 'object', 'alias' => false], '\\object'],
141+
[['name' => 'object', 'alias' => true], 'object'],
142+
[['name' => 'Object', 'alias' => false], '\\Object'],
143+
[['name' => 'Object', 'alias' => true], 'Object'],
144+
[['name' => 'OBJECT', 'alias' => false], '\\OBJECT'],
145+
[['name' => 'OBJECT', 'alias' => true], 'OBJECT'],
146+
[['name' => 'mixed', 'alias' => false], '\\mixed'],
147+
[['name' => 'mixed', 'alias' => true], 'mixed'],
148+
[['name' => 'Mixed', 'alias' => false], '\\Mixed'],
149+
[['name' => 'Mixed', 'alias' => true], 'Mixed'],
150+
[['name' => 'MIXED', 'alias' => false], '\\MIXED'],
151+
[['name' => 'MIXED', 'alias' => true], 'MIXED'],
152+
[['name' => 'resource', 'alias' => false], '\\resource'],
153+
[['name' => 'resource', 'alias' => true], 'resource'],
154+
[['name' => 'Resource', 'alias' => false], '\\Resource'],
155+
[['name' => 'Resource', 'alias' => true], 'Resource'],
156+
[['name' => 'RESOURCE', 'alias' => false], '\\RESOURCE'],
157+
[['name' => 'RESOURCE', 'alias' => true], 'RESOURCE'],
158+
[['name' => 'foo_bar', 'alias' => false], '\\foo_bar'],
159+
[['name' => 'foo_bar', 'alias' => true], 'foo_bar'],
160+
];
161+
}
162+
82163
/**
83164
* @return string[][]
84165
*/

0 commit comments

Comments
 (0)