8000 [PropertyAccess] Throw an InvalidArgumentException when the type do not match by dunglas · Pull Request #17738 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Throw an InvalidArgumentException when the type do not match #17738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Emulate PHP 7's \TypeError
  • Loading branch information
dunglas committed Feb 23, 2016
commit 799ab295fcbec93e791d8102661d5111fc08c2be
17 changes: 5 additions & 12 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,22 +586,15 @@ private function writeProperty(&$object, $property, $value)
* @throws \Exception
*/
private function callMethod($object, $method, $value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ should be on the next line

// Cannot use class_exists because the PHP 7 polyfill defines \TypeError
if (PHP_MAJOR_VERSION >= 7) {
// PHP 7
try {
$object->{$method}($value);
} catch (\TypeError $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}

$object->{$method}($value);
return;
}

// PHP 5
set_error_handler(function ($errno, $errstr) {
if (E_RECOVERABLE_ERROR === $errno) {
throw new InvalidArgumentException($errstr);
// Emulates PHP 7 behavior
set_error_handler(function ($errno, $errstr) use ($object, $method) {
if (E_RECOVERABLE_ERROR === $errno && false !== strpos($errstr, sprintf('passed to %s::%s() must', get_class($object), $method))) {
throw new \TypeError($errstr);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public function testIsWritableForReferenceChainIssue($object, $path, $value)
}

/**
* @expectedException InvalidArgumentException
* @expectedException \TypeError
*/
public function testConvertTypeErrorToInvalidArgumentException()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/PropertyAccess/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=5.5.9",
"symfony/polyfill-php70": "~1.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\PropertyAccess\\": "" },
Expand Down
0