Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace VIISON\StyleGuide\PHPCS\Standards\VIISON\Sniffs\Strings;

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

// phpcs:disable VIISON.Strings.DateMethodsInSql.NoNow
// phpcs:disable VIISON.Strings.DateMethodsInSql.NoCurrentTimestamp
// phpcs:disable VIISON.Strings.DateMethodsInSql.UtcTimestampOnlyWithArgument

/**
* This sniff disallows the usage of the method NOW() and CURRENT_TIMESTAMP() in SQL queries as they have weird
* timezone behavior. It suggests using UTC_TIMESTAMP() instead.
*/
class DateMethodsInSqlSniff implements Sniff
{
/**
* @inheritdoc
*/
public function register()
{
return [
T_CONSTANT_ENCAPSED_STRING,
T_HEREDOC,
T_NOWDOC,
];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$token = $phpcsFile->getTokens()[$stackPtr];
$content = $token['content'];

if (mb_stripos($content, 'NOW(') !== false) {
$error = 'The usage of the method NOW() in SQL queries is not allowed. Use UTC_TIMESTAMP(3) instead.';
$phpcsFile->addError($error, $stackPtr, 'NoNow');
}

if (mb_stripos($content, 'CURRENT_TIMESTAMP(') !== false) {
$error = 'The usage of the method CURRENT_TIMESTAMP() in SQL queries is not allowed. Use (UTC_TIMESTAMP(3)) (with brackets) instead.';
$phpcsFile->addError($error, $stackPtr, 'NoCurrentTimestamp');
}

if (preg_match('/UTC_TIMESTAMP\\(\\s*?[012456789]?\\s*?\\)/', $content)) {
$error = 'Always pass 3 as an argument for UTC_TIMESTAMP() to ensure a consistent precision.';
$phpcsFile->addError($error, $stackPtr, 'UtcTimestampOnlyWithArgument');
}
}
}