Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8014296

Browse files
committed
Merge branch 'feature/12' into develop
Close #12 Fixes #9
2 parents 1c2ee76 + ee5462f commit 8014296

File tree

3 files changed

+171
-64
lines changed

3 files changed

+171
-64
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#12](https://github.com/zendframework/zend-code/pull/12) adds the ability to
10+
generate arrays using either long/standard syntax (`array(...)`) or short
11+
syntax (`[...]`). This can be accomplished by setting the value type to
12+
`ValueGenerator::TYPE_ARRAY_SHORT` instead of using `TYPE_ARRAY`.
13+
Additionally, you can use `TYPE_ARRAY_LONG` instead of `TYPE_ARRAY`; the two
14+
constants are synonyms.
1015

1116
### Deprecated
1217

src/Generator/ValueGenerator.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ class ValueGenerator extends AbstractGenerator
1818
/**#@+
1919
* Constant values
2020
*/
21-
const TYPE_AUTO = 'auto';
22-
const TYPE_BOOLEAN = 'boolean';
23-
const TYPE_BOOL = 'bool';
24-
const TYPE_NUMBER = 'number';
25-
const TYPE_INTEGER = 'integer';
26-
const TYPE_INT = 'int';
27-
const TYPE_FLOAT = 'float';
28-
const TYPE_DOUBLE = 'double';
29-
const TYPE_STRING = 'string';
30-
const TYPE_ARRAY = 'array';
31-
const TYPE_CONSTANT = 'constant';
32-
const TYPE_NULL = 'null';
33-
const TYPE_OBJECT = 'object';
34-
const TYPE_OTHER = 'other';
21+
const TYPE_AUTO = 'auto';
22+
const TYPE_BOOLEAN = 'boolean';
23+
const TYPE_BOOL = 'bool';
24+
const TYPE_NUMBER = 'number';
25+
const TYPE_INTEGER = 'integer';
26+
const TYPE_INT = 'int';
27+
const TYPE_FLOAT = 'float';
28+
const TYPE_DOUBLE = 'double';
29+
const TYPE_STRING = 'string';
30+
const TYPE_ARRAY = 'array';
31+
const TYPE_ARRAY_SHORT = 'array_short';
32+
const TYPE_ARRAY_LONG = 'array_long';
33+
const TYPE_CONSTANT = 'constant';
34+
const TYPE_NULL = 'null';
35+
const TYPE_OBJECT = 'object';
36+
const TYPE_OTHER = 'other';
3537
/**#@-*/
3638

3739
const OUTPUT_MULTIPLE_LINE = 'multipleLine';
@@ -254,6 +256,8 @@ protected function getValidatedType($type)
254256
self::TYPE_DOUBLE,
255257
self::TYPE_STRING,
256258
self::TYPE_ARRAY,
259+
self::TYPE_ARRAY_SHORT,
260+
self::TYPE_ARRAY_LONG,
257261
self::TYPE_CONSTANT,
258262
self::TYPE_NULL,
259263
self::TYPE_OBJECT,
@@ -317,12 +321,21 @@ public function generate()
317321
$type = $this->getAutoDeterminedType($value);
318322
}
319323

320-
if ($type == self::TYPE_ARRAY) {
324+
$isArrayType = in_array($type, [self::TYPE_ARRAY, self::TYPE_ARRAY_LONG, self::TYPE_ARRAY_SHORT]);
325+
326+
if ($isArrayType) {
321327
foreach ($value as &$curValue) {
322328
if ($curValue instanceof self) {
323329
continue;
324330
}
325-
$curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
331+
332+
if (is_array($curValue)) {
333+
$newType = $type;
334+
} else {
335+
$newType = self::TYPE_AUTO;
336+
}
337+
338+
$curValue = new self($curValue, $newType, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
326339
}
327340
}
328341

@@ -348,7 +361,17 @@ public function generate()
348361
$output .= $value;
349362
break;
350363
case self::TYPE_ARRAY:
351-
$output .= 'array(';
364+
case self::TYPE_ARRAY_LONG:
365+
case self::TYPE_ARRAY_SHORT:
366+
if ($type == self::TYPE_ARRAY_SHORT) {
367+
$startArray = '[';
368+
$endArray = ']';
369+
} else {
370+
$startArray = 'array(';
371+
$endArray = ')';
372+
}
373+
374+
$output .= $startArray;
352375
if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
353376
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
354377
}
@@ -384,7 +407,7 @@ public function generate()
384407
}
385408
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
386409
}
387-
$output .= ')';
410+
$output .= $endArray;
388411
break;
389412
case self::TYPE_OTHER:
390413
default:

