From 8dc78bd0c9cdfef411f174776a56e72bed8bc049 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sat, 28 Jan 2012 13:37:24 +0100 Subject: [PATCH] [Form] Fixed YODA issues --- .../Form/Extension/Core/ChoiceList/ChoiceList.php | 14 +++++++++++--- .../Extension/Core/ChoiceList/ObjectChoiceList.php | 2 +- .../Core/DataMapper/PropertyPathMapper.php | 4 ++-- .../DataTransformer/ChoiceToValueTransformer.php | 6 +++--- .../DataTransformer/DateTimeToArrayTransformer.php | 2 +- .../NumberToLocalizedStringTransformer.php | 2 +- .../Core/EventListener/FixRadioInputListener.php | 2 +- .../Form/Extension/Core/Type/DateTimeType.php | 10 +++++----- .../Form/Extension/Core/Type/DateType.php | 12 ++++++------ .../Form/Extension/Core/Type/TimeType.php | 14 +++++++------- .../Validator/Validator/DelegatingValidator.php | 2 +- src/Symfony/Component/Form/Form.php | 2 +- src/Symfony/Component/Form/Util/PropertyPath.php | 2 +- 13 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php index 374a65543a..311e0f7361 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php @@ -20,7 +20,7 @@ use Symfony\Component\Form\Extension\Core\View\ChoiceView; /** * Base class for choice list implementations. * - * @author Bernhard Schussek + * @author Bernhard Schussek */ class ChoiceList implements ChoiceListInterface { @@ -530,7 +530,11 @@ class ChoiceList implements ChoiceListInterface */ protected function fixValues(array $values) { - return array_map(array($this, 'fixValue'), $values); + foreach ($values as $i => $value) { + $values[$i] = $this->fixValue($value); + } + + return $values; } /** @@ -560,7 +564,11 @@ class ChoiceList implements ChoiceListInterface */ protected function fixIndices(array $indices) { - return array_map(array($this, 'fixIndex'), $indices); + foreach ($indices as $i => $index) { + $indices[$i] = $this->fixIndex($index); + } + + return $indices; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php index f5ea145437..3533326be1 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php @@ -105,7 +105,7 @@ class ObjectChoiceList extends ChoiceList throw new UnexpectedTypeException($choices, 'array or \Traversable'); } - if ($this->groupPath !== null) { + if (null !== $this->groupPath) { $groupedChoices = array(); foreach ($choices as $i => $choice) { diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php index 6652c3843e..e9c3c126e4 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php @@ -59,7 +59,7 @@ class PropertyPathMapper implements DataMapperInterface public function mapDataToForm($data, FormInterface $form) { if (!empty($data)) { - if ($form->getAttribute('property_path') !== null) { + if (null !== $form->getAttribute('property_path')) { $form->setData($form->getAttribute('property_path')->getValue($data)); } } @@ -77,7 +77,7 @@ class PropertyPathMapper implements DataMapperInterface public function mapFormToData(FormInterface $form, &$data) { - if ($form->getAttribute('property_path') !== null && $form->isSynchronized()) { + if (null !== $form->getAttribute('property_path') && $form->isSynchronized()) { $propertyPath = $form->getAttribute('property_path'); // If the data is identical to the value in $data, we are diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php index 13d90a9151..dfab6ec454 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php @@ -46,18 +46,18 @@ class ChoiceToValueTransformer implements DataTransformerInterface // These are now valid ChoiceList values, so we can return null // right away - if ($value === '' || $value === null) { + if ('' === $value || null === $value) { return null; } $choices = $this->choiceList->getChoicesForValues(array($value)); - if (count($choices) !== 1) { + if (1 !== count($choices)) { throw new TransformationFailedException('The choice "' . $value . '" does not exist or is not unique'); } $choice = current($choices); - return $choice === '' ? null : $choice; + return '' === $choice ? null : $choice; } } diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php index e9f5fb2f2a..fb0a3bf2dd 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php @@ -124,7 +124,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer throw new UnexpectedTypeException($value, 'array'); } - if (implode('', $value) === '') { + if ('' === implode('', $value)) { return null; } diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index 56f4c68bcf..699dec00ea 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -122,7 +122,7 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); - if ($this->precision !== null) { + if (null !== $this->precision) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); } diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php index e094a77a38..1378b2e2f7 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixRadioInputListener.php @@ -41,7 +41,7 @@ class FixRadioInputListener implements EventSubscriberInterface $value = $event->getData(); $index = current($this->choiceList->getIndicesForValues(array($value))); - $event->setData($index !== false ? array($index => $value) : array()); + $event->setData(false !== $index ? array($index => $value) : array()); } static public function getSubscribedEvents() diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php index 35fe0ffa29..1c63d21641 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php @@ -45,7 +45,7 @@ class DateTimeType extends AbstractType throw new FormException(sprintf('Options "date_widget" and "time_widget" need to be identical. Used: "date_widget" = "%s" and "time_widget" = "%s".', $options['date_widget'] ?: 'choice', $options['time_widget'] ?: 'choice')); } - if ($options['widget'] === 'single_text') { + if ('single_text' === $options['widget']) { $builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format)); } else { // Only pass a subset of the options to children @@ -105,15 +105,15 @@ class DateTimeType extends AbstractType ; } - if ($options['input'] === 'string') { + if ('string' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format) )); - } elseif ($options['input'] === 'timestamp') { + } elseif ('timestamp' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) )); - } elseif ($options['input'] === 'array') { + } elseif ('array' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts) )); @@ -200,7 +200,7 @@ class DateTimeType extends AbstractType */ public function getParent(array $options) { - return $options['widget'] === 'single_text' ? 'field' : 'form'; + return 'single_text' === $options['widget'] ? 'field' : 'form'; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index 9e878d6535..8c2af29e7f 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -63,12 +63,12 @@ class DateType extends AbstractType $pattern ); - if ($options['widget'] === 'single_text') { + if ('single_text' === $options['widget']) { $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern)); } else { $yearOptions = $monthOptions = $dayOptions = array(); - if ($options['widget'] === 'choice') { + if ('choice' === $options['widget']) { if (is_array($options['empty_value'])) { $options['empty_value'] = array_merge(array('year' => null, 'month' => null, 'day' => null), $options['empty_value']); } else { @@ -123,15 +123,15 @@ class DateType extends AbstractType ; } - if ($options['input'] === 'string') { + if ('string' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d') )); - } elseif ($options['input'] === 'timestamp') { + } elseif ('timestamp' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) )); - } elseif ($options['input'] === 'array') { + } elseif ('array' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')) )); @@ -212,7 +212,7 @@ class DateType extends AbstractType */ public function getParent(array $options) { - return $options['widget'] === 'single_text' ? 'field' : 'form'; + return 'single_text' === $options['widget'] ? 'field' : 'form'; } /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php index 1eeaa40ec5..7bfe50dc6b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php @@ -35,12 +35,12 @@ class TimeType extends AbstractType $parts[] = 'second'; } - if ($options['widget'] === 'single_text') { + if ('single_text' === $options['widget']) { $builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format)); } else { $hourOptions = $minuteOptions = $secondOptions = array(); - if ($options['widget'] === 'choice') { + if ('choice' === $options['widget']) { if (is_array($options['empty_value'])) { $options['empty_value'] = array_merge(array('hour' => null, 'minute' => null, 'second' => null), $options['empty_value']); } else { @@ -103,18 +103,18 @@ class TimeType extends AbstractType $builder->add('second', $options['widget'], $secondOptions); } - $builder->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], $parts, $options['widget'] === 'text')); + $builder->appendClientTransformer(new DateTimeToArrayTransformer($options['data_timezone'], $options['user_timezone'], $parts, 'text' === $options['widget'])); } - if ($options['input'] === 'string') { + if ('string' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format) )); - } elseif ($options['input'] === 'timestamp') { + } elseif ('timestamp' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) )); - } elseif ($options['input'] === 'array') { + } elseif ('array' === $options['input']) { $builder->appendNormTransformer(new ReversedTransformer( new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts) )); @@ -184,7 +184,7 @@ class TimeType extends AbstractType */ public function getParent(array $options) { - return $options['widget'] === 'single_text' ? 'field' : 'form'; + return 'single_text' === $options['widget'] ? 'field' : 'form'; } /** diff --git a/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php b/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php index 2b7c099403..dce76724c4 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php +++ b/src/Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php @@ -229,7 +229,7 @@ class DelegatingValidator implements FormValidatorInterface $nestedNamePath = $namePath.'.'.$child->getName(); - if (strpos($path, '[') === 0) { + if (0 === strpos($path, '[')) { $nestedDataPaths = array($dataPath.$path); } else { $nestedDataPaths = array($dataPath.'.'.$path); diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 5d5d8011ce..d82f2f08e3 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -1098,6 +1098,6 @@ class Form implements \IteratorAggregate, FormInterface */ static public function isValidName($name) { - return $name === '' || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name); + return '' === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name); } } diff --git a/src/Symfony/Component/Form/Util/PropertyPath.php b/src/Symfony/Component/Form/Util/PropertyPath.php index cc4626405a..96fa57f202 100644 --- a/src/Symfony/Component/Form/Util/PropertyPath.php +++ b/src/Symfony/Component/Form/Util/PropertyPath.php @@ -67,7 +67,7 @@ class PropertyPath implements \IteratorAggregate $pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/'; while (preg_match($pattern, $remaining, $matches)) { - if ($matches[2] !== '') { + if ('' !== $matches[2]) { $this->elements[] = $matches[2]; $this->isIndex[] = false; } else {