squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

PSR2 and alternative php syntax for template #305

Closed tamvodopad closed 10 years ago

tamvodopad commented 10 years ago

I use PSR2 and alternative syntax for php in templates.

For example this code:

<?php if($foo):?>
  <?=$bar?>
<?php endif?>

After running phpsniffer, i'm see error like this: "Line indented incorrectly; expected 103 spaces, found 0"

What i can do for correct check files with alternative php code?

Exclude this files is not correct way because this files can include another errors in code.

aik099 commented 10 years ago

It's pretty hard to figure out correct indentation level, when HTML is mixed with PHP. I guess the ScopeIndent sniff, that usually reports such things, just got confused.

If these files have any specific naming pattern, then you can skip some sniffs for them as well.

gsherwood commented 10 years ago

The example code is obviously not the whole file if it is asking for that many spaces, which makes it hard to test because it is the other OPEN and CLOSE tags that actually cause the issues.

In the 2.x versions, I've rewritten the sniff that checks for indent to make it much better at processing inline PHP like this. Even with that small snippet of code, there is a difference between the errors report from 1.x (1 indent error) and 2.x (no indent errors).

So I suggest grabbing the latest code from the Github repo (phpcs-fixer branch) if you can and giving that a test. If you can't, try to at least see if the 2.0.0RC3 release works for you. If you can't do that, put the file up somewhere (if possible) so I can check that it is being sniffed correctly before I do the final 2.0.0 stable release.

tamvodopad commented 10 years ago

@gsherwood with latest Codesnifer Code 2.0.0RC3

my file:

<?php
/**
 * @var \yii\web\View $this
 * @var \webmasters\modules\tickets\widgets\unread\Unread $widget
 */
$widget = $this->context;
?>

<a href="<?=$widget->linkToTicket()?>" class="h-indicator icon icon-ticket-new-mail" title="<?=Yii::t('tickets', 'New messages') ?>">
    <?php if ($widget->unreadTicketsCounter): ?>
        <span class="h-menu-notificator notificator-mail"><?=$widget->unreadTicketsCounter?></span>
    <?php endif;?>
</a>

and codesnifer report for this file:

FILE: ...-ru-yii/webmasters/modules/tickets/widgets/unread/views/unread.view.php
--------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
 11 | ERROR | [x] Expected 1 space after closing parenthesis; found ""
 11 | ERROR | [x] Expected 1 newline after opening brace; 0 found
 13 | ERROR | [x] Closing brace must be on a line by itself
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
aik099 commented 10 years ago

I wonder what kind of validation you can expect from such HTML/PHP mixed file.

gsherwood commented 10 years ago

So these aren't indent errors, so those are obviously fixed, which is good.

The errors are now caused by the fact that your using alternate syntax here, and the PSR2 standard really isn't written to cover files like this.

Ultimately, it would be better for you to exclude these view files from being checked because PSR2 isn't going to have anything to really check in there. But I will change the sniffs that are reporting these errors to ensure they are only checking when braces are used and skipping the checks when using alternate syntax.

gsherwood commented 10 years ago

Actually, I just realised that I can't do that because the alternate syntax is checked correctly if it is not mixed inside HTML. I think the only way around this for you is to exclude these files; probably the exclude views directory, or just files that ends in .view.php. You can hard-code this rule into a ruleset.xml file to make life easier. Let me know if you need help creating one.

tamvodopad commented 10 years ago

@gsherwood ok, thanx.