8000 Emulate PHP 7's \TypeError · symfony/symfony@799ab29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 799ab29

Browse files
committed
Emulate PHP 7's \TypeError
1 parent bd210f9 commit 799ab29

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,22 +586,15 @@ private function writeProperty(&$object, $property, $value)
586586
* @throws \Exception
587587
*/
588588
private function callMethod($object, $method, $value) {
589-
// Cannot use class_exists because the PHP 7 polyfill defines \TypeError
590589
if (PHP_MAJOR_VERSION >= 7) {
591-
// PHP 7
592-
try {
593-
$object->{$method}($value);
594-
} catch (\TypeError $e) {
595-
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
596-
}
597-
590+
$object->{$method}($value);
598591
return;
599592
}
600593

601-
// PHP 5
602-
set_error_handler(function ($errno, $errstr) {
603-
if (E_RECOVERABLE_ERROR === $errno) {
604-
throw new InvalidArgumentException($errstr);
594+
// Emulates PHP 7 behavior
595+
set_error_handler(function ($errno, $errstr) use ($object, $method) {
596+
if (E_RECOVERABLE_ERROR === $errno && false !== strpos($errstr, sprintf('passed to %s::%s() must', get_class($object), $method))) {
597+
throw new \TypeError($errstr);
605598
}
606599

607600
return false;

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public function testIsWritableForReferenceChainIssue($object, $path, $value)
513513
}
514514

515515
/**
516-
* @expectedException InvalidArgumentException
516+
* @expectedException \TypeError
517517
*/
518518
public function testConvertTypeErrorToInvalidArgumentException()
519519
{

src/Symfony/Component/PropertyAccess/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.5.9"
19+
"php": ">=5.5.9",
20+
"symfony/polyfill-php70": "~1.0"
2021
},
2122
"autoload": {
2223
"psr-4": { "Symfony\\Component\\PropertyAccess\\": "" },

0 commit comments

Comments
 (0)
0