test/Generator/ValueGeneratorTest.php

Lines changed: 124 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -59,56 +59,63 @@ public function constantsTypeProvider()
5959
];
6060
}
6161

62-
public function testPropertyDefaultValueIsSettable()
62+
/**
63+
* @return array
64+
*/
65+
protected function generateArrayData($longOutput, $value)
6366
{
64-
$valueGenerator = new ValueGenerator();
65-
$valueGenerator->setValue('foo');
66-
$this->assertEquals('foo', $valueGenerator->getValue());
67-
}
67+
$shortOutput = str_replace(
68+
['array(', ')'],
69+
['[', ']'],
70+
$longOutput
71+
);
6872

69-
public function testPropertyDefaultValueCanHandleStrings()
70-
{
71-
$valueGenerator = new ValueGenerator();
72-
$valueGenerator->setValue('foo');
73-
$this->assertEquals('\'foo\'', $valueGenerator->generate());
73+
return [
74+
'auto' => [
75+
ValueGenerator::TYPE_AUTO, $value, $longOutput
76+
],
77+
'array' => [
78+
ValueGenerator::TYPE_ARRAY, $value, $longOutput
79+
],
80+
'array long' => [
81+
ValueGenerator::TYPE_ARRAY_LONG, $value, $longOutput
82+
],
83+
'array short' => [
84+
ValueGenerator::TYPE_ARRAY_SHORT, $value, $shortOutput
85+
],
86+
];
7487
}
7588

76-
public function testPropertyDefaultValueCanHandleArray()
89+
/**
90+
* Data provider for testPropertyDefaultValueCanHandleArray test
91+
*
92+
* @return array
93+
*/
94+
public function simpleArrayProvider()
7795
{
78-
$expectedSource = <<<EOS
96+
$value = ['foo'];
97+
98+
$longOutput = <<<EOS
7999
array(
80100
'foo',
81101
)
82102
EOS;
83103

84-
$valueGenerator = new ValueGenerator();
85-
$valueGenerator->setValue(['foo']);
86-
$this->assertEquals($expectedSource, $valueGenerator->generate());
87-
}
88-
89-
public function testPropertyDefaultValueCanHandleUnquotedString()
90-
{
91-
$valueGenerator = new ValueGenerator();
92-
$valueGenerator->setValue('PHP_EOL');
93-
$valueGenerator->setType('constant');
94-
$this->assertEquals('PHP_EOL', $valueGenerator->generate());
95-
96-
$valueGenerator = new ValueGenerator();
97-
$valueGenerator->setValue(5);
98-
$this->assertEquals('5', $valueGenerator->generate());
99-
100-
$valueGenerator = new ValueGenerator();
101-
$valueGenerator->setValue(5.25);
102-
$this->assertEquals('5.25', $valueGenerator->generate());
104+
return $this->generateArrayData($longOutput, $value);
103105
}
104106

