Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Fix My Deprecations

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $5.00

The last step is to find and eliminate the final deprecation warnings. Since the changes to the form system are some of the biggest, navigate to a page with a form. Woh, error!

The type name specified for the service "app.form.type.datetimepicker" does not match the actual name.

Because the way forms are named has changed, I know this has something to do with that. The stack trace points us to BlogController line 149:

... lines 1 - 70
public function newAction(Request $request)
{
... lines 73 - 76
$form = $this->createForm(new PostType(), $post)
... lines 78 - 109
}
... lines 111 - 220

And that line points us to PostType. A-ha! The publishedAt field type is still app_datetimepicker:

... lines 1 - 25
class PostType extends AbstractType
{
... lines 28 - 30
public function buildForm(FormBuilderInterface $builder, array $options)
{
... lines 33 - 42
$builder
... lines 44 - 53
->add('publishedAt', 'app_datetimepicker', array(
'label' => 'label.published_at',
))
;
}
... lines 59 - 78
}

The upgrade fixer did not fix this type name, because it's custom. Change this to DateTimePickerType::class. Then go back, hit option+enter, and click "Import class" to add the use statement:

... lines 1 - 31
public function buildForm(FormBuilderInterface $builder, array $options)
{
... lines 34 - 54
->add('publishedAt', DateTimePickerType::class, array(
... line 56
))
;
}
... lines 60 - 71

While we're here, BlogController has a problem too. Instead of saying new PostType(), you need to say PostType::class:

Refresh! It works! Open the deprecation notices. There are still 3: one for the problematic AsseticBundle, one for unquoted @ symbols and another because the global _self variable in Twig is deprecated. The _self usage comes from the Symfony Demo itself: it's used in the "Show Code" functionality. Since that's specific to the Symfony Demo, I'll ignore that one.

Finding Unquoted @'s

So let's keep going. Open up app/config/services.yml. Starting in 2.8, you need to surround any string that starts with @ with quotes. Yea, it turns out that @ is a special character, so it was always illegal to have unquoted @ symbols, but the YAML parser was nice and worked anyways. Well, now the YAML parser is super strict, so surround @markdown with single quotes. Do the same around @router:

services:
... lines 2 - 11
app.twig.app_extension:
... lines 13 - 14
arguments: ['@markdown', %app_locales%]
... lines 16 - 24
app.redirect_to_preferred_locale_listener:
... line 26
arguments: ['@router', %app_locales%, %locale%]
... lines 28 - 51

And while you're here, remove the alias from the form.type tag: that's not needed in 3.0:

services:
... lines 2 - 30
app.form.type.datetimepicker:
class: AppBundle\Form\Type\DateTimePickerType
tags:
- { name: form.type }
... lines 35 - 51

Let's see what that did. Go back and clear the cache:

