Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b82e56

Browse files
minor #29065 [OptionsResolver] Micro optimizations and simplifications (yceruto)
This PR was merged into the 4.2-dev branch. Discussion ---------- [OptionsResolver] Micro optimizations and simplifications | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29055 | License | MIT | Doc PR | - As we know, this component has a big impact on the workflow of the `Form` component. I'm a newcomer in Blackfire, so there could be other optimizations that I can't able to see, but here we go. For now, we've less code to maintain ![commit-changes](https://user-images.githubusercontent.com/2028198/47917083-c3619d00-de7e-11e8-826a-0c3009948d93.png) and a micro-optimizacion of performance. It'd be great if someone could try these changes in a real project to see their real value. ![orbf](https://user-images.githubusercontent.com/2028198/47915644-e2116500-de79-11e8-8648-a5e619fcd3eb.png) ![metrics](https://user-images.githubusercontent.com/2028198/47917598-61a23280-de80-11e8-9153-3ea60317f1a5.png) https://blackfire.io/profiles/compare/a04a13d3-7f60-4434-a2b8-0762efb8fbd6/graph https://github.com/yceruto/orbf The sample takes into account only the core extensions. Cheers! Commits ------- 24c2213 Optimizations and simplifications OMG Blackfire!
2 parents 605e2de + 24c2213 commit 1b82e56

File tree

1 file changed

+22
-79
lines changed

1 file changed

+22
-79
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 22 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ public function offsetGet($option/*, bool $triggerDeprecation = true*/)
800800
$triggerDeprecation = 1 === \func_num_args() || \func_get_arg(1);
801801

802802
// Shortcut for resolved options
803-
if (array_key_exists($option, $this->resolved)) {
803+
if (isset($this->resolved[$option]) || array_key_exists($option, $this->resolved)) {
804804
if ($triggerDeprecation && isset($this->deprecated[$option]) && (isset($this->given[$option]) || $this->calling) && \is_string($this->deprecated[$option])) {
805805
@trigger_error(strtr($this->deprecated[$option], array('%name%' => $option)), E_USER_DEPRECATED);
806806
}
@@ -809,7 +809,7 @@ public function offsetGet($option/*, bool $triggerDeprecation = true*/)
809809
}
810810

811811
// Check whether the option is set at all
812-
if (!array_key_exists($option, $this->defaults)) {
812+
if (!isset($this->defaults[$option]) && !array_key_exists($option, $this->defaults)) {
813813
if (!isset($this->defined[$option])) {
814814
throw new NoSuchOptionException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $option, implode('", "', array_keys($this->defined))));
815815
}
@@ -827,7 +827,7 @@ public function offsetGet($option/*, bool $triggerDeprecation = true*/)
827827
}
828828

829829
if (!\is_array($value)) {
830-
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $option, $this->formatValue($value), $this->formatTypeOf($value, 'array')));
830+
throw new InvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $option, $this->formatValue($value), $this->formatTypeOf($value)));
831831
}
832832

833833
// The following section must be protected from cyclic calls.
@@ -872,7 +872,7 @@ public function offsetGet($option/*, bool $triggerDeprecation = true*/)
872872
$invalidTypes = array();
873873

