Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
windaishi committed Nov 28, 2024
1 parent 1bee062 commit ad0050e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

// phpcs:disable VIISON.Strings.DateMethodsInSqlSniff.NoNow
// phpcs:disable VIISON.Strings.DateMethodsInSqlSniff.NoCurrentTimestamp

/**
* 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 NoNowInMySqlSniff implements Sniff
class DateMethodsInSqlSniff implements Sniff
{
/**
* @inheritdoc
Expand All @@ -32,12 +35,17 @@ public function process(File $phpcsFile, $stackPtr)

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

if (stripos($content, 'CURRENT_TIMESTAMP(') !== false) {
$error = 'The usage of the method CURRENT_TIMESTAMP() in SQL queries is not allowed. Use UTC_TIMESTAMP() instead.';
$phpcsFile->addError($error, $stackPtr, 'NoCurrentTimestampInSql');
$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');
}
}
}

This file was deleted.

0 comments on commit ad0050e

Please sign in to comment.