Transformation dans un format lisible de la valeur de la directive PHP error_reporting

Publié le 20/08/2021 • Actualisé le 20/08/2021


English language detected! 🇬🇧

  We noticed that your browser is using English. Do you want to read this post in this language?

Read the english version 🇬🇧 Close

Dans ce bout de code, nous voyons comment transformer dans un format lisible la valeur de la directive PHP error_reporting. Le code est extrait des commentaires de la documentation PHP que j'ai nettoyé un peu.


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

/**
 * J'utilise un trait PHP afin d'isoler chaque snippet dans un fichier.
 * Ce code doit être apellé d'un contrôleur Symfony étendant AbstractController (depuis Symfony 4.2)
 * ou Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Les services sont injectés dans le constructeur du contrôleur principal.
 */
trait Snippet160Trait
{
    public function snippet160(): void
    {
        $errorReportingConstants = array_filter(get_defined_constants(true)['Core'] ?? [], static function ($constant): bool {
            return str_starts_with($constant, 'E_');
        }, ARRAY_FILTER_USE_KEY);

        $errorReporting = error_reporting();
        $errorReporting = $errorReporting === -1 || $errorReporting > E_ALL ? E_ALL : $errorReporting;

        echo sprintf("Error level (error_reporting()): %d\n", $errorReporting);
        $pot = 0;
        foreach (array_reverse(str_split(decbin($errorReporting))) as $bit) {
            if ((int) $bit === 1) {
                $idx = array_search(2 ** $pot, $errorReportingConstants, true);
                echo sprintf("%s (%d)\n", $idx, $errorReportingConstants[(string) $idx]);
            }
            ++$pot;
        }

        // That's it! 😁
    }
}

 Exécuter le snippet  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow   Lire la doc  Plus sur le web

  Travaillez avec moi !

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @see Snippet160Trait
 */
final class Snippet160Test extends KernelTestCase
{
    /**
     * @return iterable<int, array{0: int, 1: array<string, int>}>
     */
    public function provideSnippet160(): iterable
    {
        yield [
            5, // = 1 + 4
            [
                'E_ERROR' => 1,
                'E_PARSE' => 4,
            ],
        ];

        yield [
            18646, // = 2 + 4 ... + 16384
            [
                'E_WARNING' => 2,
                'E_PARSE' => 4,
                'E_CORE_ERROR' => 16,
                'E_COMPILE_ERROR' => 64,
                'E_COMPILE_WARNING' => 128,
                'E_STRICT' => 2048,
                'E_USER_DEPRECATED' => 16384,
            ],
        ];
    }

    /**
     * @param array<string,int> $codes
     *
     * @dataProvider provideSnippet160
     *
     * @see Snippet160Trait::snippet160
     */
    public function testSnippet160(int $errorReporting, array $codes): void
    {
        self::assertSame($this->getHumanVersion($errorReporting), $codes);
    }

    /**
     * @return array<string,int>
     */
    private function getHumanVersion(int $errorReporting): array
    {
        $codes = [];
        $errorReportingConstants = array_filter(get_defined_constants(true)['Core'] ?? [], static function ($constant): bool {
            return str_starts_with($constant, 'E_');
        }, ARRAY_FILTER_USE_KEY);
        $pot = 0;
        foreach (array_reverse(str_split(decbin($errorReporting))) as $bit) {
            if ((int) $bit === 1) {
                $idx = array_search(2 ** $pot, $errorReportingConstants, true);
                $codes[(string) $idx] = (int) $errorReportingConstants[(string) $idx];
            }
            ++$pot;
        }

        return $codes;
    }
}