diff --git a/CHANGELOG.md b/CHANGELOG.md index bc2bb83..d69328c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - upgrade docker system - [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical incompatibilities for SonarQube before 9.9 version +- clean unit test files (to have less other issues) ### Deleted diff --git a/src/test/resources/checks/AvoidDoubleQuote.php b/src/test/resources/checks/AvoidDoubleQuote.php index 30e0a47..bbde7b1 100644 --- a/src/test/resources/checks/AvoidDoubleQuote.php +++ b/src/test/resources/checks/AvoidDoubleQuote.php @@ -6,19 +6,43 @@ $isStudent = true; $cours = array('physique','chimie','informatique','philosophie'); -echo $lastName; -echo $name; -echo '
'; -echo "
"; // NOK {{Avoid using double quote ("), prefer using simple quote (')}} -echo $age; +mc = new MyClass(); + +mc->display($lastName); +mc->display($name); +mc->display('
'); +mc->display("
"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}} +mc->display($age); $identite = $lastName .' '. $name; -echo $identite; +mc->display($identite); + +mc->myFunction($name, $age, $isStudent); + +mc->myFunction("name", "age", "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}} + +mc->myFunction('name', 'age', 'isStudent'); -myFunction($name, $age, $isStudent); +mc->myFunction("name", 'age', "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}} -myFunction("name", "age", "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}} -myFunction('name', 'age', 'isStudent'); +class MyClass +{ + public function myFunction($name, $age, $isStudent) + { + if ($name == null) { + return 'toto' + $age; + } else { + return 'tata' + $isStudent; + } + } -myFunction("name", 'age', "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}} + public function display($msg) + { + if ($msg != null) { + return $msg; + } else { + return ''; + } + } +} \ No newline at end of file diff --git a/src/test/resources/checks/AvoidFullSQLRequest.php b/src/test/resources/checks/AvoidFullSQLRequest.php index 4785c1d..9f62f98 100644 --- a/src/test/resources/checks/AvoidFullSQLRequest.php +++ b/src/test/resources/checks/AvoidFullSQLRequest.php @@ -1,8 +1,8 @@ SqlCall('SELECT * FROM'); // NOK {{Don't use the query SELECT * FROM}} OtherClass->SqlCall('SeLeCt DiStInCt * FrOm'); // NOK {{Don't use the query SELECT * FROM}} - OtherClass->SqlCall('SeLeCt `table.name`, *, `my_field` FrOm'); // NOK {{Don't use the query SELECT * FROM}} OtherClass->SqlCall('select name from'); } diff --git a/src/test/resources/checks/AvoidGettingSizeCollectionInLoop.php b/src/test/resources/checks/AvoidGettingSizeCollectionInLoop.php index 543a8b4..8e123cb 100644 --- a/src/test/resources/checks/AvoidGettingSizeCollectionInLoop.php +++ b/src/test/resources/checks/AvoidGettingSizeCollectionInLoop.php @@ -5,7 +5,7 @@ /** * FOR STATEMENTS // RIGHT OPERAND */ -for ($i = 0; $i < count($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; $i < count($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -14,7 +14,7 @@ var_dump($array[$i]); } -for ($i = 0; $i < sizeof($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; $i < sizeof($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -23,7 +23,7 @@ var_dump($array[$i]); } -for ($i = 0; $i < iterator_count(new ArrayIterator($array)); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; $i < iterator_count(new ArrayIterator($array)); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -35,7 +35,7 @@ /** * FOR STATEMENTS // LEFT OPERAND */ -for ($i = 0; count($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; count($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -44,7 +44,7 @@ var_dump($array[$i]); } -for ($i = 0; sizeof($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; sizeof($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -53,7 +53,7 @@ var_dump($array[$i]); } -for ($i = 0; iterator_count(new ArrayIterator($array)) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} +for ($i = 0; iterator_count(new ArrayIterator($array)) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}} var_dump($array[$i]); } @@ -115,7 +115,7 @@ $i = 0; $size = count($array); -while ($size> $i) { // Compliant +while ($size > $i) { // Compliant var_dump($array[$i]); ++$i; } diff --git a/src/test/resources/checks/AvoidSQLRequestInLoop.php b/src/test/resources/checks/AvoidSQLRequestInLoop.php index 5a804bb..41088ab 100644 --- a/src/test/resources/checks/AvoidSQLRequestInLoop.php +++ b/src/test/resources/checks/AvoidSQLRequestInLoop.php @@ -6,7 +6,7 @@ class AvoidFullSQLRequest private $dbName = 'name'; private $dbPass = 'pass'; private $dbHost = 'host'; - private $query = 'SELECT * FROM Table'; + private $query = 'SELECT col1 FROM Table'; private $otherQuery = 'SELECT name FROM User'; private $connection; public function launchSQLRequest($someCondition) diff --git a/src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php b/src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php index 3431ab8..9a1c0b0 100644 --- a/src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php +++ b/src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php @@ -2,27 +2,19 @@ $a = 1; $b = 2; - function somme() // NOK {{Prefer local variables to globals}} { $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b']; } -somme(); -echo $b; - function somme2() // NOK {{Prefer local variables to globals}} { global $a, $b; - $b = $a + $b; + $c = $a + $b; } -somme2(); -echo $b; - -function somme3($a, $b) // Compliant +function somme3($c, $d) // Compliant { - return $a + $b; + $e = $c + $d; + return ++$e; } - -echo somme3($a, $b); diff --git a/src/test/resources/checks/IncrementCheck.php b/src/test/resources/checks/IncrementCheck.php index 16bfc68..2ea866f 100644 --- a/src/test/resources/checks/IncrementCheck.php +++ b/src/test/resources/checks/IncrementCheck.php @@ -3,7 +3,8 @@ function foo() { $counter = 0; - return $counter++; // NOK {{Remove the usage of $i++. prefer ++$i}} + $counter++; // NOK {{Remove the usage of $i++. prefer ++$i}} + return $counter; } function bar() diff --git a/src/test/resources/checks/NoFunctionCallWhenDeclaringForLoop.php b/src/test/resources/checks/NoFunctionCallWhenDeclaringForLoop.php index 4062cde..1dd5b6f 100644 --- a/src/test/resources/checks/NoFunctionCallWhenDeclaringForLoop.php +++ b/src/test/resources/checks/NoFunctionCallWhenDeclaringForLoop.php @@ -2,13 +2,13 @@ /* exemple 1 */ -for ($i = 1; $i <= 10; $i++) { +for ($i = 1; $i <= 10; ++$i) { echo $i; } /* exemple 2 */ -for ($i = 1; ; $i++) { +for ($i = 1; ; ++$i) { if ($i > 10) { break; } @@ -18,30 +18,30 @@ /* exemple 3 */ $i = 1; -for (; ;) { +while (true) { if ($i > 10) { break; } echo $i; - $i++; + ++$i; } /* exemple 4 */ -for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); // NOK {{Do not call a function in for-type loop declaration}} +for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, ++$i); // NOK {{Do not call a function in for-type loop declaration}} -function somewhat_calcMax() +function somewhatCalcMax() { return 500; } -for ($i = 0; $i <= somewhat_calcMax(); $i++) { // NOK {{Do not call a function in for-type loop declaration}} +for ($i = 0; $i <= somewhatCalcMax(); ++$i) { // NOK {{Do not call a function in for-type loop declaration}} var_dump($i); } -$maxI = somewhat_calcMax(); -for ($i = 0; $i <= $maxI; $i++) { // COMPLIANT +$maxI = somewhatCalcMax(); +for ($i = 0; $i <= $maxI; ++$i) { // COMPLIANT var_dump($i); }