874874
foreach ($this->allowedTypes[$option] as $type) {
875-
$type = isset(self::$typeAliases[$type]) ? self::$typeAliases[$type] : $type;
875+
$type = self::$typeAliases[$type] ?? $type;
876876

877877
if ($valid = $this->verifyTypes($type, $value, $invalidTypes)) {
878878
break;
@@ -987,58 +987,33 @@ public function offsetGet($option/*, bool $triggerDeprecation = true*/)
987987
return $value;
988988
}
989989

990-
private function verifyTypes(string $type, $value, array &$invalidTypes): bool
990+
private function verifyTypes(string $type, $value, array &$invalidTypes, int $level = 0): bool
991991
{
992992
if (\is_array($value) && '[]' === substr($type, -2)) {
993-
return $this->verifyArrayType($type, $value, $invalidTypes);
994-
}
995-
996-
if (self::isValueValidType($type, $value)) {
997-
return true;
998-
}
999-
1000-
if (!$invalidTypes) {
1001-
$invalidTypes[$this->formatTypeOf($value, null)] = true;
1002-
}
1003-
1004-
return false;
1005-
}
1006-
1007-
private function verifyArrayType(string $type, array $value, array &$invalidTypes, int $level = 0): bool
1008-
{
1009-
$type = substr($type, 0, -2);
1010-
1011-
$suffix = '[]';
1012-
while (\strlen($suffix) <= $level * 2) {
1013-
$suffix .= '[]';
1014-
}
1015-
1016-
if ('[]' === substr($type, -2)) {
1017-
$success = true;
1018-
foreach ($value as $item) {
1019-
if (!\is_array($item)) {
1020-
$invalidTypes[$this->formatTypeOf($item, null).$suffix] = true;
993+
$type = substr($type, 0, -2);
1021994

995+
foreach ($value as $val) {
996+
if (!$this->verifyTypes($type, $val, $invalidTypes, $level + 1)) {
1022997
return false;
1023998
}
1024-
1025-
if (!$this->verifyArrayType($type, $item, $invalidTypes, $level + 1)) {
1026-
$success = false;
1027-
}
1028999
}
10291000

1030-
return $success;
1001+
return true;
10311002
}
10321003

1033-
foreach ($value as $item) {
1034-
if (!self::isValueValidType($type, $item)) {
1035-
$invalidTypes[$this->formatTypeOf($item, $type).$suffix] = $value;
1004+
if (('null' === $type && null === $value) || (\function_exists($func = 'is_'.$type) && $func($value)) || $value instanceof $type) {
1005+
return true;
1006+
}
10361007

1037-
return false;
1008+
if (!$invalidTypes) {
1009+
$suffix = '';
1010+
while (\strlen($suffix) < $level * 2) {
1011+
$suffix .= '[]';
10381012
}
1013+
$invalidTypes[$this->formatTypeOf($value).$suffix] = true;
10391014
}
10401015

1041-
return true;
1016+
return false;
10421017
}
10431018

10441019
/**
@@ -1104,40 +1079,13 @@ public function count()
11041079
/**
11051080
* Returns a string representation of the type of the value.
11061081
*
1107-
* This method should be used if you pass the type of a value as
1108-
* message parameter to a constraint violation. Note that such
1109-
* parameters should usually not be included in messages aimed at
1110-
* non-technical people.
1111-
*
11121082
* @param mixed $value The value to return the type of
1083+
*
1084+
* @return string The type of the value
11131085
*/
1114-
private function formatTypeOf($value, ?string $type): string
1086+
private function formatTypeOf($value): string
11151087
{
1116-
$suffix = '';
1117-
1118-
if (null !== $type && '[]' === substr($type, -2)) {
1119-
$suffix = '[]';
1120-
$type = substr($type, 0, -2);
1121-
while ('[]' === substr($type, -2)) {
1122-
$type = substr($type, 0, -2);
1123-
$value = array_shift($value);
1124-
if (!\is_array($value)) {
1125-
break;
1126-
}
1127-
$suffix .= '[]';
1128-
}
1129-
1130-
if (\is_array($value)) {
1131-
$subTypes = array();
1132-
foreach ($value as $val) {
1133-
$subTypes[$this->formatTypeOf($val, null)] = true;
1134-
}
1135-
1136-
return implode('|', array_keys($subTypes)).$suffix;
1137-
}
1138-
}
1139-
1140-
return (\is_object($value) ? \get_class($value) : \gettype($value)).$suffix;
1088+
return \is_object($value) ? \get_class($value) : \gettype($value);
11411089
}
11421090

11431091
/**
@@ -1198,9 +1146,4 @@ private function formatValues(array $values): string
11981146

11991147
return implode(', ', $values);
12001148
}
1201-
1202-
private static function isValueValidType(string $type, $value): bool
1203-
{
1204-
return (\function_exists($isFunction = 'is_'.$type) && $isFunction($value)) || $value instanceof $type;
1205-
}
12061149
}

0 commit comments

Comments
 (0)