rm -rf var/cache/*

Refresh! Now we're down to 4 deprecations, and two are still coming from unquoted YAML files. Hmm. Look at the stack trace. It's not easy to figure out where this problem is coming from. But look: CodeExplorerExtension::load(): that class lives in our code: inside the CodeExplorerBundle. Open that class:

... lines 1 - 21
class CodeExplorerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

Inside, we load a services.yml file. Ah, I bet there's an @ symbol inside! And there it is: wrap that string in single quotes:

services:
... lines 2 - 8
code_explorer.controller_listener:
class: CodeExplorerBundle\EventListener\ControllerListener
arguments: ['@code_explorer.twig.source_code_extension']
... lines 12 - 14

Let's do it again: clear the cache and refresh:

rm -rf var/cache/*

But there's still one unwrapped @ symbol. Look really closely. This time, you can see that the trace starts with a Router::getRouteCollection() followed by a YamlFileLoader. This makes me think that this is coming from a routing.yml file. Open the main one. Of course! Put quotes around the @AppBundle string:

... lines 1 - 6
app:
resource: '@AppBundle/Controller/'
... lines 9 - 28

Configuration Tweaks

Before you refresh, fix one more: the csrf notice. In this case, a config key was renamed from csrf_provider to csrf_token_generator in security.yml. A lot of changes are like this: simple renames:

security:
... lines 2 - 14
firewalls:
secured_area:
... lines 17 - 25
form_login:
... lines 27 - 33
csrf_token_generator: security.csrf.token_manager
... lines 35 - 46

Clear the cache and refresh:

rm -rf var/cache/*

And boom! The only deprecation left is coming from our code, but I'm going to ignore it because fixing it isn't very interesting. Refresh one more time to simulate using the cached files. We can still see the other problem we have because AsseticBundle isn't yet compatible with 3.0.

In real life, this will block us from continuing to Symfony 3.0. Outside bundles will be the biggest issue with upgrading. But don't worry! Until then, you have all the features you need on 2.8. Be patient.

In your project, continue going through other sections of your site, looking for - and fixing - more deprecated code. And of course, run your tests: if you have the phpunit bridge installed, you'll get a print-out of deprecated calls hit from your tests.

Once the deprecations are gone: you're ready for 3.0.

Leave a comment!

9
Login or Register to join the conversation
Default user avatar
Default user avatar Dominik | posted 2 years ago

Hello! I have strange problem. I see 0 deprecations in symfony profiler. I'm 100% sure there are lots of deprecations but none of them are showing up. Do you have any idea where to look for the problem? I mean why profiler is not logging any deprecations?
Tried lots of things from stackoverflow - didn't work.

Reply

Hey @Dominik

That IS strange. Lets start from scratch. Is you PHP configured to show all errors with error_reporting config key set to E_ALL? and display errors set to true?

However another question why are you so sure that you have a lot of deprecations?

and some more questions: What is your php version? What Symfony version do you use? Is it related to course code?

Cheers

Reply
Oda Avatar

Hi,
in symfony 2.3 I used (in TWIG file):

{% set nameBundle = app.request.get('_template').get('bundle') %}
{% set nameEntity = app.request.get('_template').get('controller') %}

I try to use in 2.8/3.0 :
{% set nameBundle = app.request.attributes.get('_bundle') %}
{% set nameEntity = app.request.attributes.get('_controller') %}
but it is not the same!

what can I use for gettin the same answer?

Thank you in advance.
Oda

Reply
MolloKhan Avatar MolloKhan | SFCASTS | Oda | posted 3 years ago | edited

Hey Oda

I don't remember exactly how things were in Symfony2 but when I doubt is always handy to dump things so you can check the names of variables and more. Just do this on any twig template {{ dump(app.request) }}
Or, dump the request inside a controller's action dump($request);

Cheers!

Reply
Default user avatar
Default user avatar vamshikrishna | posted 5 years ago

Warning: trim() expects parameter 1 to be string, array given in symfony sir what should i do

Reply

Hi!

Do exactly what the error message says. The trim() function does not work with arrays, in dev mode look at the error page and find the line where you pass incorrect value. Try to debug why the value is an array, is it on purpose or just a simple dev misprint? If on purpose, you need to change the logic because trim() does not work with arrays. Here's the link to the docs:
http://php.net/manual/en/fu...

Cheers!

Reply
Default user avatar

Hi,
I have the case where : @=service('doctrine.orm.default_entity_manager').getRepository('LineFringeBundle:PerformanceBookmark')
I've tried several different ways of wrapping it in quotes but with no luck. Any suggestions?

Reply

Hey claire

Have you tried using double quotes as a main wrapper? (and inside it, single quotes)

I believe you may find this article useful: https://symfony.com/doc/cur...

Cheers!

Reply
Default user avatar

Thankyou! Should have thought of that, cheers for the advice

1 Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.9",
        "ext-pdo_sqlite": "*",
        "doctrine/doctrine-bundle": "~1.5", // 1.6.3
        "doctrine/doctrine-fixtures-bundle": "~2.2", // 2.3.0
        "doctrine/orm": "~2.4", // v2.5.4
        "erusev/parsedown": "~1.5", // 1.6.0
        "ezyang/htmlpurifier": "~4.7", // v4.7.0
        "incenteev/composer-parameter-handler": "~2.1", // v2.1.2
        "ircmaxell/password-compat": "~1.0", // v1.0.4
        "knplabs/knp-paginator-bundle": "~2.4", // 2.5.3
        "leafo/scssphp": "~0.1.5", // v0.1.10
        "patchwork/jsqueeze": "~1.0", // v1.0.7
        "sensio/distribution-bundle": "^5.0", // v5.0.7
        "sensio/framework-extra-bundle": "~3.0", // v3.0.16
        "symfony/assetic-bundle": "~2.8", // v2.8.0
        "symfony/monolog-bundle": "~2.7", // 2.11.1
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.11
        "symfony/symfony": "3.0.*", // v3.0.9
        "twig/extensions": "~1.2" // v1.3.0
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0" // v3.0.7
    }
}
userVoice