[Form] removed deprecated FormType::getName()

This commit is contained in:
Fabien Potencier 2015-10-01 09:27:49 +02:00
parent bfba6caebf
commit 582f3a39b7
47 changed files with 14 additions and 489 deletions

View File

@ -156,15 +156,7 @@ abstract class AbstractExtension implements FormExtensionInterface
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
}
// Since Symfony 3.0 types are identified by their FQCN
$fqcn = get_class($type);
$legacyName = $type->getName();
$this->types[$fqcn] = $type;
if ($legacyName) {
$this->types[$legacyName] = $type;
}
$this->types[get_class($type)] = $type;
}
}

View File

@ -47,15 +47,6 @@ abstract class AbstractType implements FormTypeInterface
{
}
/**
* {@inheritdoc}
*/
public function getName()
{
// As of Symfony 2.8, the name defaults to the fully-qualified class name
return get_class($this);
}
/**
* Returns the prefix of the template block name for this type.
*
@ -66,11 +57,7 @@ abstract class AbstractType implements FormTypeInterface
*/
public function getBlockPrefix()
{
$fqcn = get_class($this);
$name = $this->getName();
// For BC: Use the name as block prefix if one is set
return $name !== $fqcn ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
return StringUtil::fqcnToBlockPrefix(get_class($this));
}
/**

View File

@ -78,17 +78,7 @@ abstract class BaseType extends AbstractType
$blockPrefixes = array();
for ($type = $form->getConfig()->getType(); null !== $type; $type = $type->getParent()) {
if (method_exists($type, 'getBlockPrefix')) {
array_unshift($blockPrefixes, $type->getBlockPrefix());
} else {
@trigger_error(get_class($type).': The ResolvedFormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should add it to your implementation.', E_USER_DEPRECATED);
$fqcn = get_class($type->getInnerType());
$name = $type->getName();
$hasCustomName = $name !== $fqcn;
array_unshift($blockPrefixes, $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn));
}
array_unshift($blockPrefixes, $type->getBlockPrefix());
}
$blockPrefixes[] = $uniqueBlockPrefix;
@ -111,7 +101,7 @@ abstract class BaseType extends AbstractType
// collection form have different types (dynamically), they should
// be rendered differently.
// https://github.com/symfony/symfony/issues/5038
'cache_key' => $uniqueBlockPrefix.'_'.$form->getConfig()->getType()->getName(),
'cache_key' => $uniqueBlockPrefix.'_'.$form->getConfig()->getType()->getBlockPrefix(),
));
}

View File

@ -34,14 +34,6 @@ class BirthdayType extends AbstractType
return __NAMESPACE__.'\DateType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -28,14 +28,6 @@ class ButtonType extends BaseType implements ButtonTypeInterface
{
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -62,14 +62,6 @@ class CheckboxType extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -357,14 +357,6 @@ class ChoiceType extends AbstractType
$resolver->setAllowedTypes('group_by', array('null', 'array', '\Traversable', 'string', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -96,14 +96,6 @@ class CollectionType extends AbstractType
$resolver->setNormalizer('options', $optionsNormalizer);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -36,14 +36,6 @@ class CountryType extends AbstractType
return __NAMESPACE__.'\ChoiceType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -36,14 +36,6 @@ class CurrencyType extends AbstractType
return __NAMESPACE__.'\ChoiceType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -283,14 +283,6 @@ class DateTimeType extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -276,14 +276,6 @@ class DateType extends AbstractType
$resolver->setAllowedTypes('days', 'array');
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -23,14 +23,6 @@ class EmailType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -57,14 +57,6 @@ class FileType extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -240,14 +240,6 @@ class FormType extends BaseType
{
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -30,14 +30,6 @@ class HiddenType extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -59,14 +59,6 @@ class IntegerType extends AbstractType
$resolver->setAllowedTypes('scale', array('null', 'int'));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -36,14 +36,6 @@ class LanguageType extends AbstractType
return __NAMESPACE__.'\ChoiceType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -36,14 +36,6 @@ class LocaleType extends AbstractType
return __NAMESPACE__.'\ChoiceType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -62,14 +62,6 @@ class MoneyType extends AbstractType
$resolver->setAllowedTypes('scale', 'int');
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -57,14 +57,6 @@ class NumberType extends AbstractType
$resolver->setAllowedTypes('scale', array('null', 'int'));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -47,14 +47,6 @@ class PasswordType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -46,14 +46,6 @@ class PercentType extends AbstractType
$resolver->setAllowedTypes('scale', 'int');
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -23,14 +23,6 @@ class RadioType extends AbstractType
return __NAMESPACE__.'\CheckboxType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -23,14 +23,6 @@ class RangeType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -61,14 +61,6 @@ class RepeatedType extends AbstractType
$resolver->setAllowedTypes('second_options', 'array');
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -29,14 +29,6 @@ class ResetType extends AbstractType implements ButtonTypeInterface
return __NAMESPACE__.'\ButtonType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -23,14 +23,6 @@ class SearchType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -36,14 +36,6 @@ class SubmitType extends AbstractType implements SubmitButtonTypeInterface
return __NAMESPACE__.'\ButtonType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -26,14 +26,6 @@ class TextType extends AbstractType
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -33,14 +33,6 @@ class TextareaType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -261,14 +261,6 @@ class TimeType extends AbstractType
$resolver->setAllowedTypes('seconds', 'array');
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -42,14 +42,6 @@ class TimezoneType extends AbstractType
return __NAMESPACE__.'\ChoiceType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -46,14 +46,6 @@ class UrlType extends AbstractType
return __NAMESPACE__.'\TextType';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/

View File

@ -46,7 +46,6 @@ class FormDataExtractor implements FormDataExtractorInterface
$data = array(
'id' => $this->buildId($form),
'name' => $form->getName(),
'type' => $form->getConfig()->getType()->getName(),
'type_class' => get_class($form->getConfig()->getType()->getInnerType()),
'synchronized' => $this->valueExporter->exportValue($form->isSynchronized()),
'passed_options' => array(),

View File

@ -41,20 +41,7 @@ class DependencyInjectionExtension implements FormExtensionInterface
throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name));
}
$type = $this->container->get($this->typeServiceIds[$name]);
// BC: validate result of getName() for legacy names (non-FQCN)
if ($name !== get_class($type) && $type->getName() !== $name) {
throw new InvalidArgumentException(
sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"',
$this->typeServiceIds[$name],
$name,
$type->getName()
)
);
}
return $type;
return $this->container->get($this->typeServiceIds[$name]);
}
public function hasType($name)

View File

@ -105,17 +105,12 @@ class FormFactory implements FormFactoryInterface
$options['data'] = $data;
}
if ($type instanceof FormTypeInterface) {
@trigger_error('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
$type = $this->resolveType($type);
} elseif (is_string($type)) {
$type = $this->registry->getType($type);
} elseif ($type instanceof ResolvedFormTypeInterface) {
@trigger_error('Passing type instances to FormBuilder::add(), Form::add() or the FormFactory is deprecated since version 2.8 and will not be supported in 3.0. Use the fully-qualified type class name instead.', E_USER_DEPRECATED);
} else {
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
if (!is_string($type)) {
throw new UnexpectedTypeException($type, 'string');
}
$type = $this->registry->getType($type);
$builder = $type->createBuilder($this, $name, $options);
// Explicitly call buildForm() in order to be able to override either

View File

@ -34,11 +34,6 @@ class FormRegistry implements FormRegistryInterface
*/
private $types = array();
/**
* @var string[]
*/
private $legacyNames = array();
/**
* @var FormTypeGuesserInterface|false|null
*/
@ -100,10 +95,6 @@ class FormRegistry implements FormRegistryInterface
$this->resolveAndAddType($type);
}
if (isset($this->legacyNames[$name])) {
@trigger_error(sprintf('Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0. Use the fully-qualified type class name "%s" instead.', $name, get_class($this->types[$name]->getInnerType())), E_USER_DEPRECATED);
}
return $this->types[$name];
}
@ -120,28 +111,6 @@ class FormRegistry implements FormRegistryInterface
$typeExtensions = array();
$parentType = $type->getParent();
$fqcn = get_class($type);
$name = $type->getName();
$hasCustomName = $name !== $fqcn;
if ($parentType instanceof FormTypeInterface) {
@trigger_error('Returning a FormTypeInterface from FormTypeInterface::getParent() is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
$this->resolveAndAddType($parentType);
$parentType = $parentType->getName();
}
if ($hasCustomName) {
foreach ($this->extensions as $extension) {
$typeExtensions = array_merge(
$typeExtensions,
$extension->getTypeExtensions($name)
);
}
if ($typeExtensions) {
@trigger_error('Returning a type name from FormTypeExtensionInterface::getExtendedType() is deprecated since version 2.8 and will be removed in 3.0. Return the fully-qualified type class name instead.', E_USER_DEPRECATED);
}
}
foreach ($this->extensions as $extension) {
$typeExtensions = array_merge(
@ -157,12 +126,6 @@ class FormRegistry implements FormRegistryInterface
);
$this->types[$fqcn] = $resolvedType;
if ($hasCustomName) {
// Enable access by the explicit type name until Symfony 3.0
$this->types[$name] = $resolvedType;
$this->legacyNames[$name] = true;
}
}
/**
@ -170,10 +133,6 @@ class FormRegistry implements FormRegistryInterface
*/
public function hasType($name)
{
if (isset($this->legacyNames[$name])) {
@trigger_error(sprintf('Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0. Use the fully-qualified type class name "%s" instead.', $name, get_class($this->types[$name]->getInnerType())), E_USER_DEPRECATED);
}
if (isset($this->types[$name])) {
return true;
}

View File

@ -78,26 +78,7 @@ interface FormTypeInterface
/**
* Returns the name of the parent type.
*
* You can also return a type instance from this method, although doing so
* is discouraged because it leads to a performance penalty. The support
* for returning type instances may be dropped from future releases.
*
* Returning a {@link FormTypeInterface} instance is deprecated since
* Symfony 2.8 and will be unsupported as of Symfony 3.0. Return the
* fully-qualified class name of the parent type instead.
*
* @return string|null|FormTypeInterface The name of the parent type if any,
* null otherwise.
* @return string|null The name of the parent type if any, null otherwise
*/
public function getParent();
/**
* Returns the name of this type.
*
* @return string The name of this type
*
* @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
* Use the fully-qualified class name of the type instead.
*/
public function getName();
}

View File

@ -48,10 +48,6 @@ class PreloadedExtension implements FormExtensionInterface
$this->typeGuesser = $typeGuesser;
foreach ($types as $type) {
// Up to Symfony 2.8, types were identified by their names
$this->types[$type->getName()] = $type;
// Since Symfony 2.8, types are identified by their FQCN
$this->types[get_class($type)] = $type;
}
}

