Re: Bug #19708 [Com]: PHP_CodeSniffer doesn't detect <% and <? if short_open_tag and asp_tags are off

From: Date: Sat, 24 Nov 2012 14:47:59 +0000
Subject: Re: Bug #19708 [Com]: PHP_CodeSniffer doesn't detect <% and <? if short_open_tag and asp_tags are off
References: 1  Groups: php.pear.qa php.pear.bugs 
Request: Send a blank email to pear-qa+get-6125@lists.php.net to get a copy of this message
LING-PENG 在 2012-11-20,8:23,"wiltave@gmail.com" <pear-qa@lists.php.net> 写道: > Edit report at > https://pear.php.net/bugs/bug.php?id=19708&edit=1 > > ID: 19708 > Comment by: wiltave@gmail.com > Reported By: wiltave at gmail dot com > Summary: PHP_CodeSniffer doesn't detect <% and <? if > short_open_tag and asp_tags are off > Status: Feedback > Type: Bug > Package: PHP_CodeSniffer > Operating System: Debian Linux (Testing) > Package Version: 1.4.2 > PHP Version: 5.4.4 > Assigned To: squiz > Roadmap Versions: > New Comment: > > I understand. Maybe you can close this bug report. > Thank you again. > > > Previous Comments: > ------------------------------------------------------------------------ > > [2012-11-18 23:22:01] squiz > > I guess the problem is really that when those settings are off, the file > is not valid > PHP. I don't tokenize PHP files character by character (like I do for > CSS and JS) so I > dont know what context those tags are in. > > This could easily be done by a sniff though. One that detects if you are > using short > open tags and then bans them. It could look for T_OPEN TAG for cases > where the ini > setting is ON and it could also look for T_INLINE_HTML and look inside > that content > for cases where the ini setting is OFF. > > Adding additional tokenizing to the core for this would slow down > processing, so a > sniff works out better because it is optional to include in a standard. > > ------------------------------------------------------------------------ > > [2012-11-16 18:00:30] willianveiga > > Greg Sherwood, I got your point, but I think PHP_CodeSniffer should > detect both as T_OPEN_TAG even if these configuration are set off. > > I've noticed about behaviour but somebody could not do the same. Using > just my sniff above, without your recomendation, somebody with > short_open_tag and asp_tags Off would not have the same code violations > as somebody with these directive On. It would create inconsistencies. > > Maybe we should report it to PHP developers. > But anyway, I am not sure at all if it makes sense. What do you think > Greg? > > Thank you very much and sorry about my english. > > ------------------------------------------------------------------------ > > [2012-11-15 20:37:27] squiz > > -Status: Open > +Status: Feedback > -Assigned To: > +Assigned To: squiz > This is expected behaviour because PHP will not tokenize those tags as > open and > close tags. It tokenizes them as T_INLINE_HTML. > > If you run PHPCS over a file that only contains short open tags with > that ini setting > disabled, you'll get this: > FILE: /Users/gsherwood/Sites/Projects/PHP_CodeSniffer/temp.php > -------------------------------------------------------------------------------- > FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) > -------------------------------------------------------------------------------- > 1 | WARNING | No PHP code was found in this file and short open tags > are not > | | allowed by this install of PHP. This file may be using > short > | | open tags but PHP does not allow them. > -------------------------------------------------------------------------------- > > If you want to write a sniff that detects short open tags, you're going > to need to look > for T_INLINE_HTML tokens and then see if you can find <? or <% tags in > the content. > They will look like this: > > Process token 0 on line 1 [lvl:0;]: T_INLINE_HTML => <? echo TRUE; ?>\n > Process token 1 on line 2 [lvl:0;]: T_INLINE_HTML => <% echo FALSE; > %>\n > > So should be fairly easy to find. > > If that doesn't make sense, please let me know and I'll try and help get > your sniff > working. > > ------------------------------------------------------------------------ > > [2012-11-15 13:12:11] willianveiga > > -Summary: PHP_CodeSniffer doesn&#039;t detect &lt;% %&gt; and &lt;? > ?&gt; when > short_open_tag and asp_tags > +Summary: PHP_CodeSniffer doesn&#039;t detect &lt;% and &lt;? if > short_open_tag and asp_tags are off > > > ------------------------------------------------------------------------ > > [2012-11-15 13:10:41] willianveiga > > Description: > ------------ > If short_open_tag and asp_tags are Off in php.ini, using the following > sniff, PHP_CodeSniffer will not complain about coding standards > violations: > > <?php > class CIStandard_Sniffs_Php_MustUseFullOpeningTagsSniff implements > PHP_CodeSniffer_Sniff > { > public function register() > { > return [T_OPEN_TAG]; > } > > public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) > { > $tokens = $phpcsFile->getTokens(); > $token = $tokens[$stackPtr]; > $content = trim($token['content']); > > if ($token['code'] === T_OPEN_TAG && $content != '<?php') > { > $error = 'Always use full PHP opening tags (<?php), in case > a server does not have short_open_tag enabled. found %s'; > $data = [$content]; > $phpcsFile->addError($error, $stackPtr, 'Found', $data); > } > } > } > > Test script: > --------------- > <?php echo TRUE; ?> > <? echo TRUE; ?> > <% echo FALSE; %> > > Expected result: > ---------------- > -------------------------------------------------------------------------------- > FOUND 2 ERROR(S) AFFECTING 2 LINE(S) > -------------------------------------------------------------------------------- > 2 | ERROR | Always use full PHP opening tags (<?php), in case a server > does > | | not have short_open_tag enabled. found <? > 3 | ERROR | Always use full PHP opening tags (<?php), in case a server > does > | | not have short_open_tag enabled. found <% > -------------------------------------------------------------------------------- > > Time: 0 seconds, Memory: 1.50Mb > > Actual result: > -------------- > Time: 0 seconds, Memory: 1.50Mb > > ------------------------------------------------------------------------ > > > -- > Edit this bug report at > https://pear.php.net/bugs/bug.php?id=19708&edit=1 > > econds, Memory: 1.50Mb > > ------------------------------------------------------------------------ > > > -- > Edit this bug report at > https://pear.php.net/bugs/bug.php?id=19708&edit=1 >

« previous php.pear.qa (#6125) next »