From 1bee06282ca62552fc1fa639bb3fb49aae9df05e Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:27:32 +0100 Subject: [PATCH 1/7] Add sniffs for NOW() and CURRENT_TIMESTAMP() --- .../Sniffs/Strings/NoNowInMySqlSniff.php | 43 +++++++++++++++++++ ...tcTimestampWithoutArgumentInMySqlSniff.php | 37 ++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php create mode 100644 php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php new file mode 100644 index 0000000..0e33e91 --- /dev/null +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php @@ -0,0 +1,43 @@ +getTokens()[$stackPtr]; + $content = $token['content']; + + 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'); + } + + 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'); + } + } +} diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php new file mode 100644 index 0000000..ec3e995 --- /dev/null +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php @@ -0,0 +1,37 @@ +getTokens()[$stackPtr]; + $content = $token['content']; + + 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, 'FoundNowFunction'); + } + } +} From ad0050ee790117e51e73b1ceac41288cc62420f3 Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:40:38 +0100 Subject: [PATCH 2/7] WIP --- ...SqlSniff.php => DateMethodsInSqlSniff.php} | 14 +++++-- ...tcTimestampWithoutArgumentInMySqlSniff.php | 37 ------------------- 2 files changed, 11 insertions(+), 40 deletions(-) rename php/php-codesniffer-standard/VIISON/Sniffs/Strings/{NoNowInMySqlSniff.php => DateMethodsInSqlSniff.php} (68%) delete mode 100644 php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php similarity index 68% rename from php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php rename to php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index 0e33e91..9bd6a3f 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoNowInMySqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -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 @@ -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'); } } } diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php deleted file mode 100644 index ec3e995..0000000 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/NoUtcTimestampWithoutArgumentInMySqlSniff.php +++ /dev/null @@ -1,37 +0,0 @@ -getTokens()[$stackPtr]; - $content = $token['content']; - - 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, 'FoundNowFunction'); - } - } -} From f07e5be9d7b2081c6ad9669c7aa577bb4860a5f0 Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:46:25 +0100 Subject: [PATCH 3/7] WIP --- .../VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index 9bd6a3f..0ad5b62 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -4,8 +4,8 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; -// phpcs:disable VIISON.Strings.DateMethodsInSqlSniff.NoNow -// phpcs:disable VIISON.Strings.DateMethodsInSqlSniff.NoCurrentTimestamp +// phpcs:disable VIISON.Strings.DateMethodsInSql.NoNow +// phpcs:disable VIISON.Strings.DateMethodsInSql.NoCurrentTimestamp /** * This sniff disallows the usage of the method NOW() and CURRENT_TIMESTAMP() in SQL queries as they have weird @@ -33,12 +33,12 @@ public function process(File $phpcsFile, $stackPtr) $token = $phpcsFile->getTokens()[$stackPtr]; $content = $token['content']; - if (stripos($content, 'NOW(') !== false) { + if (mb_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, 'NoNow'); } - if (stripos($content, 'CURRENT_TIMESTAMP(') !== false) { + if (mb_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, 'NoCurrentTimestamp'); } From 247569d62fc41b0dbaf36c99af40bd238f8780ad Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:31:12 +0100 Subject: [PATCH 4/7] WIP --- .../VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index 0ad5b62..8b2a555 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -39,7 +39,7 @@ public function process(File $phpcsFile, $stackPtr) } if (mb_stripos($content, 'CURRENT_TIMESTAMP(') !== false) { - $error = 'The usage of the method CURRENT_TIMESTAMP() in SQL queries is not allowed. Use UTC_TIMESTAMP() instead.'; + $error = 'The usage of the method CURRENT_TIMESTAMP() in SQL queries is not allowed. Use (UTC_TIMESTAMP()) (with brackets) instead.'; $phpcsFile->addError($error, $stackPtr, 'NoCurrentTimestamp'); } From fb5bccec5d49e9c3543ca413066422270cec9dc6 Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:32:03 +0100 Subject: [PATCH 5/7] WIP --- .../VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index 8b2a555..701e2fa 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -34,12 +34,12 @@ public function process(File $phpcsFile, $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() instead.'; + $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()) (with brackets) instead.'; + $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'); } From 0a8ba0eb054290a417c8e87fb06a30cd7301d6b8 Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:37:00 +0100 Subject: [PATCH 6/7] WIP --- .../VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index 701e2fa..e8519d4 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -6,6 +6,7 @@ // 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 @@ -34,12 +35,12 @@ public function process(File $phpcsFile, $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.'; + $error = 'The usage of the method NOW() in SQL queries is not allowed. Use UTC_TIMESTAMP() 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.'; + $error = 'The usage of the method CURRENT_TIMESTAMP() in SQL queries is not allowed. Use (UTC_TIMESTAMP()) (with brackets) instead.'; $phpcsFile->addError($error, $stackPtr, 'NoCurrentTimestamp'); } From 676d145764d18ce029779ef26a93a02816d573c2 Mon Sep 17 00:00:00 2001 From: Manuel Kress <6232639+windaishi@users.noreply.github.com> Date: Fri, 29 Nov 2024 10:30:44 +0100 Subject: [PATCH 7/7] Review feedback --- .../VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php index e8519d4..6a3fb13 100644 --- a/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php +++ b/php/php-codesniffer-standard/VIISON/Sniffs/Strings/DateMethodsInSqlSniff.php @@ -35,12 +35,12 @@ public function process(File $phpcsFile, $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() instead.'; + $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()) (with brackets) instead.'; + $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'); }