[Form] Fixed YODA issues

This commit is contained in:
Bernhard Schussek 2012-01-28 13:37:24 +01:00
parent 600cec746a
commit 8dc78bd0c9
13 changed files with 41 additions and 33 deletions

View File

@ -20,7 +20,7 @@ use Symfony\Component\Form\Extension\Core\View\ChoiceView;
/** /**
* Base class for choice list implementations. * Base class for choice list implementations.
* *
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.<com>
*/ */
class ChoiceList implements ChoiceListInterface class ChoiceList implements ChoiceListInterface
{ {
@ -530,7 +530,11 @@ class ChoiceList implements ChoiceListInterface
*/ */
protected function fixValues(array $values) 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) protected function fixIndices(array $indices)
{ {
return array_map(array($this, 'fixIndex'), $indices); foreach ($indices as $i => $index) {
$indices[$i] = $this->fixIndex($index);
}
return $indices;
} }
/** /**

View File

@ -105,7 +105,7 @@ class ObjectChoiceList extends ChoiceList
throw new UnexpectedTypeException($choices, 'array or \Traversable'); throw new UnexpectedTypeException($choices, 'array or \Traversable');
} }
if ($this->groupPath !== null) { if (null !== $this->groupPath) {
$groupedChoices = array(); $groupedChoices = array();
foreach ($choices as $i => $choice) { foreach ($choices as $i => $choice) {

View File

@ -59,7 +59,7 @@ class PropertyPathMapper implements DataMapperInterface
public function mapDataToForm($data, FormInterface $form) public function mapDataToForm($data, FormInterface $form)
{ {
if (!empty($data)) { if (!empty($data)) {
if ($form->getAttribute('property_path') !== null) { if (null !== $form->getAttribute('property_path')) {
$form->setData($form->getAttribute('property_path')->getValue($data)); $form->setData($form->getAttribute('property_path')->getValue($data));
} }
} }
@ -77,7 +77,7 @@ class PropertyPathMapper implements DataMapperInterface
public function mapFormToData(FormInterface $form, &$data) 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'); $propertyPath = $form->getAttribute('property_path');
// If the data is identical to the value in $data, we are // If the data is identical to the value in $data, we are

View File

@ -46,18 +46,18 @@ class ChoiceToValueTransformer implements DataTransformerInterface
// These are now valid ChoiceList values, so we can return null // These are now valid ChoiceList values, so we can return null
// right away // right away
if ($value === '' || $value === null) { if ('' === $value || null === $value) {
return null; return null;
} }
$choices = $this->choiceList->getChoicesForValues(array($value)); $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'); throw new TransformationFailedException('The choice "' . $value . '" does not exist or is not unique');
} }
$choice = current($choices); $choice = current($choices);
return $choice === '' ? null : $choice; return '' === $choice ? null : $choice;
} }
} }

View File

@ -124,7 +124,7 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
throw new UnexpectedTypeException($value, 'array'); throw new UnexpectedTypeException($value, 'array');
} }
if (implode('', $value) === '') { if ('' === implode('', $value)) {
return null; return null;
} }

View File

@ -122,7 +122,7 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
{ {
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); $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::FRACTION_DIGITS, $this->precision);
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
} }

View File

@ -41,7 +41,7 @@ class FixRadioInputListener implements EventSubscriberInterface
$value = $event->getData(); $value = $event->getData();
$index = current($this->choiceList->getIndicesForValues(array($value))); $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() static public function getSubscribedEvents()

View File

@ -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')); 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)); $builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format));
} else { } else {
// Only pass a subset of the options to children // 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( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format) new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format)
)); ));
} elseif ($options['input'] === 'timestamp') { } elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])
)); ));
} elseif ($options['input'] === 'array') { } elseif ('array' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts) new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts)
)); ));
@ -200,7 +200,7 @@ class DateTimeType extends AbstractType
*/ */
public function getParent(array $options) public function getParent(array $options)
{ {
return $options['widget'] === 'single_text' ? 'field' : 'form'; return 'single_text' === $options['widget'] ? 'field' : 'form';
} }
/** /**

View File

@ -63,12 +63,12 @@ class DateType extends AbstractType
$pattern $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)); $builder->appendClientTransformer(new DateTimeToLocalizedStringTransformer($options['data_timezone'], $options['user_timezone'], $format, \IntlDateFormatter::NONE, \IntlDateFormatter::GREGORIAN, $pattern));
} else { } else {
$yearOptions = $monthOptions = $dayOptions = array(); $yearOptions = $monthOptions = $dayOptions = array();
if ($options['widget'] === 'choice') { if ('choice' === $options['widget']) {
if (is_array($options['empty_value'])) { if (is_array($options['empty_value'])) {
$options['empty_value'] = array_merge(array('year' => null, 'month' => null, 'day' => null), $options['empty_value']); $options['empty_value'] = array_merge(array('year' => null, 'month' => null, 'day' => null), $options['empty_value']);
} else { } else {
@ -123,15 +123,15 @@ class DateType extends AbstractType
; ;
} }
if ($options['input'] === 'string') { if ('string' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d') new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'Y-m-d')
)); ));
} elseif ($options['input'] === 'timestamp') { } elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])
)); ));
} elseif ($options['input'] === 'array') { } elseif ('array' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], array('year', 'month', 'day')) 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) public function getParent(array $options)
{ {
return $options['widget'] === 'single_text' ? 'field' : 'form'; return 'single_text' === $options['widget'] ? 'field' : 'form';
} }
/** /**

View File

@ -35,12 +35,12 @@ class TimeType extends AbstractType
$parts[] = 'second'; $parts[] = 'second';
} }
if ($options['widget'] === 'single_text') { if ('single_text' === $options['widget']) {
$builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format)); $builder->appendClientTransformer(new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format));
} else { } else {
$hourOptions = $minuteOptions = $secondOptions = array(); $hourOptions = $minuteOptions = $secondOptions = array();
if ($options['widget'] === 'choice') { if ('choice' === $options['widget']) {
if (is_array($options['empty_value'])) { if (is_array($options['empty_value'])) {
$options['empty_value'] = array_merge(array('hour' => null, 'minute' => null, 'second' => null), $options['empty_value']); $options['empty_value'] = array_merge(array('hour' => null, 'minute' => null, 'second' => null), $options['empty_value']);
} else { } else {
@ -103,18 +103,18 @@ class TimeType extends AbstractType
$builder->add('second', $options['widget'], $secondOptions); $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( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format) new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format)
)); ));
} elseif ($options['input'] === 'timestamp') { } elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone']) new DateTimeToTimestampTransformer($options['data_timezone'], $options['data_timezone'])
)); ));
} elseif ($options['input'] === 'array') { } elseif ('array' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer( $builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts) new DateTimeToArrayTransformer($options['data_timezone'], $options['data_timezone'], $parts)
)); ));
@ -184,7 +184,7 @@ class TimeType extends AbstractType
*/ */
public function getParent(array $options) public function getParent(array $options)
{ {
return $options['widget'] === 'single_text' ? 'field' : 'form'; return 'single_text' === $options['widget'] ? 'field' : 'form';
} }
/** /**

View File

@ -229,7 +229,7 @@ class DelegatingValidator implements FormValidatorInterface
$nestedNamePath = $namePath.'.'.$child->getName(); $nestedNamePath = $namePath.'.'.$child->getName();
if (strpos($path, '[') === 0) { if (0 === strpos($path, '[')) {
$nestedDataPaths = array($dataPath.$path); $nestedDataPaths = array($dataPath.$path);
} else { } else {
$nestedDataPaths = array($dataPath.'.'.$path); $nestedDataPaths = array($dataPath.'.'.$path);

View File

@ -1098,6 +1098,6 @@ class Form implements \IteratorAggregate, FormInterface
*/ */
static public function isValidName($name) 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);
} }
} }

View File

@ -67,7 +67,7 @@ class PropertyPath implements \IteratorAggregate
$pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/'; $pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/';
while (preg_match($pattern, $remaining, $matches)) { while (preg_match($pattern, $remaining, $matches)) {
if ($matches[2] !== '') { if ('' !== $matches[2]) {
$this->elements[] = $matches[2]; $this->elements[] = $matches[2];
$this->isIndex[] = false; $this->isIndex[] = false;
} else { } else {