33

Since my Symfony 2 update to 2.7. I get a lot of deprecated erors in PHPUnit and console (message is clear by now).

ProjectX\ApiBundle\Tests\Controller\SectionsControllerTest::testPostDebug()
The twig.form.resources configuration key is deprecated since version 2.6 and will be removed in 3.0. Use the twig.form_themes configuration key instead.

Any idea how to disable them for now?

Matteo
  • 37,680
  • 11
  • 100
  • 115
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78
  • Well, hopefully, as of symfony 2.7.1 deprecation messages are silenced by default and will only appear in the dev debug bar. http://symfony.com/blog/symfony-2-7-1-released – COil Jul 03 '15 at 21:35

5 Answers5

48

AppKernel's inherited Kernel::init() function is depreciated itself so changing it is not a viable long term solution.

You can easily override the error reporting by changing the call to Debug::enable(); in both app/console and web/app_dev.php like so.

Change

Debug::enable();

to

Debug::enable(E_RECOVERABLE_ERROR & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED, false);

This will leave all other error reporting in tact while suppressing depreciated warnings. And you don't need to mess with the Kernel at all.

James Grundner
  • 1,454
  • 1
  • 10
  • 12
43

In my case, i couldn't hide deprecated warning without using SYMFONY_DEPRECATIONS_HELPERenvironnment variable.

Change your phpunit.xml with

<phpunit>
    <!-- ... -->

    <php>
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    </php>
</phpunit>

Then, you'll just have one message like "Remaining deprecation notices (x)" which is not considered as a test failure.

Hope this will help.

j-guyon
  • 1,822
  • 1
  • 19
  • 31
  • This does solve it for my pipeline, but while locally running `phpunit` I actually want to output these deprecations. `$_ENV['SYMFONY_DEPRECATIONS_HELPER'] = 'weak';` does not solve this though. – Rvanlaak Feb 15 '17 at 22:15
18

I have the same problem and solved it similar to the below link. Symfony declares to report all errors and overrides what you put in php.ini by design (otherwise it couldn't catch & display nice stack traces for you).

So, you'll need to override Symfony2's built-in error reporting by creating an init() function in your AppKernel.php and setting the error_reporting how you'd like there, along with (probably) some environment detection to make sure you don't display errors in production, for example:

// Add this to app/AppKernel.php
public function init()
{
    if ($this->debug) {
        ini_set('display_errors', 1);
        error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
    } else {
        ini_set('display_errors', 0);
    }
}

More details here (use Google Translate if you do not read Russian :) http://tokarchuk.ru/2012/12/disable-deprecated-warnings-in-symfony-2/

phpguru
  • 2,351
  • 2
  • 23
  • 33
16

Note that disabling deprecation warnings via error_reporting() or Debug::enable() will not prevent them from being logged to dev.log. To disable them from being logged you'll need to change the log level in your monolog handler to "warning" (deprecation warnings are logged as "info" in the "php" channel).

Alternatively, to prevent other logs from being affected, you can create a separate monolog handler with a different level for the "php" channel e.g.

monolog:
    handlers:
        main:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug
            formatter: monolog.formatter.session_request
            channels: '!php'
        php:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: warning
            formatter: monolog.formatter.session_request
            channels: 'php'
  • 1
    FYI the `php` channel can contain more than only deprecation messages. For example, it might also contain uncaught exceptions during the rendering of a twig template. – Maurice Dec 18 '19 at 14:02
  • 1
    problem with this is that maybe there is a bunch of other logs classified as info that are worth being logged on production – Murilo May 26 '21 at 02:01
7

The twig.form configuration key has been removed in the new version of Twig. Therefore you should replace the key in your config.yml

 ///DEPRECATED : 

  twig:
     form:
         resources:
             - 'path_to_template_file'

 // NEW WAY : 
  twig:
     form_themes:
         - 'path_to_template_file'
axelvnk
  • 442
  • 1
  • 6
  • 15