From 961d13c3f1bfb13a82954793d1879a8974db3bbf Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 13 Jul 2016 21:13:06 +0200 Subject: [PATCH 1/4] Add sniff to disallow alternative PHP open tags. Fixes #580 This should be removed again if the upstream issue squizlabs/PHP_CodeSniffer#1063 would get traction and the associated PR would be merged *and* the minimum PHPCS version for WPCS would become higher than the version in which the PR is merged ;-) --- WordPress-Core/ruleset.xml | 1 + .../PHP/DisallowAlternativeOpenTagSniff.php | 123 ++++++++++++++++++ .../DisallowAlternativeOpenTagUnitTest.inc | 9 ++ .../DisallowAlternativeOpenTagUnitTest.php | 77 +++++++++++ 4 files changed, 210 insertions(+) create mode 100644 WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php create mode 100644 WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.inc create mode 100644 WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index c2d1a687b9..321bbf652c 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -21,6 +21,7 @@ + diff --git a/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php b/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php new file mode 100644 index 0000000000..7a2afb601c --- /dev/null +++ b/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php @@ -0,0 +1,123 @@ + + */ +class WordPress_Sniffs_PHP_DisallowAlternativeOpenTagSniff implements PHP_CodeSniffer_Sniff { + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + $tokens = array( + T_OPEN_TAG, + T_OPEN_TAG_WITH_ECHO, + ); + + $asp_enabled = (boolean) ini_get( 'asp_tags' ); + $short_enabled = (boolean) ini_get( 'short_open_tag' ); + + if ( false === $asp_enabled || ( true === $asp_enabled && false === $short_enabled ) ) { + $tokens[] = T_INLINE_HTML; + } + + return $tokens; + + } // end register() + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $openTag = $tokens[ $stackPtr ]; + $asp_enabled = (boolean) ini_get( 'asp_tags' ); + $short_enabled = (boolean) ini_get( 'short_open_tag' ); + + if ( T_OPEN_TAG === $openTag['code'] ) { + if ( '<%' === $openTag['content'] ) { + $error = 'ASP style opening tag used; expected "addError( $error, $stackPtr, 'ASPOpenTagFound', $data ); + } + + if ( ' + diff --git a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php b/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php new file mode 100644 index 0000000000..71e536b2e0 --- /dev/null +++ b/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php @@ -0,0 +1,77 @@ + + */ +class WordPress_Tests_PHP_DisallowAlternativeOpenTagUnitTest extends AbstractSniffUnitTest { + + /** + * Returns the lines where errors should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of errors that should occur on that line. + * + * @return array + */ + public function getErrorList() { + $asp_enabled = (boolean) ini_get( 'asp_tags' ); + $short_enabled = (boolean) ini_get( 'short_open_tag' ); + + $errors = array( + 6 => 1, + ); + + if ( true === $asp_enabled ) { + $errors[4] = 1; + } + if ( true === $asp_enabled && ( true === $short_enabled || defined( 'HHVM_VERSION' ) === true ) ) { + $errors[5] = 1; + } + + return $errors; + } // end getErrorList() + + /** + * Returns the lines where warnings should occur. + * + * The key of the array should represent the line number and the value + * should represent the number of warnings that should occur on that line. + * + * @return array + */ + public function getWarningList() { + $asp_enabled = (boolean) ini_get( 'asp_tags' ); + $short_enabled = (boolean) ini_get( 'short_open_tag' ); + + $warnings = array(); + + if ( false === $asp_enabled ) { + $warnings = array( + 4 => 1, + 5 => 1, + ); + } elseif ( false === $short_enabled && false === defined( 'HHVM_VERSION' ) ) { + $warnings = array( + 5 => 1, + ); + } + + return $warnings; + + } // end getWarningList() + +} // end class From 2cdba84ca7f55dabcc65ee6118da286ca7db6541 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Jul 2016 12:31:12 +0200 Subject: [PATCH 2/4] Make alternative openings tags fixable. Only implemented for when we are sure we've found alternative opening tags. The 'maybe' causes which need manual inspection should not be auto-fixable. --- .../PHP/DisallowAlternativeOpenTagSniff.php | 72 ++++++++++++++++++- .../DisallowAlternativeOpenTagUnitTest.fixed | 9 +++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed diff --git a/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php b/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php index 7a2afb601c..42c8d0eb6a 100644 --- a/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php +++ b/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php @@ -59,13 +59,33 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { if ( '<%' === $openTag['content'] ) { $error = 'ASP style opening tag used; expected "addError( $error, $stackPtr, 'ASPOpenTagFound', $data ); + + $closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' ); + + if ( false === $closer ) { + $phpcsFile->addError( $error, $stackPtr, 'ASPOpenTagFound', $data ); + } else { + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPOpenTagFound', $data ); + if ( true === $fix ) { + $this->add_changeset( $phpcsFile, $stackPtr, $closer ); + } + } } if ( '' ); + + if ( false === $closer ) { + $phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); + } else { + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); + if ( true === $fix ) { + $this->add_changeset( $phpcsFile, $stackPtr, $closer ); + } + } } } @@ -77,7 +97,17 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { $openTag['content'], $nextVar['content'], ); - $phpcsFile->addError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); + + $closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' ); + + if ( false === $closer ) { + $phpcsFile->addError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); + } else { + $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); + if ( true === $fix ) { + $this->add_changeset( $phpcsFile, $stackPtr, $closer ); + } + } } if ( ( true === $asp_enabled && false === $short_enabled ) && ( T_INLINE_HTML === $openTag['code'] && 0 === strpos( $openTag['content'], '<%=' ) ) ) { @@ -120,4 +150,40 @@ private function get_snippet( $content, $length = 40 ) { return $snippet; } // end get_snippet() + /** + * Try and find a matching PHP closing tag. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param array $tokens The token stack. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * @param string $content The expected content of the closing tag to match the opener. + * @return int|false Pointer to the position in the stack for the closing tag or false if not found. + */ + private function find_closing_tag( PHP_CodeSniffer_File $phpcsFile, $tokens, $stackPtr, $content ) { + $closer = $phpcsFile->findNext( T_CLOSE_TAG, ( $stackPtr + 1 ) ); + + if ( false !== $closer ) { + if ( $content === $tokens[ $closer ]['content'] ) { + return $closer; + } + } + + return false; + } // end find_closing_tag() + + /** + * Add a changeset to replace the alternative PHP tags. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $open_tag_pointer Stack pointer to the PHP open tag. + * @param int $close_tag_pointer Stack pointer to the PHP close tag. + */ + private function add_changeset( $phpcsFile, $open_tag_pointer, $close_tag_pointer ) { + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->replaceToken( $open_tag_pointer, 'fixer->replaceToken( $close_tag_pointer, '?>' ); + $phpcsFile->fixer->endChangeset(); + } // end add_changeset() + } // end class diff --git a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed b/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed new file mode 100644 index 0000000000..93ae12630a --- /dev/null +++ b/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed @@ -0,0 +1,9 @@ +
+ +Some content here. + + + +
From be045879d75884c16b722988eba4b2cbea55d498 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 14 Jul 2016 19:32:20 +0200 Subject: [PATCH 3/4] Rename sniff to `DisallowAlternativePHPTags`. --- WordPress-Core/ruleset.xml | 2 +- ...OpenTagSniff.php => DisallowAlternativePHPTagsSniff.php} | 6 ++++-- ...tTest.fixed => DisallowAlternativePHPTagsUnitTest.fixed} | 0 ...gUnitTest.inc => DisallowAlternativePHPTagsUnitTest.inc} | 0 ...gUnitTest.php => DisallowAlternativePHPTagsUnitTest.php} | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) rename WordPress/Sniffs/PHP/{DisallowAlternativeOpenTagSniff.php => DisallowAlternativePHPTagsSniff.php} (96%) rename WordPress/Tests/PHP/{DisallowAlternativeOpenTagUnitTest.fixed => DisallowAlternativePHPTagsUnitTest.fixed} (100%) rename WordPress/Tests/PHP/{DisallowAlternativeOpenTagUnitTest.inc => DisallowAlternativePHPTagsUnitTest.inc} (100%) rename WordPress/Tests/PHP/{DisallowAlternativeOpenTagUnitTest.php => DisallowAlternativePHPTagsUnitTest.php} (93%) diff --git a/WordPress-Core/ruleset.xml b/WordPress-Core/ruleset.xml index 321bbf652c..b4e973a70f 100644 --- a/WordPress-Core/ruleset.xml +++ b/WordPress-Core/ruleset.xml @@ -21,7 +21,7 @@ - + diff --git a/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php b/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php similarity index 96% rename from WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php rename to WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php index 42c8d0eb6a..b2270e3387 100644 --- a/WordPress/Sniffs/PHP/DisallowAlternativeOpenTagSniff.php +++ b/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php @@ -8,7 +8,9 @@ */ /** - * Makes sure that no alternative PHP open tags are used. + * Verifies that no alternative PHP open tags are used. + * + * If alternative PHP open tags are found, this sniff can fix both the open and close tags. * * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/580 * @@ -16,7 +18,7 @@ * @package PHP_CodeSniffer * @author Juliette Reinders Folmer */ -class WordPress_Sniffs_PHP_DisallowAlternativeOpenTagSniff implements PHP_CodeSniffer_Sniff { +class WordPress_Sniffs_PHP_DisallowAlternativePHPTagsSniff implements PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. diff --git a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.fixed similarity index 100% rename from WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.fixed rename to WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.fixed diff --git a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.inc b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc similarity index 100% rename from WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.inc rename to WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc diff --git a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php similarity index 93% rename from WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php rename to WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php index 71e536b2e0..f27083a7ca 100644 --- a/WordPress/Tests/PHP/DisallowAlternativeOpenTagUnitTest.php +++ b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php @@ -8,7 +8,7 @@ */ /** - * Unit test class for the DisallowAlternativeOpenTag sniff. + * Unit test class for the DisallowAlternativePHPTags sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. @@ -17,7 +17,7 @@ * @package PHP_CodeSniffer * @author Juliette Reinders Folmer */ -class WordPress_Tests_PHP_DisallowAlternativeOpenTagUnitTest extends AbstractSniffUnitTest { +class WordPress_Tests_PHP_DisallowAlternativePHPTagsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. From 34d436f567e69d36f6d94f6a5abdf8331c2c3385 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 22 Jul 2016 02:10:38 +0200 Subject: [PATCH 4/4] Revisit the complete logic of the `DisallowAlternativePHPTags` Sniff. New & improved version. Includes additional unit tests. --- .travis.yml | 1 + .../PHP/DisallowAlternativePHPTagsSniff.php | 178 +++++++++++------- .../DisallowAlternativePHPTagsUnitTest.fixed | 9 - .../DisallowAlternativePHPTagsUnitTest.inc | 11 +- ...sallowAlternativePHPTagsUnitTest.inc.fixed | 18 ++ .../DisallowAlternativePHPTagsUnitTest.php | 63 ++++--- myconfig.ini | 3 + 7 files changed, 179 insertions(+), 104 deletions(-) delete mode 100644 WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.fixed create mode 100644 WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc.fixed create mode 100644 myconfig.ini diff --git a/.travis.yml b/.travis.yml index ea05038248..500c1a8546 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,7 @@ matrix: - env: PHPCS_BRANCH=3.0 before_script: + - if [[ $TRAVIS_PHP_VERSION != "7.0" && $TRAVIS_PHP_VERSION != "nightly" && $TRAVIS_PHP_VERSION != "hhvm" ]]; then phpenv config-add myconfig.ini; fi - export PHPCS_DIR=/tmp/phpcs - export PHPCS_BIN=$(if [[ $PHPCS_BRANCH == 3.0 ]]; then echo $PHPCS_DIR/bin/phpcs; else echo $PHPCS_DIR/scripts/phpcs; fi) - mkdir -p $PHPCS_DIR && git clone --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git -b $PHPCS_BRANCH $PHPCS_DIR diff --git a/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php b/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php index b2270e3387..e2690c16b6 100644 --- a/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php +++ b/WordPress/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php @@ -3,7 +3,7 @@ * WordPress Coding Standard. * * @category PHP - * @package PHP_CodeSniffer + * @package PHP\CodeSniffer\WordPress-Coding-Standards * @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ */ @@ -15,31 +15,34 @@ * @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/580 * * @category PHP - * @package PHP_CodeSniffer + * @package PHP\CodeSniffer\WordPress-Coding-Standards * @author Juliette Reinders Folmer */ class WordPress_Sniffs_PHP_DisallowAlternativePHPTagsSniff implements PHP_CodeSniffer_Sniff { + /** + * Whether ASP tags are enabled or not. + * + * @var bool + */ + private $asp_tags = false; + /** * Returns an array of tokens this test wants to listen for. * * @return array */ public function register() { - $tokens = array( + if ( version_compare( PHP_VERSION, '7.0.0alpha1', '<' ) ) { + $this->asp_tags = (bool) ini_get( 'asp_tags' ); + } + + return array( T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, + T_INLINE_HTML, ); - $asp_enabled = (boolean) ini_get( 'asp_tags' ); - $short_enabled = (boolean) ini_get( 'short_open_tag' ); - - if ( false === $asp_enabled || ( true === $asp_enabled && false === $short_enabled ) ) { - $tokens[] = T_INLINE_HTML; - } - - return $tokens; - } // end register() /** @@ -52,52 +55,51 @@ public function register() { * @return void */ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { - $tokens = $phpcsFile->getTokens(); - $openTag = $tokens[ $stackPtr ]; - $asp_enabled = (boolean) ini_get( 'asp_tags' ); - $short_enabled = (boolean) ini_get( 'short_open_tag' ); + $tokens = $phpcsFile->getTokens(); + $openTag = $tokens[ $stackPtr ]; + $content = $openTag['content']; + + if ( '' === trim( $content ) ) { + return; + } if ( T_OPEN_TAG === $openTag['code'] ) { - if ( '<%' === $openTag['content'] ) { - $error = 'ASP style opening tag used; expected "find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' ); + if ( '<%' === $content ) { + $error = 'ASP style opening tag used; expected "find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' ); + $error_id = 'ASPOpenTagFound'; - if ( false === $closer ) { - $phpcsFile->addError( $error, $stackPtr, 'ASPOpenTagFound', $data ); - } else { - $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPOpenTagFound', $data ); - if ( true === $fix ) { - $this->add_changeset( $phpcsFile, $stackPtr, $closer ); - } - } + } elseif ( false !== strpos( $content, '' ); + $error_id = 'ScriptOpenTagFound'; } - if ( '' ); + if ( isset( $error, $closer, $error_id ) ) { + $data = array( $content ); if ( false === $closer ) { - $phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); + $phpcsFile->addError( $error, $stackPtr, $error_id, $data ); } else { - $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); + $fix = $phpcsFile->addFixableError( $error, $stackPtr, $error_id, $data ); if ( true === $fix ) { - $this->add_changeset( $phpcsFile, $stackPtr, $closer ); + $this->add_changeset( $phpcsFile, $tokens, $stackPtr, $closer ); } } } + + return; } - if ( T_OPEN_TAG_WITH_ECHO === $openTag['code'] && '<%=' === $openTag['content'] ) { + if ( T_OPEN_TAG_WITH_ECHO === $openTag['code'] && '<%=' === $content ) { $error = 'ASP style opening tag used with echo; expected "findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ) ]; + $nextVar = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), null, true ); + $snippet = $this->get_snippet( $tokens[ $nextVar ]['content'] ); $data = array( - $nextVar['content'], - $openTag['content'], - $nextVar['content'], + $snippet, + $content, + $snippet, ); $closer = $this->find_closing_tag( $phpcsFile, $tokens, $stackPtr, '%>' ); @@ -107,50 +109,67 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } else { $fix = $phpcsFile->addFixableError( $error, $stackPtr, 'ASPShortOpenTagFound', $data ); if ( true === $fix ) { - $this->add_changeset( $phpcsFile, $stackPtr, $closer ); + $this->add_changeset( $phpcsFile, $tokens, $stackPtr, $closer, true ); } } + + return; } - if ( ( true === $asp_enabled && false === $short_enabled ) && ( T_INLINE_HTML === $openTag['code'] && 0 === strpos( $openTag['content'], '<%=' ) ) ) { + // Account for incorrect script open tags. The "(?:]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match ) ) { + $error = 'Script style opening tag used; expected "get_snippet( $content, $match[1] ); + $data = array( $match[1] . $snippet ); - $error = 'Possible use of ASP style short opening tags. Needs manual inspection. Found: %s'; - $data = array( $this->get_snippet( $openTag['content'] ) ); - $phpcsFile->addWarning( $error, $stackPtr, 'MaybeASPShortOpenTagFound', $data ); - } + $phpcsFile->addError( $error, $stackPtr, 'ScriptOpenTagFound', $data ); - if ( false === $asp_enabled && T_INLINE_HTML === $openTag['code'] ) { + return; + } - $data = array( $this->get_snippet( $openTag['content'] ) ); + if ( T_INLINE_HTML === $openTag['code'] && false === $this->asp_tags ) { + if ( false !== strpos( $content, '<%=' ) ) { + $error = 'Possible use of ASP style short opening tags detected. Needs manual inspection. Found: %s'; + $snippet = $this->get_snippet( $content, '<%=' ); + $data = array( '<%=' . $snippet ); - if ( 0 === strpos( $openTag['content'], '<%=' ) ) { - $error = 'Possible use of ASP style short opening tags. Needs manual inspection. Found: %s'; $phpcsFile->addWarning( $error, $stackPtr, 'MaybeASPShortOpenTagFound', $data ); - } elseif ( 0 === strpos( $openTag['content'], '<%' ) ) { - $error = 'Possible use of ASP style opening tags. Needs manual inspection. Found: %s'; + + } elseif ( false !== strpos( $content, '<%' ) ) { + $error = 'Possible use of ASP style opening tags detected. Needs manual inspection. Found: %s'; + $snippet = $this->get_snippet( $content, '<%' ); + $data = array( '<%' . $snippet ); + $phpcsFile->addWarning( $error, $stackPtr, 'MaybeASPOpenTagFound', $data ); - } elseif ( 0 === strpos( $openTag['content'], ' + + + diff --git a/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc.fixed b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc.fixed new file mode 100644 index 0000000000..2b1c151387 --- /dev/null +++ b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.inc.fixed @@ -0,0 +1,18 @@ +
+ +Some content here. + +

Some text and some more text

+ +

Some text and some more text

+ + + + +
diff --git a/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php index f27083a7ca..f68c4a94de 100644 --- a/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php +++ b/WordPress/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php @@ -3,7 +3,7 @@ * Unit test class for WordPress Coding Standard. * * @category PHP - * @package PHP_CodeSniffer + * @package PHP\CodeSniffer\WordPress-Coding-Standards * @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ */ @@ -14,11 +14,38 @@ * coding standard. Expected errors and warnings are stored in this class. * * @category PHP - * @package PHP_CodeSniffer + * @package PHP\CodeSniffer\WordPress-Coding-Standards * @author Juliette Reinders Folmer */ class WordPress_Tests_PHP_DisallowAlternativePHPTagsUnitTest extends AbstractSniffUnitTest { + /** + * Whether ASP tags are enabled or not. + * + * @var bool + */ + private $asp_tags = false; + + /** + * Get the ini values only once. + */ + protected function setUp() { + parent::setUp(); + + if ( version_compare( PHP_VERSION, '7.0.0alpha1', '<' ) ) { + $this->asp_tags = (bool) ini_get( 'asp_tags' ); + } + } + + /** + * Skip this test on HHVM. + * + * @return bool Whether to skip this test. + */ + protected function shouldSkipTest() { + return defined( 'HHVM_VERSION' ); + } + /** * Returns the lines where errors should occur. * @@ -28,22 +55,22 @@ class WordPress_Tests_PHP_DisallowAlternativePHPTagsUnitTest extends AbstractSni * @return array */ public function getErrorList() { - $asp_enabled = (boolean) ini_get( 'asp_tags' ); - $short_enabled = (boolean) ini_get( 'short_open_tag' ); - $errors = array( - 6 => 1, + 8 => 1, + 11 => 1, + 12 => 1, + 15 => 1, ); - if ( true === $asp_enabled ) { + if ( true === $this->asp_tags ) { $errors[4] = 1; - } - if ( true === $asp_enabled && ( true === $short_enabled || defined( 'HHVM_VERSION' ) === true ) ) { $errors[5] = 1; + $errors[6] = 1; + $errors[7] = 1; } return $errors; - } // end getErrorList() + } /** * Returns the lines where warnings should occur. @@ -54,24 +81,18 @@ public function getErrorList() { * @return array */ public function getWarningList() { - $asp_enabled = (boolean) ini_get( 'asp_tags' ); - $short_enabled = (boolean) ini_get( 'short_open_tag' ); - $warnings = array(); - if ( false === $asp_enabled ) { + if ( false === $this->asp_tags ) { $warnings = array( 4 => 1, 5 => 1, - ); - } elseif ( false === $short_enabled && false === defined( 'HHVM_VERSION' ) ) { - $warnings = array( - 5 => 1, + 6 => 1, + 7 => 1, ); } return $warnings; + } - } // end getWarningList() - -} // end class +} // End class. diff --git a/myconfig.ini b/myconfig.ini new file mode 100644 index 0000000000..8c5f68f520 --- /dev/null +++ b/myconfig.ini @@ -0,0 +1,3 @@ +; Allow ASP-style <% %> tags. +; http://php.net/asp-tags +asp_tags = On