<?php

use Illuminate\Support\Facades\File;

/**
 * A Blade comment describes a directive. It never spells one.
 *
 * Blade compiles what is inside `{{-- --}}` as well: the comment is stripped
 * from the OUTPUT, but the directives in it are compiled on the way there. So a
 * comment explaining why the parenthesised php directive was avoided produced
 * exactly the open PHP tag it was warning about — and everything after it in the
 * file was swallowed as PHP source.
 *
 * That failure is silent. No exception, no warning, no missing-view error: the
 * page renders, one block short. The term switch on the order page disappeared
 * this way and the only symptom was a test asserting it was there.
 *
 * Scoped to the forms that open a PHP tag, which are the ones that eat the rest
 * of the file. A comment mentioning @if or @disabled is survivable and there are
 * two of those in the repo already; this is not the place to relitigate them.
 */
it('never spells a php open tag inside a blade comment', function () {
    $offenders = [];

    foreach (File::allFiles(resource_path('views')) as $file) {
        if (! str_ends_with($file->getFilename(), '.blade.php')) {
            continue;
        }

        preg_match_all('/\{\{--.*?--\}\}/s', $file->getContents(), $comments);

        foreach ($comments[0] ?? [] as $comment) {
            // Assembled rather than written out, for the reason this test
            // exists: the needles below would otherwise be compiled out of THIS
            // file's own comments if it were ever a template.
            $needles = ['@'.'php', '@'.'endphp', '<'.'?php', '<'.'?='];

            foreach ($needles as $needle) {
                if (str_contains($comment, $needle)) {
                    $offenders[] = str_replace(resource_path().'/', '', $file->getPathname()).': '.$needle;
                }
            }
        }
    }

    expect($offenders)->toBe([]);
});
