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

Commit 1cf4d81

Browse files
committed
Merge branch 'hotfix/#4-allow-class-constants-with-all-possible-scalar-values' into develop
Close #4 Forward port #4
2 parents b17e072 + 2a14807 commit 1cf4d81

File tree

2 files changed

+94
-16
lines changed

2 files changed

+94
-16
lines changed

src/Generator/ClassGenerator.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,28 +510,27 @@ public function addConstantFromGenerator(PropertyGenerator $constant)
510510
/**
511511
* Add Constant
512512
*
513-
* @param string $name
514-
* @param string $value
513+
* @param string $name non-empty string
514+
* @param string|int|null|float|array $value scalar
515+
*
515516
* @throws Exception\InvalidArgumentException
517+
*
516518
* @return ClassGenerator
517519
*/
518520
public function addConstant($name, $value)
519521
{
520-
if (!is_string($name)) {
522+
if (empty($name) || !is_string($name)) {
521523
throw new Exception\InvalidArgumentException(sprintf(
522524
'%s expects string for name',
523525
__METHOD__
524526
));
525527
}
526528

527-
if (empty($value) || !is_string($value)) {
528-
throw new Exception\InvalidArgumentException(sprintf(
529-
'%s expects value for constant, value must be a string',
530-
__METHOD__
531-
));
532-
}
529+
$this->validateConstantValue($value);
533530

534-
return $this->addConstantFromGenerator(new PropertyGenerator($name, $value, PropertyGenerator::FLAG_CONSTANT));
531+
return $this->addConstantFromGenerator(
532+
new PropertyGenerator($name, new PropertyValueGenerator($value), PropertyGenerator::FLAG_CONSTANT)
533+
);
535534
}
536535

537536
/**
@@ -984,4 +983,29 @@ public function generate()
984983

985984
return $output;
986985
}
986+
987+
/**
988+
* @param mixed $value
989+
*
990+
* @return void
991+
*
992+
* @throws Exception\InvalidArgumentException
993+
*/
994+
private function validateConstantValue($value)
995+
{
996+
if (null === $value || is_scalar($value)) {
997+
return;
998+
}
999+
1000+
if (is_array($value)) {
1001+
array_walk($value, [$this, 'validateConstantValue']);
1002+
1003+
return;
1004+
}
1005+
1006+
throw new Exception\InvalidArgumentException(sprintf(
1007+
'Expected value for constant, value must be a "scalar" or "null", "%s" found',
1008+
gettype($value)
1009+
));
1010+
}
9871011
}

test/Generator/ClassGeneratorTest.php

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ReflectionMethod;
1313
use Zend\Code\Generator\ClassGenerator;
1414
use Zend\Code\Generator\DocBlockGenerator;
15+
use Zend\Code\Generator\Exception\InvalidArgumentException;
1516
use Zend\Code\Generator\PropertyGenerator;
1617
use Zend\Code\Generator\MethodGenerator;
1718
use Zend\Code\Reflection\ClassReflection;
@@ -563,16 +564,69 @@ public function testAddConstantThrowsExceptionWithInvalidName()
563564
$classGenerator->addConstant([], 'value1');
564565
}
565566

566-
/**
567-
* @group 6274
568-
*/
569-
public function testAddConstantThrowsExceptionWithInvalidValue()
567+
public function testAddConstantThrowsExceptionWithEmptyConstantName()
570568
{
571-
$this->setExpectedException('InvalidArgumentException');
569+
$classGenerator = new ClassGenerator();
570+
571+
$this->setExpectedException(InvalidArgumentException::class);
572+
573+
$classGenerator->addConstant('', 'value');
574+
}
575+
576+
public function testAddConstantAcceptsMixedScalars()
577+
{
578+
$classGenerator = new ClassGenerator();
579+
580+
$classGenerator->addConstant('a', 'v');
581+
$classGenerator->addConstant('b', 123);
582+
$classGenerator->addConstant('c', 123.456);
583+
$classGenerator->addConstant('d', []);
584+
$classGenerator->addConstant('e', ['v1' => 'v2']);
585+
$classGenerator->addConstant('f', ['v1' => ['v2' => 'v3']]);
586+
$classGenerator->addConstant('g', null);
587+
588+
$this->assertEquals('v', $classGenerator->getConstant('a')->getDefaultValue()->getValue());
589+
$this->assertEquals(123, $classGenerator->getConstant('b')->getDefaultValue()->getValue());
590+
$this->assertEquals(123.456, $classGenerator->getConstant('c')->getDefaultValue()->getValue());
591+
$this->assertEquals([], $classGenerator->getConstant('d')->getDefaultValue()->getValue());
592+
$this->assertEquals(['v1' => 'v2'], $classGenerator->getConstant('e')->getDefaultValue()->getValue());
593+
$this->assertEquals(['v1' => ['v2' => 'v3']], $classGenerator->getConstant('f')->getDefaultValue()->getValue());
594+
$this->assertEquals(null, $classGenerator->getConstant('g')->getDefaultValue()->getValue());
595+
}
596+
597+
public function testAddConstantRejectsObjectConstantValue()
598+
{
599+
$classGenerator = new ClassGenerator();
600+
601+
$this->setExpectedException(InvalidArgumentException::class);
602+
603+
$classGenerator->addConstant('a', new \stdClass());
604+
}
572605

606+
public function testAddConstantRejectsResourceConstantValue()
607+
{
573608
$classGenerator = new ClassGenerator();
574609

575-
$classGenerator->addConstant('x', null);
610+
$resource = fopen('php://memory', 'r');
611+
612+
try {
613+
$classGenerator->addConstant('a', $resource);
614+
615+
$this->fail('Not supposed to be reached');
616+
} catch (InvalidArgumentException $e) {
617+
$this->assertEmpty($classGenerator->getConstants());
618+
} finally {
619+
fclose($resource);
620+
}
621+
}
622+
623+
public function testAddConstantRejectsArrayWithInvalidNestedValue()
624+
{
625+
$classGenerator = new ClassGenerator();
626+
627+
$this->setExpectedException(InvalidArgumentException::class);
628+
629+
$classGenerator->addConstant('a', [new \stdClass()]);
576630
}
577631

578632
/**

0 commit comments

Comments
 (0)