View File

@ -24,16 +24,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
class ResolvedFormType implements ResolvedFormTypeInterface
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $blockPrefix;
/**
* @var FormTypeInterface
*/
@ -56,64 +46,17 @@ class ResolvedFormType implements ResolvedFormTypeInterface
public function __construct(FormTypeInterface $innerType, array $typeExtensions = array(), ResolvedFormTypeInterface $parent = null)
{
$fqcn = get_class($innerType);
$name = $innerType->getName();
$hasCustomName = $name !== $fqcn;
if (method_exists($innerType, 'getBlockPrefix')) {
$reflector = new \ReflectionMethod($innerType, 'getName');
$isOldOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
$reflector = new \ReflectionMethod($innerType, 'getBlockPrefix');
$isNewOverwritten = $reflector->getDeclaringClass()->getName() !== 'Symfony\Component\Form\AbstractType';
// Bundles compatible with both 2.3 and 2.8 should implement both methods
// Anyone else should only override getBlockPrefix() if they actually
// want to have a different block prefix than the default one
if ($isOldOverwritten && !$isNewOverwritten) {
@trigger_error(get_class($this->innerType).': The FormTypeInterface::getName() method is deprecated since version 2.8 and will be removed in 3.0. Remove it from your classes. Use getBlockPrefix() if you want to customize the template block prefix. This method will be added to the FormTypeInterface with Symfony 3.0.', E_USER_DEPRECATED);
}
$blockPrefix = $innerType->getBlockPrefix();
} else {
@trigger_error(get_class($this->innerType).': The FormTypeInterface::getBlockPrefix() method will be added in version 3.0. You should extend AbstractType or add it to your implementation.', E_USER_DEPRECATED);
// Deal with classes that don't extend AbstractType
// Calculate block prefix from the FQCN by default
$blockPrefix = $hasCustomName ? $name : StringUtil::fqcnToBlockPrefix($fqcn);
}
// As of Symfony 2.8, getName() returns the FQCN by default
// Otherwise check that the name matches the old naming restrictions
if ($hasCustomName && !preg_match('/^[a-z0-9_]*$/i', $name)) {
throw new InvalidArgumentException(sprintf(
'The "%s" form type name ("%s") is not valid. Names must only contain letters, numbers, and "_".',
get_class($innerType),
$name
));
}
foreach ($typeExtensions as $extension) {
if (!$extension instanceof FormTypeExtensionInterface) {
throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
}
}
$this->name = $name;
$this->blockPrefix = $blockPrefix;
$this->innerType = $innerType;
$this->typeExtensions = $typeExtensions;
$this->parent = $parent;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* Returns the prefix of the template block name for this type.
*
@ -121,7 +64,7 @@ class ResolvedFormType implements ResolvedFormTypeInterface
*/
public function getBlockPrefix()
{
return $this->blockPrefix;
return $this->innerType->getBlockPrefix();
}
/**

View File

@ -20,13 +20,6 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
*/
interface ResolvedFormTypeInterface
{
/**
* Returns the name of the type.
*
* @return string The type name.
*/
public function getName();
/**
* Returns the parent type.
*

View File

@ -81,7 +81,6 @@ class FormDataExtractorTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
'passed_options' => array(),
@ -115,7 +114,6 @@ class FormDataExtractorTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
'passed_options' => array(
@ -150,7 +148,6 @@ class FormDataExtractorTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(
'id' => 'name',
'name' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
'passed_options' => array(),
@ -190,7 +187,6 @@ class FormDataExtractorTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array(
'id' => 'grandParent_parent_name',
'name' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
'passed_options' => array(),

View File

@ -31,7 +31,7 @@ class TestExtension implements FormExtensionInterface
public function addType(FormTypeInterface $type)
{
$this->types[$type->getName() ?: get_class($type)] = $type;
$this->types[get_class($type)] = $type;
}
public function getType($name)

View File

@ -40,7 +40,7 @@ class FormFactoryBuilderTest extends \PHPUnit_Framework_TestCase
$extensions = $registry->getExtensions();
$this->assertCount(1, $extensions);
$this->assertTrue($extensions[0]->hasType($this->type->getName()));
$this->assertTrue($extensions[0]->hasType(get_class($this->type)));
$this->assertNull($extensions[0]->getTypeGuesser());
}

View File

@ -153,7 +153,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
* @expectedExceptionMessage Expected argument of type "string", "stdClass" given
*/
public function testCreateNamedBuilderThrowsUnderstandableException()
{

View File

@ -323,51 +323,6 @@ class ResolvedFormTypeTest extends \PHPUnit_Framework_TestCase
$this->resolvedType->finishView($view, $form, $options);
}
/**
* @dataProvider provideValidNames
*/
public function testGetName($name)
{
$this->type->expects($this->once())
->method('getName')
->willReturn($name);
$resolvedType = new ResolvedFormType($this->type);
$this->assertSame($name, $resolvedType->getName());
}
/**
* @dataProvider provideInvalidNames
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
*/
public function testGetNameFailsIfInvalidChars($name)
{
$this->type->expects($this->once())
->method('getName')
->willReturn($name);
new ResolvedFormType($this->type);
}
public function provideValidNames()
{
return array(
array('text'),
array('type123'),
array('my_type123'),
);
}
public function provideInvalidNames()
{
return array(
array('my-type'),
array('my[type]'),
array('my{type}'),
);
}
public function testGetBlockPrefix()
{
$this->type->expects($this->once())