Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header #2848

Closed
pronist opened this issue Jan 31, 2020 · 5 comments
Milestone

Comments

@pronist
Copy link

pronist commented Jan 31, 2020

Check this.

<?php if (array_key_exists('user', $_SESSION)) : ?>
    <li><a href="/user/update.php">My page</a></li>
    <li><a href="/post/write.php">Write</a></li>
    <li><a href="/auth/logout.php">Sign out</a></li>
<?php else : ?>
    <li><a href="/auth/login.php">Sign in</a></li>
<?php endif; ?>

My composer.json

{
    "name": "pronist/phpblog",
    "scripts":{
        "lint": "phpcs --standard=PSR12 -n --extensions=php --ignore=vendor ."
    },
    "require-dev": {
        "squizlabs/php_codesniffer": "^3.5"
    }
}
# PHP_CodeSniffer version 3.5.3
$ phpcs --standard=PSR12 -n --extensions=php --ignore=vendor .
# PHP_CodeSniffer version 3.5.4
$ phpcs --standard=PSR12 -n --extensions=php --ignore=vendor .

FILE: D:\Development\php\phpblog\layouts\top.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 21 | ERROR | [x] Header blocks must be separated by a single blank
    |       |     line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Is it a bug?

@jrfnl
Copy link
Contributor

jrfnl commented Jan 31, 2020

@pronist Could you run phpcs --config-show and phpcs --version both from CLI directly, as well as via a Composer script and share the output ?

I suspect different installations of PHPCS are being used.

@pronist pronist changed the title Different result on 'composer run lint' and CLI Execution Different result on '3.5.3' and '3.5.4' Jan 31, 2020
@pronist
Copy link
Author

pronist commented Jan 31, 2020

@jrfnl Oh, global Installation is 3.5.3, but local Installation is 3.5.4

@jrfnl
Copy link
Contributor

jrfnl commented Jan 31, 2020

Yup, that looks like a regression. Probably related to a bug in PHPCS 3.5.3 which was fixed in 3.5.4. See: #2732

PSR 12 states:

When the opening <?php tag is on the first line of the file, it MUST be on its own line with no other statements unless it is a file containing markup outside of PHP opening and closing tags.

Based on that and presuming this is the start of the file, your code should pass. However as the error you quote mentions line 21 and the code sample you provided does not contain 21 lines, more information - i.e. a more complete code sample - is needed to properly evaluate this.

Nonetheless, I can confirm this is a bug as with your code sample I can reproduce the error on line 1.

@pronist
Copy link
Author

pronist commented Jan 31, 2020

@jrfnl

When the opening <?php tag is on the first line of the file, ...

but, my file is just starting with <!DOCTYPE html>. not <?php.

It's a Full code sample.

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>phpblog - <?=$_SERVER['REQUEST_URI'] ?? ''?></title>

        <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Noto+Sans+KR:400,500&display=swap&subset=korean">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.2.6/dist/css/uikit.min.css" />
        <link rel="stylesheet" href="/app.css">
    </head>
    <body>
        <div id="app" class="uk-container-expand">
            <nav id="nav" role="navigation" class="uk-navbar-container uk-navbar-transparent uk-padding uk-padding-remove-vertical uk-margin-bottom" uk-navbar>
                <div class="uk-navbar-right">
                    <ul class="uk-navbar-nav">
                        <li><a href="/">Home</a></li>
                        <li><a href="/user/register.php">Register</a></li>
                        <?php if (array_key_exists('user', $_SESSION)) : ?>
                            <li><a href="/user/update.php">My page</a></li>
                            <li><a href="/post/write.php">Write</a></li>
                            <li><a href="/auth/logout.php">Sign out</a></li>
                        <?php else : ?>
                            <li><a href="/auth/login.php">Sign in</a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </nav>
            <main id="main" role="main">

@gsherwood gsherwood changed the title Different result on '3.5.3' and '3.5.4' PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header Feb 2, 2020
@gsherwood gsherwood added this to the 3.5.5 milestone Feb 2, 2020
gsherwood added a commit that referenced this issue Feb 2, 2020
gsherwood added a commit that referenced this issue Feb 2, 2020
@gsherwood
Copy link
Member

Thanks for reporting this. The fix will be in 3.5.5.

tobias-trozowski pushed a commit to tobias-trozowski/PHP_CodeSniffer that referenced this issue Feb 13, 2020
…file with mixed PHP and HTML and no file header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants