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

Commit 10a742a

Browse files
committed
Merge branch 'hotfix/179'
Close #179 Fix #178
2 parents a277be9 + 8db5b4e commit 10a742a

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#179](https://github.com/zendframework/zend-code/pull/179) fixes exception message when invalid value provided in `Zend\Code\Generator\ValueGenerator`.
2626

2727
## 3.4.0 - 2019-10-06
2828

src/Generator/ValueGenerator.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use function in_array;
2626
use function is_array;
2727
use function is_int;
28+
use function is_object;
2829
use function max;
2930
use function sprintf;
3031
use function str_repeat;
@@ -437,9 +438,10 @@ public function generate()
437438
break;
438439
case self::TYPE_OTHER:
439440
default:
440-
throw new Exception\RuntimeException(
441-
sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
442-
);
441+
throw new Exception\RuntimeException(sprintf(
442+
'Type "%s" is unknown or cannot be used as property default value.',
443+
is_object($value) ? get_class($value) : gettype($value)
444+
));
443445
}
444446

445447
return $output;

test/Generator/ValueGeneratorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
use ArrayAccess;
1313
use ArrayObject as SplArrayObject;
14+
use DateTime;
15+
use Generator;
1416
use PHPUnit\Framework\TestCase;
1517
use Zend\Code\Exception\InvalidArgumentException;
18+
use Zend\Code\Exception\RuntimeException;
1619
use Zend\Code\Generator\PropertyGenerator;
1720
use Zend\Code\Generator\PropertyValueGenerator;
1821
use Zend\Code\Generator\ValueGenerator;
@@ -462,4 +465,24 @@ public function getEscapedParameters()
462465
["\\'", "\\\\\\'"],
463466
];
464467
}
468+
469+
public function invalidValue() : Generator
470+
{
471+
yield 'object' => [new DateTime(), DateTime::class];
472+
yield 'resource' => [fopen('php://input', 'r'), 'resource'];
473+
}
474+
475+
/**
476+
* @dataProvider invalidValue
477+
*
478+
* @param mixed $value
479+
*/
480+
public function testExceptionInvalidValue($value, string $type) : void
481+
{
482+
$valueGenerator = new ValueGenerator($value);
483+
484+
$this->expectException(RuntimeException::class);
485+
$this->expectExceptionMessage('Type "'.$type.'" is unknown or cannot be used');
486+
$valueGenerator->generate();
487+
}
465488
}

0 commit comments

Comments
 (0)