105-
public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
107+
/**
108+
* Data provider for testPropertyDefaultValueCanHandleComplexArrayOfTypes test
109+
*
110+
* @return array
111+
*/
112+
public function complexArrayProvider()
106113
{
107-
$targetValue = [
114+
$value = [
108115
5,
109116
'one' => 1,
110117
'two' => '2',
111-
'constant1' => '__DIR__ . \'/anydir1/anydir2\'',
118+
'constant1' => "__DIR__ . '/anydir1/anydir2'",
112119
[
113120
'baz' => true,
114121
'foo',
@@ -122,7 +129,7 @@ public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
122129
new ValueGenerator('PHP_EOL', 'constant')
123130
];
124131

125-
$expectedSource = <<<EOS
132+
$longOutput = <<<EOS
126133
array(
127134
5,
128135
'one' => 1,
@@ -142,14 +149,15 @@ public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
142149
)
143150
EOS;
144151

145-
$valueGenerator = new ValueGenerator();
146-
$valueGenerator->initEnvironmentConstants();
147-
$valueGenerator->setValue($targetValue);
148-
$generatedTargetSource = $valueGenerator->generate();
149-
$this->assertEquals($expectedSource, $generatedTargetSource);
152+
return $this->generateArrayData($longOutput, $value);
150153
}
151154

152-
public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
155+
/**
156+
* Data provider for testPropertyDefaultValueCanHandleArrayWithUnsortedKeys test
157+
*
158+
* @return array
159+
*/
160+
public function unsortedKeysArrayProvider()
153161
{
154162
$value = [
155163
1 => 'a',
@@ -159,9 +167,7 @@ public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
159167
3 => 'e'
160168
];
161169

162-
$valueGenerator = new ValueGenerator();
163-
$valueGenerator->setValue($value);
164-
$expectedSource = <<<EOS
170+
$longOutput = <<<EOS
165171
array(
166172
1 => 'a',
167173
0 => 'b',
@@ -171,7 +177,80 @@ public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
171177
)
172178
EOS;
173179

174-
$this->assertEquals($expectedSource, $valueGenerator->generate());
180+
return $this->generateArrayData($longOutput, $value);
181+
}
182+
183+
/**
184+
* @dataProvider unsortedKeysArrayProvider
185+
*/
186+
public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys($type, $value, $expected)
187+
{
188+
$valueGenerator = new ValueGenerator();
189+
$valueGenerator->setType($type);
190+
$valueGenerator->setValue($value);
191+
192+
$this->assertEquals($expected, $valueGenerator->generate());
193+
}
194+
195+
public function testPropertyDefaultValueConstructor()
196+
{
197+
$valueGenerator = new ValueGenerator();
198+
$this->isInstanceOf($valueGenerator, 'Zend\Code\Generator\ValueGenerator');
199+
}
200+
201+
public function testPropertyDefaultValueIsSettable()
202+
{
203+
$valueGenerator = new ValueGenerator();
204+
$valueGenerator->setValue('foo');
205+
$this->assertEquals('foo', $valueGenerator->getValue());
206+
}
207+
208+
public function testPropertyDefaultValueCanHandleStrings()
209+
{
210+
$valueGenerator = new ValueGenerator();
211+
$valueGenerator->setValue('foo');
212+
$this->assertEquals("'foo'", $valueGenerator->generate());
213+
}
214+
215+
/**
216+
* @dataProvider simpleArrayProvider
217+
*/
218+
public function testPropertyDefaultValueCanHandleArray($type, $value, $expected)
219+
{
220+
$valueGenerator = new ValueGenerator();
221+
$valueGenerator->setType($type);
222+
$valueGenerator->setValue($value);
223+
224+
$this->assertEquals($expected, $valueGenerator->generate());
225+
}
226+
227+
public function testPropertyDefaultValueCanHandleUnquotedString()
228+
{
229+
$valueGenerator = new ValueGenerator();
230+
$valueGenerator->setValue('PHP_EOL');
231+
$valueGenerator->setType('constant');
232+
$this->assertEquals('PHP_EOL', $valueGenerator->generate());
233+
234+
$valueGenerator = new ValueGenerator();
235+
$valueGenerator->setValue(5);
236+
$this->assertEquals('5', $valueGenerator->generate());
237+
238+
$valueGenerator = new ValueGenerator();
239+
$valueGenerator->setValue(5.25);
240+
$this->assertEquals('5.25', $valueGenerator->generate());
241+
}
242+
243+
/**
244+
* @dataProvider complexArrayProvider
245+
*/
246+
public function testPropertyDefaultValueCanHandleComplexArrayOfTypes($type, $value, $expected)
247+
{
248+
$valueGenerator = new ValueGenerator();
249+
$valueGenerator->initEnvironmentConstants();
250+
$valueGenerator->setType($type);
251+
$valueGenerator->setValue($value);
252+
253+
$this->assertEquals($expected, $valueGenerator->generate());
175254
}
176255

177256
/**

0 commit comments

Comments
 (0)