Skip to content

Commit

Permalink
minor #28910 [Serializer] Improve perf a bit by not using a signaling…
Browse files Browse the repository at this point in the history
… exception when not needed (nicolas-grekas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Serializer] Improve perf a bit by not using a signaling exception when not needed

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

And disabling unneeded autoload calls for interface_exists checks meanwhile.

Commits
-------

2a2914e [Serializer] Improve perf a bit by not using a signaling exception when not needed
  • Loading branch information
nicolas-grekas committed Oct 18, 2018
2 parents dd432c4 + 2a2914e commit bcff647
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 14 deletions.
Expand Up @@ -285,7 +285,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->prototype('scalar')
->cannotBeEmpty()
->validate()
->ifTrue(function ($v) { return !class_exists($v) && !interface_exists($v); })
->ifTrue(function ($v) { return !class_exists($v) && !interface_exists($v, false); })
->thenInvalid('The supported class or interface "%s" does not exist.')
->end()
->end()
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormConfigBuilder.php
Expand Up @@ -179,7 +179,7 @@ public function __construct($name, ?string $dataClass, EventDispatcherInterface
{
self::validateName($name);

if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass)) {
if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass, false)) {
throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass));
}

Expand Down
Expand Up @@ -131,7 +131,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
$method = $method['method'] ?? '__invoke';
}

if (!\class_exists($messageClass) && !\interface_exists($messageClass)) {
if (!\class_exists($messageClass) && !\interface_exists($messageClass, false)) {
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);

throw new RuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
Expand Down
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Serializer\Mapping\Factory;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;

Expand Down Expand Up @@ -70,14 +69,6 @@ public function getMetadataFor($value)
*/
public function hasMetadataFor($value)
{
try {
$this->getClass($value);

return true;
} catch (InvalidArgumentException $invalidArgumentException) {
// Return false in case of exception
}

return false;
return \is_object($value) || (\is_string($value) && (\class_exists($value) || \interface_exists($value, false)));
}
}
Expand Up @@ -34,7 +34,7 @@ trait ClassResolverTrait
private function getClass($value)
{
if (\is_string($value)) {
if (!class_exists($value) && !interface_exists($value)) {
if (!class_exists($value) && !interface_exists($value, false)) {
throw new InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $value));
}

Expand Down

0 comments on commit bcff647

Please sign in to comment.