Skip to content

Commit

Permalink
Add sniff for URL shorteners used in a theme
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed May 18, 2019
1 parent 8398440 commit 4256d38
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
117 changes: 117 additions & 0 deletions WPThemeReview/Sniffs/Privacy/NoUrlShortenersSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* WPThemeReview Coding Standard.
*
* @package WPTRT\WPThemeReview
* @link https://github.com/WPTRT/WPThemeReview
* @license https://opensource.org/licenses/MIT MIT
*/

namespace WPThemeReview\Sniffs\Privacy;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

/**
* Check if the file contains a URL shortener.
*
* @since 0.2.0
*/
class NoUrlShortenersSniff implements Sniff {

/**
* Error message template.
*
* @var string
*/
const ERROR_MSG = 'No URL shorteners should used in the theme. Found: "%s".';

/**
* Found used shortener in a file
*
* @var string
*/
protected $shortener;

/**
* List of url shorteners.
*
* @since 0.2.0
*
* @var array
*/
protected $url_shorteners = [
'goo.gl',
'bitly.com',
'polrproject.org',
'www.rebrandly.com',
'tinyurl.com',
'hootsuite.com/pages/owl',
'is.gd',
'buffer.com',
'df.ly',
'bit.do',
];

/**
* Supported Tokenizers
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'CSS',
'JS',
);

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
return array(
\T_CONSTANT_ENCAPSED_STRING,
\T_DOUBLE_QUOTED_STRING,
\T_INLINE_HTML,
\T_HEREDOC,
\T_NOWDOC,
\T_COMMENT,
\T_DOC_COMMENT,
\T_DOC_COMMENT_STAR,
\T_DOC_COMMENT_WHITESPACE,
\T_DOC_COMMENT_TAG,
\T_DOC_COMMENT_OPEN_TAG,
\T_DOC_COMMENT_CLOSE_TAG,
\T_DOC_COMMENT_STRING,
);
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position of the current token
* in the stack.
*
* @return void|int Optionally returns an integer stack pointer or void to continue
* normal file processing.
*/
public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
$content = $tokens[ $stackPtr ]['content'];

foreach ( $this->url_shorteners as $url_shortener ) {
if ( strpos( $content, $url_shortener ) !== false ) {
$phpcsFile->addError(
self::ERROR_MSG,
$stackPtr,
'URLShorternerFound',
array( $url_shortener )
);
}
}
}
}
11 changes: 11 additions & 0 deletions WPThemeReview/Tests/Privacy/NoUrlShortenersUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
// Check for url shorteners used in themes. Examples found in existing theme repository.
$option_descriptions = array(
$shortname . '_bitly_api_key' => array(
'title' => __('Bit.ly API Key', 'my-theme'),
'desc' => __('You can get your API Key in your <a href="https://bitly.com/a/account">bit.ly account</a> (you should to log in)', 'my-theme'), // BAD
),
);
// http://tinyurl.com/77w9wh // BAD
if (reshape) { // do this only if container has no size http://tinyurl.com/da2oa9 // BAD
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url); // BAD
44 changes: 44 additions & 0 deletions WPThemeReview/Tests/Privacy/NoUrlShortenersUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Unit test class for WPThemeReview Coding Standard.
*
* @package WPTRT\WPThemeReview
* @link https://github.com/WPTRT/WPThemeReview
* @license https://opensource.org/licenses/MIT MIT
*/

namespace WPThemeReview\Tests\Privacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the NoUrlShorteners sniff.
*
* @since 0.1.0
*/
class NoUrlShortenersUnitTest extends AbstractSniffUnitTest {

/**
* Returns the lines where errors should occur.
*
* @return array <int line number> => <int number of errors>
*/
public function getErrorList() {
return array(
6 => 1,
9 => 1,
10 => 1,
11 => 1,
);
}

/**
* Returns the lines where warnings should occur.
*
* @return array <int line number> => <int number of warnings>
*/
public function getWarningList() {
return array();
}

}

0 comments on commit 4256d38

Please sign in to comment.