Skip to content

Commit

Permalink
Enhanced the triggering of E_USER_DEPRECATED errors
Browse files Browse the repository at this point in the history
- Removed useless error handlers around FormEvent as the triggering has
  been fixed in it.
- Enhanced the triggering of deprecation errors for places where the BC
  method provide some user logic needing to be converted to a new way.
- Enhanced the deprecation messages to mention the replacement whenever
  possible.
  • Loading branch information
stof committed Jan 10, 2013
1 parent 2d5bede commit 5412a4e
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 97 deletions.
22 changes: 14 additions & 8 deletions AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ public function finishView(FormView $view, FormInterface $form, array $options)
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
$resolver->setDefaults($this->getDefaultOptions(array()));
$resolver->addAllowedValues($this->getAllowedOptionValues(array()));
restore_error_handler();
$defaults = $this->getDefaultOptions(array());
$allowedTypes = $this->getAllowedOptionValues(array());

if (!empty($defaults)) {
trigger_error('getDefaultOptions() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

$resolver->setDefaults($defaults);
}

if (!empty($allowedTypes)) {
trigger_error('getAllowedOptionValues() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

$resolver->addAllowedValues($allowedTypes);
}
}

/**
Expand All @@ -69,8 +79,6 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function getDefaultOptions(array $options)
{
trigger_error('getDefaultOptions() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

return array();
}

Expand All @@ -86,8 +94,6 @@ public function getDefaultOptions(array $options)
*/
public function getAllowedOptionValues(array $options)
{
trigger_error('getAllowedOptionValues() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

return array();
}

Expand Down
22 changes: 14 additions & 8 deletions AbstractTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,20 @@ public function finishView(FormView $view, FormInterface $form, array $options)
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
$resolver->setDefaults($this->getDefaultOptions());
$resolver->addAllowedValues($this->getAllowedOptionValues());
restore_error_handler();
$defaults = $this->getDefaultOptions(array());
$allowedTypes = $this->getAllowedOptionValues(array());

if (!empty($defaults)) {
trigger_error('getDefaultOptions() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

$resolver->setDefaults($defaults);
}

if (!empty($allowedTypes)) {
trigger_error('getAllowedOptionValues() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

$resolver->addAllowedValues($allowedTypes);
}
}

/**
Expand All @@ -60,8 +70,6 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function getDefaultOptions()
{
trigger_error('getDefaultOptions() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

return array();
}

Expand All @@ -75,8 +83,6 @@ public function getDefaultOptions()
*/
public function getAllowedOptionValues()
{
trigger_error('getAllowedOptionValues() is deprecated since version 2.1 and will be removed in 2.3. Use setDefaultOptions() instead.', E_USER_DEPRECATED);

return array();
}
}
2 changes: 1 addition & 1 deletion CallbackValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CallbackValidator implements FormValidatorInterface
*/
public function __construct($callback)
{
trigger_error('CallbackValidator is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
trigger_error('CallbackValidator is deprecated since version 2.1 and will be removed in 2.3. Use the FormEvents::POST_BIND event instead.', E_USER_DEPRECATED);

$this->callback = $callback;
}
Expand Down
23 changes: 15 additions & 8 deletions Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,12 @@ public function setData($modelData)

// Hook to change content of the data
if ($dispatcher->hasListeners(FormEvents::PRE_SET_DATA) || $dispatcher->hasListeners(FormEvents::SET_DATA)) {
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
$event = new FormEvent($this, $modelData);
restore_error_handler();
$dispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
// BC until 2.3
if ($dispatcher->hasListeners(FormEvents::SET_DATA)) {
trigger_error('The FormEvents::SET_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::PRE_SET_DATA event instead.', E_USER_DEPRECATED);
}
$dispatcher->dispatch(FormEvents::SET_DATA, $event);
$modelData = $event->getData();
}
Expand Down Expand Up @@ -532,11 +533,12 @@ public function bind($submittedData)

// Hook to change content of the data bound by the browser
if ($dispatcher->hasListeners(FormEvents::PRE_BIND) || $dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) {
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
$event = new FormEvent($this, $submittedData);
restore_error_handler();
$dispatcher->dispatch(FormEvents::PRE_BIND, $event);
// BC until 2.3
if ($dispatcher->hasListeners(FormEvents::BIND_CLIENT_DATA)) {
trigger_error('The FormEvents::BIND_CLIENT_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::PRE_BIND event instead.', E_USER_DEPRECATED);
}
$dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event);
$submittedData = $event->getData();
}
Expand Down Expand Up @@ -594,11 +596,12 @@ public function bind($submittedData)
// Hook to change content of the data into the normalized
// representation
if ($dispatcher->hasListeners(FormEvents::BIND) || $dispatcher->hasListeners(FormEvents::BIND_NORM_DATA)) {
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
$event = new FormEvent($this, $normData);
restore_error_handler();
$dispatcher->dispatch(FormEvents::BIND, $event);
// BC until 2.3
if ($dispatcher->hasListeners(FormEvents::BIND_NORM_DATA)) {
trigger_error('The FormEvents::BIND_NORM_DATA event is deprecated since 2.1 and will be removed in 2.3. Use the FormEvents::BIND event instead.', E_USER_DEPRECATED);
}
$dispatcher->dispatch(FormEvents::BIND_NORM_DATA, $event);
$normData = $event->getData();
}
Expand All @@ -621,10 +624,14 @@ public function bind($submittedData)
}

set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handleBC'));
foreach ($this->config->getValidators() as $validator) {
$validators = $this->config->getValidators();
restore_error_handler();

foreach ($validators as $validator) {
trigger_error(sprintf('FormConfigInterface::getValidators() is deprecated since 2.1 and will be removed in 2.3. Convert your %s class to a listener on the FormEvents::POST_BIND event.', get_class($validator)), E_USER_DEPRECATED);

$validator->validate($this);
}
restore_error_handler();

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ public function getIterator()
*/
public function getTypes()
{
trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);

if ($this->locked) {
throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}

trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);

$types = array();

for ($type = $this->getType(); null !== $type; $type = $type->getParent()) {
Expand Down
12 changes: 0 additions & 12 deletions Test/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Symfony\Component\Form\Test;

use Symfony\Component\Form\FormInterface as NonTestFormInterface;
use Symfony\Component\Form\FormEvent;

class DeprecationErrorHandler
{
public static function handle($errorNumber, $message, $file, $line, $context)
Expand All @@ -24,13 +21,4 @@ public static function handleBC($errorNumber, $message, $file, $line, $context)

return false;
}

public static function getFormEvent(NonTestFormInterface $form, $data)
{
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
$event = new FormEvent($form, $data);
restore_error_handler();

return $event;
}
}
4 changes: 1 addition & 3 deletions Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ public function testRemove()
$this->form->remove('foo');

$this->assertNull($child->getParent());
set_error_handler(array('Symfony\Component\Form\Test\DeprecationErrorHandler', 'handle'));
$this->assertFalse($this->form->hasChildren());
restore_error_handler();
$this->assertCount(0, $this->form);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Test\DeprecationErrorHandler;

class FixRadioInputListenerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public function testFixRadio()
{
$data = '1';
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$this->listener->preBind($event);

Expand All @@ -53,7 +53,7 @@ public function testFixZero()
{
$data = '0';
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$this->listener->preBind($event);

Expand All @@ -64,7 +64,7 @@ public function testIgnoreEmptyString()
{
$data = '';
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$this->listener->preBind($event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener;
use Symfony\Component\Form\Test\DeprecationErrorHandler;

class FixUrlProtocolListenerTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -27,7 +27,7 @@ public function testFixHttpUrl()
{
$data = "www.symfony.com";
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$filter = new FixUrlProtocolListener('http');
$filter->onBind($event);
Expand All @@ -39,7 +39,7 @@ public function testSkipKnownUrl()
{
$data = "http://www.symfony.com";
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$filter = new FixUrlProtocolListener('http');
$filter->onBind($event);
Expand All @@ -51,7 +51,7 @@ public function testSkipOtherProtocol()
{
$data = "ftp://www.symfony.com";
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$event = DeprecationErrorHandler::getFormEvent($form, $data);
$event = new FormEvent($form, $data);

$filter = new FixUrlProtocolListener('http');
$filter->onBind($event);
Expand Down
20 changes: 10 additions & 10 deletions Tests/Extension/Core/EventListener/MergeCollectionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Form\Tests\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
use Symfony\Component\Form\Test\DeprecationErrorHandler;

abstract class MergeCollectionListenerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public function testAddExtraEntriesIfAllowAdd($allowDelete)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

// The original object was modified
Expand All @@ -108,7 +108,7 @@ public function testAddExtraEntriesIfAllowAddDontOverwriteExistingIndices($allow

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

// The original object was modified
Expand All @@ -133,7 +133,7 @@ public function testDoNothingIfNotAllowAdd($allowDelete)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

// We still have the original object
Expand All @@ -157,7 +157,7 @@ public function testRemoveMissingEntriesIfAllowDelete($allowAdd)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

// The original object was modified
Expand All @@ -182,7 +182,7 @@ public function testDoNothingIfNotAllowDelete($allowAdd)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

// We still have the original object
Expand All @@ -201,7 +201,7 @@ public function testDoNothingIfNotAllowDelete($allowAdd)
public function testRequireArrayOrTraversable($allowAdd, $allowDelete)
{
$newData = 'no array or traversable';
$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener = new MergeCollectionListener($allowAdd, $allowDelete);
$listener->onBind($event);
}
Expand All @@ -215,7 +215,7 @@ public function testDealWithNullData()

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

$this->assertSame($originalData, $event->getData());
Expand All @@ -233,7 +233,7 @@ public function testDealWithNullOriginalDataIfAllowAdd($allowDelete)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

$this->assertSame($newData, $event->getData());
Expand All @@ -251,7 +251,7 @@ public function testDontDealWithNullOriginalDataIfNotAllowAdd($allowDelete)

$this->form->setData($originalData);

$event = DeprecationErrorHandler::getFormEvent($this->form, $newData);
$event = new FormEvent($this->form, $newData);
$listener->onBind($event);

$this->assertNull($event->getData());
Expand Down
Loading

0 comments on commit 5412a4e

Please sign in to comment.