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

Allow arrays as constant values in generated constants #94

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ public function isValidConstantType()
$type = $this->type;
}

// valid types for constants
$scalarTypes = [
$validConstantTypes = [
self::TYPE_ARRAY,
self::TYPE_ARRAY_LONG,
self::TYPE_ARRAY_SHORT,
self::TYPE_BOOLEAN,
self::TYPE_BOOL,
self::TYPE_NUMBER,
Expand All @@ -186,7 +188,7 @@ public function isValidConstantType()
self::TYPE_NULL
];

return in_array($type, $scalarTypes);
return in_array($type, $validConstantTypes);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use ArrayAccess;
use ArrayObject as SplArrayObject;
use Zend\Code\Exception\InvalidArgumentException;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\PropertyValueGenerator;
use Zend\Stdlib\ArrayObject as StdlibArrayObject;
use Zend\Code\Generator\ValueGenerator;

Expand Down Expand Up @@ -64,6 +66,34 @@ public function constantsTypeProvider()
];
}

/**
* @dataProvider validConstantTypesProvider
*/
public function testValidConstantTypes($generator, $expectedOutput)
{
$propertyGenerator = new PropertyGenerator('FOO', $generator);
$propertyGenerator->setConst(true);
$this->assertSame($expectedOutput, $propertyGenerator->generate());
}

public function validConstantTypesProvider()
{
return [
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"],
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_LONG, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"],
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_SHORT, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = [];"],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL), " const FOO = true;"],
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN), " const FOO = true;"],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT), " const FOO = 1;"],
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER), " const FOO = 1;"],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE), " const FOO = 0.1;"],
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT), " const FOO = 0.1;"],
[new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING), " const FOO = 'bar';"],
[new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL), " const FOO = null;"],
[new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT), " const FOO = PHP_EOL;"],
];
}

/**
* @return array
*/
Expand Down