Re: [PEAR-BUG] Bug #19708 [Com]: PHP_CodeSniffer doesn't detect <% and <? if short_open_tag and asp_tags are off
[email protected] ("
[email protected]")
Sat, 24 Nov 2012 22:47:59 +0800
| Newsgroups |
php.pear.qa,php.pear.bugs |
| Message-ID |
<[email protected]> |
LING-PENG
=D4=DA 2012-11-20=A3=AC8:23=A3=AC"[email protected]" <[email protected]>=
=D0=B4=B5=C0=A3=BA
> Edit report at https://pear.php.net/bugs/bug.php?id=3D19708&edit=3D1
>=20
> ID: 19708
> Comment by: [email protected]
> 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:=20
> New Comment:
>=20
> I understand. Maybe you can close this bug report.
> Thank you again.
>=20
>=20
> Previous Comments:
> ------------------------------------------------------------------------
>=20
> [2012-11-18 23:22:01] squiz
>=20
> I guess the problem is really that when those settings are off, the file
> is not valid=20
> PHP. I don't tokenize PHP files character by character (like I do for
> CSS and JS) so I=20
> dont know what context those tags are in.
>=20
> This could easily be done by a sniff though. One that detects if you are
> using short=20
> open tags and then bans them. It could look for T_OPEN TAG for cases
> where the ini=20
> setting is ON and it could also look for T_INLINE_HTML and look inside
> that content=20
> for cases where the ini setting is OFF.
>=20
> Adding additional tokenizing to the core for this would slow down
> processing, so a=20
> sniff works out better because it is optional to include in a standard.
>=20
> ------------------------------------------------------------------------
>=20
> [2012-11-16 18:00:30] willianveiga
>=20
> 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.
>=20
> 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.
>=20
> 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?
>=20
> Thank you very much and sorry about my english.
>=20
> ------------------------------------------------------------------------
>=20
> [2012-11-15 20:37:27] squiz
>=20
> -Status: Open
> +Status: Feedback
> -Assigned To:
> +Assigned To: squiz
> This is expected behaviour because PHP will not tokenize those tags as
> open and=20
> close tags. It tokenizes them as T_INLINE_HTML.
>=20
> If you run PHPCS over a file that only contains short open tags with
> that ini setting=20
> 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.
> --------------------------------------------------------------------------=
------
>=20
> If you want to write a sniff that detects short open tags, you're going
> to need to look=20
> for T_INLINE_HTML tokens and then see if you can find <? or <% tags in
> the content. =20
> They will look like this:
>=20
> Process token 0 on line 1 [lvl:0;]: T_INLINE_HTML =3D> <? echo TRUE; ?>\n
> Process token 1 on line 2 [lvl:0;]: T_INLINE_HTML =3D> <% echo FALSE;
> %>\n
>=20
> So should be fairly easy to find.
>=20
> If that doesn't make sense, please let me know and I'll try and help get
> your sniff=20
> working.
>=20
> ------------------------------------------------------------------------
>=20
> [2012-11-15 13:12:11] willianveiga
>=20
> -Summary: PHP_CodeSniffer doesn't detect <% %> and <?
> ?> when
> short_open_tag and asp_tags
> +Summary: PHP_CodeSniffer doesn't detect <% and <? if
> short_open_tag and asp_tags are off
>=20
>=20
> ------------------------------------------------------------------------
>=20
> [2012-11-15 13:10:41] willianveiga
>=20
> 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:
>=20
> <?php
> class CIStandard_Sniffs_Php_MustUseFullOpeningTagsSniff implements
> PHP_CodeSniffer_Sniff
> {
> public function register()
> {
> return [T_OPEN_TAG];
> }
>=20
> public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
> {
> $tokens =3D $phpcsFile->getTokens();
> $token =3D $tokens[$stackPtr];
> $content =3D trim($token['content']);
>=20
> if ($token['code'] =3D=3D=3D T_OPEN_TAG && $content !=3D '<?php') {=
> $error =3D 'Always use full PHP opening tags (<?php), in case
> a server does not have short_open_tag enabled. found %s';
> $data =3D [$content];
> $phpcsFile->addError($error, $stackPtr, 'Found', $data);
> }
> }
> }
>=20
> Test script:
> ---------------
> <?php echo TRUE; ?>
> <? echo TRUE; ?>
> <% echo FALSE; %>
>=20
> 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 <%
> --------------------------------------------------------------------------=
------
>=20
> Time: 0 seconds, Memory: 1.50Mb
>=20
> Actual result:
> --------------
> Time: 0 seconds, Memory: 1.50Mb
>=20
> ------------------------------------------------------------------------
>=20
>=20
> --=20
> Edit this bug report at https://pear.php.net/bugs/bug.php?id=3D19708&edit=3D=
1
>=20
> econds, Memory: 1.50Mb
>=20
> ------------------------------------------------------------------------
>=20
>=20
> --=20
> Edit this bug report at https://pear.php.net/bugs/bug.php?id=3D19708&edit=3D=
1
>=20