diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php new file mode 100644 index 0000000..6a3fb13 --- /dev/null +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -0,0 +1,52 @@ +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'); + } + } +}