Repository: VKCOM/noverify
Pull request #1177 (2022-05-03 06:43:35)

linter: copy rules
### SonarQube
- `usageExitOrDie` - https://rules.sonarsource.com/php/RSPEC-1799
- `useEvalCheck` - https://rules.sonarsource.com/php/RSPEC-1523
- `useSleepCheck` - https://rules.sonarsource.com/php/RSPEC-2964


Patch:
diff --git a/docs/checkers_doc.md b/docs/checkers_doc.md
index c944a4a..3ad1f97 100644
--- a/docs/checkers_doc.md
+++ b/docs/checkers_doc.md
@@ -4,7 +4,7 @@
 
 | Total checks | Checks enabled by default | Disabled checks by default | Autofixable checks |
 | ------------ | ------------------------- | -------------------------- | ------------------ |
-| 99           | 82                        | 17                         | 12                 |
+| 102           | 85                        | 17                         | 12                 |
 
 ## Table of contents
  - Enabled by default
@@ -89,6 +89,9 @@
    - [`undefinedVariable` checker](#undefinedvariable-checker)
    - [`unimplemented` checker](#unimplemented-checker)
    - [`unused` checker](#unused-checker)
+   - [`useEval` checker](#useeval-checker)
+   - [`useExitOrDie` checker](#useexitordie-checker)
+   - [`useSleep` checker](#usesleep-checker)
    - [`varShadow` checker](#varshadow-checker)
  - Disabled by default
    - [`argsReverse` checker](#argsreverse-checker)
@@ -1759,6 +1762,60 @@ return [$result, $err];
 <p><br></p>
 
 
+### `useEval` checker
+
+#### Description
+
+Report using `eval` function.
+
+#### Non-compliant code:
+```php
+eval("2 + 2");
+```
+
+#### Compliant code:
+```php
+// no eval
+```
+<p><br></p>
+
+
+### `useExitOrDie` checker
+
+#### Description
+
+Report using `exit` or `die` functions.
+
+#### Non-compliant code:
+```php
+exit(1);
+```
+
+#### Compliant code:
+```php
+// no exit
+```
+<p><br></p>
+
+
+### `useSleep` checker
+
+#### Description
+
+Report using `sleep` function.
+
+#### Non-compliant code:
+```php
+sleep(10);
+```
+
+#### Compliant code:
+```php
+// no sleep
+```
+<p><br></p>
+
+
 ### `varShadow` checker
 
 #### Description
diff --git a/src/cmd/embeddedrules/rules.php b/src/cmd/embeddedrules/rules.php
index bcc7c0c..4f3ddb6 100644
--- a/src/cmd/embeddedrules/rules.php
+++ b/src/cmd/embeddedrules/rules.php
@@ -674,3 +674,44 @@ function concatenationPrecedence() {
         $_ . $_ >> $_;
     }
 }
+
+/**
+ * @comment Report using `exit` or `die` functions.
+ * @before  exit(1);
+ * @after   // no exit
+ */
+function useExitOrDie() {
+  /**
+   * @warning Don't use the 'exit' function
+   */
+  exit($_);
+
+  /**
+   * @warning Don't use the 'die' function
+   */
+  die($_);
+}
+
+/**
+ * @comment Report using `eval` function.
+ * @before  eval("2 + 2");
+ * @after   // no eval
+ */
+function useEval() {
+  /**
+   * @warning Don't use the 'eval' function
+   */
+  eval($_);
+}
+
+/**
+ * @comment Report using `sleep` function.
+ * @before  sleep(10);
+ * @after   // no sleep
+ */
+function useSleep() {
+  /**
+   * @warning Don't use the 'sleep' function
+   */
+  sleep($_);
+}


Test patch:
diff --git a/src/tests/golden/testdata/mustache/golden.txt b/src/tests/golden/testdata/mustache/golden.txt
index 257811f..dfdf00a 100644
--- a/src/tests/golden/testdata/mustache/golden.txt
+++ b/src/tests/golden/testdata/mustache/golden.txt
@@ -22,6 +22,9 @@ MAYBE   ternarySimplify: Could rewrite as `$this->fileMode ?? (0666 & ~umask())`
 WARNING errorSilence: Don't use @, silencing errors is bad practice at testdata/mustache/src/Mustache/Cache/FilesystemCache.php:143
                 @chmod($fileName, $mode);
                 ^^^^^^^^^^^^^^^^^^^^^^^^
+WARNING useEval: Don't use the 'eval' function at testdata/mustache/src/Mustache/Cache/NoopCache.php:45
+        eval('?>' . $value);
+        ^^^^^^^^^^^^^^^^^^^
 MAYBE   ternarySimplify: Could rewrite as `$node[Mustache_Tokenizer::FILTERS] ?? array()` at testdata/mustache/src/Mustache/Compiler.php:99
                         isset($node[Mustache_Tokenizer::FILTERS]) ? $node[Mustache_Tokenizer::FILTERS] : array(),
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/src/tests/golden/testdata/phprocksyd/golden.txt b/src/tests/golden/testdata/phprocksyd/golden.txt
index ecbfb09..0cbf272 100644
--- a/src/tests/golden/testdata/phprocksyd/golden.txt
+++ b/src/tests/golden/testdata/phprocksyd/golden.txt
@@ -1,9 +1,39 @@
+WARNING useSleep: Don't use the 'sleep' function at testdata/phprocksyd/Phprocksyd.php:8
+        sleep(10);
+        ^^^^^^^^^
+WARNING useSleep: Don't use the 'sleep' function at testdata/phprocksyd/Phprocksyd.php:18
+        sleep(100);
+        ^^^^^^^^^^
 MAYBE   deprecated: Call to deprecated function dl (since: 5.3) at testdata/phprocksyd/Phprocksyd.php:73
             if (!extension_loaded($ext) && !dl($ext . '.so')) {
                                             ^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:75
+                exit(1);
+                ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:82
+            exit(1);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:87
+            exit(0);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:93
+            exit(1);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:98
+            exit(1);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:118
+            exit(1);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:129
+                exit(1);
+                ^^^^^^^
 ERROR   constCase: Constant 'NULL' should be used in lower case as 'null' at testdata/phprocksyd/Phprocksyd.php:321
             $n = stream_select($read, $write, $except, NULL);
                                                        ^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:325
+                exit(1);
+                ^^^^^^^
 WARNING invalidDocblock: Malformed @param $stream_id tag (maybe type is missing?) at testdata/phprocksyd/Phprocksyd.php:364
      * @param $stream_id
               ^^^^^^^^^^
@@ -31,9 +61,27 @@ WARNING invalidDocblock: Malformed @param $stream_id tag (maybe type is missing?
 WARNING invalidDocblock: Malformed @param $req tag (maybe type is missing?) at testdata/phprocksyd/Phprocksyd.php:444
      * @param $req
               ^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:481
+                exit(0);
+                ^^^^^^^
 ERROR   undefinedMethod: Call to undefined method {mixed}->run() at testdata/phprocksyd/Phprocksyd.php:479
                 $instance->run($req['params']);
                            ^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:557
+                        exit(1);
+                        ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:590
+            exit(0);
+            ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:619
+        exit(1);
+        ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Phprocksyd.php:685
+        exit(0);
+        ^^^^^^^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/phprocksyd/Simple.php:38
+            exit(1);
+            ^^^^^^^
 ERROR   constCase: Constant 'NULL' should be used in lower case as 'null' at testdata/phprocksyd/Simple.php:158
             $n = stream_select($read, $write, $except, NULL);
                                                        ^^^^
diff --git a/src/tests/golden/testdata/qrcode/golden.txt b/src/tests/golden/testdata/qrcode/golden.txt
index 912349e..e6b2f8e 100644
--- a/src/tests/golden/testdata/qrcode/golden.txt
+++ b/src/tests/golden/testdata/qrcode/golden.txt
@@ -85,6 +85,9 @@ MAYBE   callSimplify: Could simplify to $group[1] at testdata/qrcode/qrcode.php:
 WARNING switchDefault: Add 'default' branch to avoid unexpected unhandled condition values at testdata/qrcode/qrcode.php:697
     switch ($mask) {
     ^
+WARNING useExitOrDie: Don't use the 'exit' function at testdata/qrcode/qrcode.php:33
+  exit(0);
+  ^^^^^^^
 ERROR   classMembersOrder: Property $qr_capacity must go before methods in the class QRCode at testdata/qrcode/qrcode.php:890
   private $qr_capacity = [
   ^^
diff --git a/src/tests/inline/testdata/embeddedrules/useEval.php b/src/tests/inline/testdata/embeddedrules/useEval.php
new file mode 100644
index 0000000..d31339c
--- /dev/null
+++ b/src/tests/inline/testdata/embeddedrules/useEval.php
@@ -0,0 +1,7 @@
+<?php
+
+function useEval() {
+  $hello = "Hello NoVerify!";
+
+  eval("echo \"$hello\";"); // want `Don't use the 'eval' function`
+}
diff --git a/src/tests/inline/testdata/embeddedrules/useExitOrDie.php b/src/tests/inline/testdata/embeddedrules/useExitOrDie.php
new file mode 100644
index 0000000..b1b0c26
--- /dev/null
+++ b/src/tests/inline/testdata/embeddedrules/useExitOrDie.php
@@ -0,0 +1,9 @@
+<?php
+
+function useExitOrDie() {
+  exit; // want `Don't use the 'exit' function`
+  exit(1); // want `Don't use the 'exit' function`
+
+  die; // want `Don't use the 'die' function`
+  die("die"); // want `Don't use the 'die' function`
+}
diff --git a/src/tests/inline/testdata/embeddedrules/useSleep.php b/src/tests/inline/testdata/embeddedrules/useSleep.php
new file mode 100644
index 0000000..67973ed
--- /dev/null
+++ b/src/tests/inline/testdata/embeddedrules/useSleep.php
@@ -0,0 +1,5 @@
+<?php
+
+function useSleep() {
+  sleep(5); // want `Don't use the 'sleep' function`
+}